Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Squirrel code for HTTP requests #531

Merged
merged 25 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
dd1983f
Squirrel side of HTTP requests
Erlite Nov 27, 2022
43f6dec
Move to Northstar.Custom
Erlite Nov 27, 2022
415f29a
Working HTTP requests from Squirrel
Erlite Nov 27, 2022
dcea336
Fix overlooked delete, add comments
Erlite Nov 29, 2022
ff06973
Add request success bool as return in req funcs.
Erlite Dec 1, 2022
1534153
Merge branch 'main' into squirrel-http-main
Erlite Dec 1, 2022
c8b60a3
Formatting issues.
Erlite Dec 2, 2022
5d45595
More formatting fixes from code reviews.
Erlite Dec 2, 2022
b918a39
Success callback uses struct now.
Erlite Dec 4, 2022
b49614d
Merge branch 'squirrel-http-main' of https://github.com/Erlite/Norths…
Erlite Dec 4, 2022
3b5781f
Merge branch 'main' into squirrel-http-main
Erlite Dec 4, 2022
890dea1
Never fix conflicts on mobile browsers, noted.
Erlite Dec 5, 2022
f255b76
HttpRequest.baseUrl -> url
Erlite Dec 5, 2022
85b878b
More formatting changes.
Erlite Dec 5, 2022
b2115ab
Merge branch 'main' into squirrel-http-main
Erlite Dec 5, 2022
69d02ce
Use struct for http failures
Erlite Dec 5, 2022
5642055
Merge branch 'squirrel-http-main' of https://github.com/Erlite/Norths…
Erlite Dec 5, 2022
4e40a7a
Merge tool is stupid
Erlite Dec 5, 2022
9724d97
Fix compile issues
Erlite Dec 5, 2022
9401815
Remove leftover semicolons
Erlite Dec 6, 2022
c1564c8
Formatting fixes.
Erlite Dec 8, 2022
6146c4d
Formatting.
Erlite Dec 8, 2022
f89add6
Update Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_reques…
Erlite Dec 8, 2022
9c6ecbb
Tabs not spaces
Erlite Dec 10, 2022
b10dbb1
Merge branch 'main' into squirrel-http-main
Erlite Dec 10, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Northstar.Custom/mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,10 @@
"ServerCallback": {
"Before": "MessageUtils_ServerInit"
}
},
{
"Path": "sh_northstar_http_requests.gnut",
"RunOn": "CLIENT || SERVER || UI"
}
],

Expand Down
235 changes: 235 additions & 0 deletions Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
globalize_all_functions

global enum HttpRequestMethod
{
GET = 0,
POST = 1
HEAD = 2,
PUT = 3,
DELETE = 4,
PATCH = 5,
OPTIONS = 6,
}

global struct HttpRequest
{
/** Method used for this http request. */
int method
/** Base URL of this http request. */
string url
/** Headers used for this http request. Some may get overridden or ignored. */
table< string, array< string > > headers
/** Query parameters for this http request. */
table< string, array< string > > queryParameters
/** The content type of this http request. Defaults to application/json & UTF-8 charset. */
string contentType = "application/json; charset=utf-8"
/** The body of this http request. If set, will override queryParameters.*/
string body
/** The timeout for the http request in seconds. Must be between 1 and 60. */
int timeout = 60
/** If set, the override to use for the User-Agent header. */
string userAgent
}

global struct HttpRequestResponse
{
/** The status code returned by the remote the call was made to. */
int statusCode
/** The body of the response. */
string body
/** The raw headers returned by the remote. */
string rawHeaders
/** A key -> values table of headers returned by the remote. */
table< string, array< string > > headers
}

global struct HttpRequestFailure
{
/** The error code returned by native for this failure. */
int errorCode
/** The reason why this http request failed. */
string errorMessage
}

struct HttpRequestCallbacks
{
/**
* The function to call if the HTTP request was a success.
* Passes in the response received from the remote.
*/
void functionref( HttpRequestResponse ) onSuccess

/**
* The function to call if the HTTP request failed.
*/
void functionref( HttpRequestFailure ) onFailure
}

table< int, HttpRequestCallbacks > pendingCallbacks
Erlite marked this conversation as resolved.
Show resolved Hide resolved

/**
* Called from native when a HTTP request is successful.
* This is internal and shouldn't be used.
* Keep in mind that the success can be successful, but have a non-success status code.
* @param handle The handle of the request we got a response for.
* @param statusCode The status code returned in the response.
* @param body The body returned for GET requests.
* @param headers The headers that were returned in the response.
*/
void function NSHandleSuccessfulHttpRequest( int handle, int statusCode, string body, string headers )
{
if ( !( handle in pendingCallbacks ) )
{
return
}

if ( pendingCallbacks[ handle ].onSuccess != null )
{
HttpRequestResponse response
response.statusCode = statusCode
response.body = body
response.rawHeaders = headers

// Parse the raw headers into key -> values
array<string> values = split( headers, "\n" )

foreach ( string header in values )
{
var index = header.find( ":" )
Erlite marked this conversation as resolved.
Show resolved Hide resolved
if ( index == null )
{
continue
}

expect int( index )

string name = strip( header.slice( 0, index ) )
string value = strip( header.slice( index + 1 ) )

if ( name in response.headers )
{
response.headers[ name ].append( value )
}
else
{
response.headers[ name ] <- [ value ]
}
}

pendingCallbacks[ handle ].onSuccess( response )
}

delete pendingCallbacks[ handle ]
}

