You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a preview API draft of new functionality for HTTP requests in Garry's Mod Lua.
Everything presented in this document is a subject to change without any notice.
There is a possibility of being unaccepted and/or altered by devs. For example:
Only partial functions may be implemented.
Function/Class name may be different in the final revision.
Some parts in this document are marked using tags to help distinguish between old/new stuff. Legend:
[N]: New functionality.
[U]: Functionality that could be user-implemented in the current game version.
Value: "GET"
The HTTP GET method requests a representation of the specified resource. Requests using GET should only retrieve data.
HTTP_POST
Value: "POST"
The HTTP POST method sends data to the server. The type of the body of the request is indicated by the Content-Type header.
HTTP_HEAD
Value: "HEAD"
The HTTP HEAD method requests the headers that are returned if the specified resource would be requested with an HTTP GET method. Such a request can be done before deciding to download a large resource to save bandwidth, for example.
HTTP_PUT
Value: "PUT"
The HTTP PUT request method creates a new resource or replaces a representation of the target resource with the request payload.
HTTP_DELETE
Value: "DELETE"
The HTTP DELETE request method deletes the specified resource.
HTTP_PATCH
Value: "PATCH"
The HTTP PATCH request method applies partial modifications to a resource.
HTTP_OPTIONS
Value: "OPTIONS"
The HTTP OPTIONS method is used to describe the communication options for the target resource. The client can specify a URL for the OPTIONS method, or an asterisk (*) to refer to the entire server.
Sets the request body string for POST data. If set, will override request parameters.
You can obtain a value of charset by calling the GetHeaders function.
body: Body string for POST data.
type: Content type for body.
[Q] Is this function really necessary, can't these be done using request headers (SetHeaders)? (If not, why does it exist? Convenience function?)
table GetCompletionCallbacks()[U]
Returns a table of all completion callbacks (sequence of functions to be called once a request is completed).
A direct reference to the internal table, so you are allowed to reorder callbacks too, by using table.sort function.
Adds the completion callback (a function to be called once this request is completed, ragardless of fail/success).
callback has the following function signature:
HTTPResponse: The response object.
identifier: Optional. If specified, it can be used to remove the callback with RemoveCompletionCallback function; otherwise, the callback is linked and unremovable.
identifier: String identifier of the completion callback to be removed.
table GetProgressCallbacks()[N]
Returns a table of all progress-report callbacks (sequence of functions to be called whenever a network change occurs in this request).
A direct reference to the internal table, so you are allowed to reorder callbacks too, by using table.sort function.
Adds the progress-report callback (a function to be called each time there is a network change in this request).
callback has the following function signature:
number: Transfer rate (download/upload speed in bytes-per-second).
number: Downloaded/Uploaded file-size so far (in bytes).
number: Total file-size (in bytes). May be nil if size can not be determined (i.e. when request is returning chunked-data).
number: Estimated time left (remaining time to completion in seconds). May be nil if total size can not be determined (i.e. when request is returning chunked-data).
identifier: Optional. If specified, it can be used to remove the callback with RemoveProgressCallback function; otherwise, the callback is linked and unremovable.
Removes the progress-report callback. See also AddProgressCallback function.
identifier: String identifier of the progress-report callback to be removed.
boolean IsCompleted()[U]
Returns true to indicate if this HTTP request has been completed.
boolean IsStarted()[U]
Returns true to indicate if this HTTP request is in-progress. (Note: This is not the same as not HttpFile:IsPaused()!)
boolean IsPaused()[X]
Returns true to indicate if this HTTP request is currently paused.
boolean CanResume()[X]
Returns true to indicate resume capability, if this HTTP request can be resumed successfully.
boolean Start()
Starts this HTTP request.
Returns true to indicate success, this HTTP request has started; otherwise, false.
boolean Pause()[X]
Pauses this HTTP request.
Returns true to indicate success, this HTTP request has been paused; otherwise, false.
boolean Stop()[N]
Stops this HTTP request.
Returns true to indicate success, this HTTP request has been stopped completely (loses all made progress); otherwise, false.
boolean Restart()[N]
Restarts this HTTP request. (Convenience function.)
Returns true to indicate success, this HTTP request has been restarted (stops and starts again); otherwise, false.
boolean Defer()[N]
Defers this HTTP request (only when in-progress), by moving it at the back of the queue.
Returns true if this request has been successfully deferred; otherwise, false.
boolean Prioritize()[N]
Prioritizes this HTTP request (only when in-progress), by moving it at the front of the queue.
Returns true if this request has been successfully prioritized; otherwise, false.
Demonstrates how to queue HTTP requests using HttpFile, and also print out their progress.
localfunctionprintf( ... ) MsgN( string.format( ... ) ) end-- Feed this table with HTTPRequest tables. See https://wiki.garrysmod.com/page/Structures/HTTPRequest-- Note: 'failed' and 'success' functions (if used) would be combined into an appropriate completion callback (internally).localqueue= {
{
method=HTTP_GET;
url='https://facepunch.com/';
success=function( code, body, headers )
printf( '[HTTP GET request to https://facepunch.com/] Legacy completion callback (success). Status code: #%d. Body length: %d. Headers:', code, #body )
PrintTable( headers, 1 )
end;
failed=function( reason )
printf( '[HTTP GET request to https://facepunch.com/] Legacy completion callback (failed). Reason: %q', reason )
end;
};
{
method=HTTP_GET;
url='https://wiki.garrysmod.com/page/Main_Page';
};
}
-- Generic/Shared completion callback for queued HTTP requests.localfunctionCCB( request, response )
ifresponse.Successthenprintf( '[HTTP %s request to %q] Completion callback (success). Status code: #%d. Body length: %d. Headers:', request:GetMethod( ), request:GetURL( ), response.StatusCode, #response.Body )
PrintTable( response.Headers, 1 )
elseprintf( '[HTTP %s request to %q] Completion callback (failed). Status code: #%d. Reason: %q', request:GetMethod( ), request:GetURL( ), response.StatusCode, response.Body )
endend-- Generic/Shared progress-report callback for queued HTTP requests.localfunctionPRCB( request, transferRate, currentSize, totalSize, eta )
printf( '[HTTP %s request to %q] Speed: %.2f B/s, Size: %.2f B, Total size: %.2f B, ETA: %d second(s)', request:GetMethod( ), request:GetURL( ), transferRate, currentSize, totalSizeor'N/A', etaor'N/A' )
end-- A helper function to rewrite function 'f' to provide 'arg' upon call.-- Used to rewrite generic functions above, to provide the request (HttpFile) object as the first argument.localfunctionRe( arg, f )
returnfunction( ... ) returnf( arg, ... ) endend-- Process queue.fori, requestinipairs( queue ) dolocalreq=HttpFile( request ) -- Create a new HttpFile object, and initialize it using the given HTTPRequest table values.req:AddCompletionCallback( Re( req, CCB ) ) -- Add a completion callback, to be notified upon completion of request.req:AddProgressCallback( Re( req, PRCB ) ) -- Add a progress-report callback, to be notified of new network change.ifreq:Start( ) then-- Attempt to initiate HTTP request.printf( '[#%d] HTTP %s request has been started, URL: %q', i, req:GetMethod( ), req:GetURL( ) )
endqueue[ i ] =reqend
Reserved.
I have fixed Markdown issues and updated 'Simple queue' example usage code.
Changelog
API rev. 2:
HttpFile
Defer()
andPrioritize()
functions.