/**
* Called from native when a HTTP request has failed.
* This is internal and shouldn't be used.
* @param handle The handle of the request that failed.
* @param errorCode The error code returned by curl.
* @param errorMessage The error message returned by curl.
*/
void function NSHandleFailedHttpRequest( int handle, int errorCode, string errorMessage )
{
if ( handle in pendingCallbacks )
{
if ( pendingCallbacks[ handle ].onFailure != null )
{
HttpRequestFailure failure
failure.errorCode = errorCode
failure.errorMessage = errorMessage

pendingCallbacks[ handle ].onFailure( failure )
}

delete pendingCallbacks[ handle ]
}
}

/**
* Launch a HTTP request with the given request data.
* This function is async, and the provided callbacks will be called when it is completed.
* @param requestParameters The parameters to use for this request.
* @param onSuccess The callback to execute if the request is successful.
* @param onFailure The callback to execute if the request has failed.
* @returns Whether or not the request has been successfully started.
*/
bool function NSHttpRequest( HttpRequest requestParameters, void functionref( HttpRequestResponse ) onSuccess = null, void functionref( HttpRequestFailure ) onFailure = null )
{
int handle = NS_InternalMakeHttpRequest( requestParameters.method, requestParameters.url, requestParameters.headers,
requestParameters.queryParameters, requestParameters.contentType, requestParameters.body, requestParameters.timeout, requestParameters.userAgent )

if ( handle != -1 && ( onSuccess != null || onFailure != null ) )
{
HttpRequestCallbacks callback
callback.onSuccess = onSuccess
callback.onFailure = onFailure

pendingCallbacks[ handle ] <- callback
}

return handle != -1
}

/**
* Launches an HTTP GET request at the specified URL with the given query parameters.
* This function is async, and the provided callbacks will be called when it is completed.
* @param url The url to make the http request for.
* @param queryParameters A table of key value parameters to insert in the url.
* @param onSuccess The callback to execute if the request is successful.
* @param onFailure The callback to execute if the request has failed.
* @returns Whether or not the request has been successfully started.
*/
bool function NSHttpGet( string url, table< string, array<string > > queryParameters = {}, void functionref( HttpRequestResponse ) onSuccess = null, void functionref( HttpRequestFailure ) onFailure = null )
Erlite marked this conversation as resolved.
Show resolved Hide resolved
{
HttpRequest request
request.method = HttpRequestMethod.GET
request.url = url
request.queryParameters = queryParameters

return NSHttpRequest( request, onSuccess, onFailure )
}

/**
* Launches an HTTP POST request at the specified URL with the given query parameters.
* This function is async, and the provided callbacks will be called when it is completed.
* @param url The url to make the http request for.
* @param queryParameters A table of key value parameters to insert in the url.
* @param onSuccess The callback to execute if the request is successful.
* @param onFailure The callback to execute if the request has failed.
* @returns Whether or not the request has been successfully started.
*/
bool function NSHttpPostQuery( string url, table< string, array<string > > queryParameters, void functionref( HttpRequestResponse ) onSuccess = null, void functionref( HttpRequestFailure ) onFailure = null )
Erlite marked this conversation as resolved.
Show resolved Hide resolved
{
HttpRequest request
request.method = HttpRequestMethod.POST
request.url = url
request.queryParameters = queryParameters

return NSHttpRequest( request, onSuccess, onFailure )
}

/**
* Launches an HTTP POST request at the specified URL with the given body.
* This function is async, and the provided callbacks will be called when it is completed.
* @param url The url to make the http request for.
* @param queryParameters A table of key value parameters to insert in the url.
* @param onSuccess The callback to execute if the request is successful.
* @param onFailure The callback to execute if the request has failed.
* @returns Whether or not the request has been successfully started.
*/
bool function NSHttpPostBody( string url, string body, void functionref( HttpRequestResponse ) onSuccess = null, void functionref( HttpRequestFailure ) onFailure = null )
{
HttpRequest request
request.method = HttpRequestMethod.POST
request.url = url
request.body = body

return NSHttpRequest( request, onSuccess, onFailure )
}

/** Whether or not the given status code is considered successful. */
bool function NSIsSuccessHttpCode( int statusCode )
{
return statusCode >= 200 && statusCode <= 299
}