From dd1983fa37cb420a4933c3e4f8a0a470f5ec0f58 Mon Sep 17 00:00:00 2001 From: Erlite Date: Sun, 27 Nov 2022 20:56:59 +0100 Subject: [PATCH 01/19] Squirrel side of HTTP requests --- .../vscripts/cl_northstar_http_requests.nut | 158 ++++++++++++++++++ .../vscripts/northstar_http_requests.nut | 158 ++++++++++++++++++ 2 files changed, 316 insertions(+) create mode 100644 Northstar.Client/mod/scripts/vscripts/cl_northstar_http_requests.nut create mode 100644 Northstar.CustomServers/mod/scripts/vscripts/northstar_http_requests.nut diff --git a/Northstar.Client/mod/scripts/vscripts/cl_northstar_http_requests.nut b/Northstar.Client/mod/scripts/vscripts/cl_northstar_http_requests.nut new file mode 100644 index 000000000..b113eb9f2 --- /dev/null +++ b/Northstar.Client/mod/scripts/vscripts/cl_northstar_http_requests.nut @@ -0,0 +1,158 @@ +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 baseUrl + /** Headers used for this http request. Some may get overridden or ignored. */ + table headers + /** Query parameters for this http request. */ + table 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 +} + +struct HttpRequestCallbacks +{ + void functionref( int, string, string ) onSuccess + void functionref( int, string ) onFailure +} + +table pendingCallbacks + +/** + * 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 && pendingCallbacks[handle].onSuccess != null) + { + pendingCallbacks[handle].onSuccess(statusCode, body, headers) + } + + 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 && pendingCallbacks[handle].onFailure != null) + { + pendingCallbacks[handle].onFailure(errorCode, errorMessage) + } + + 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. + */ +void function NSHttpRequest(HttpRequest requestParameters, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null) +{ + int handle = NS_InternalMakeHttpRequest(requestParameters.method, requestParameters.baseUrl, 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 + } +} + +/** + * 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. + */ +void function NSHttpGet(string url, table queryParameters = {}, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null) +{ + HttpRequest request = { ... } + request.method = HttpRequestMethod.GET + request.baseUrl = url + request.queryParameters = queryParameters + + NSHttpRequest(request, NSBigSuccess, 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. + */ +void function NSHttpPostQuery(string url, table queryParameters, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null) +{ + HttpRequest request = { ... } + request.method = HttpRequestMethod.POST + request.baseUrl = url + request.queryParameters = queryParameters + + 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. + */ +void function NSHttpPostBody(string url, string body, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null) +{ + HttpRequest request = { ... } + request.method = HttpRequestMethod.POST + request.baseUrl = url + request.body = body + + NSHttpRequest(request, onSuccess, onFailure) +} + +/** Whether or not the given status code is considered successful. */ +bool function NSIsSuccessHttpCode(int statusCode) +{ + return statusCode >= 200 && statusCode <= 299 +} \ No newline at end of file diff --git a/Northstar.CustomServers/mod/scripts/vscripts/northstar_http_requests.nut b/Northstar.CustomServers/mod/scripts/vscripts/northstar_http_requests.nut new file mode 100644 index 000000000..3c471fcba --- /dev/null +++ b/Northstar.CustomServers/mod/scripts/vscripts/northstar_http_requests.nut @@ -0,0 +1,158 @@ +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 baseUrl + /** Headers used for this http request. Some may get overridden or ignored. */ + table headers + /** Query parameters for this http request. */ + table 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 +} + +struct HttpRequestCallbacks +{ + void functionref( int, string, string ) onSuccess + void functionref( int, string ) onFailure +} + +table pendingCallbacks + +/** + * 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 && pendingCallbacks[handle].onSuccess != null) + { + pendingCallbacks[handle].onSuccess(statusCode, body, headers) + } + + 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 && pendingCallbacks[handle].onFailure != null) + { + pendingCallbacks[handle].onFailure(errorCode, errorMessage) + } + + 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. + */ +void function NSHttpRequest(HttpRequest requestParameters, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null) +{ + int handle = NS_InternalMakeHttpRequest(requestParameters.method, requestParameters.baseUrl, 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 + } +} + +/** + * 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. + */ +void function NSHttpGet(string url, table queryParameters = {}, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null) +{ + HttpRequest request = { ... } + request.method = HttpRequestMethod.GET + request.baseUrl = url + request.queryParameters = queryParameters + + 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. + */ +void function NSHttpPostQuery(string url, table queryParameters, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null) +{ + HttpRequest request = { ... } + request.method = HttpRequestMethod.POST + request.baseUrl = url + request.queryParameters = queryParameters + + 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. + */ +void function NSHttpPostBody(string url, string body, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null) +{ + HttpRequest request = { ... } + request.method = HttpRequestMethod.POST + request.baseUrl = url + request.body = body + + NSHttpRequest(request, onSuccess, onFailure) +} + +/** Whether or not the given status code is considered successful. */ +bool function NSIsSuccessHttpCode(int statusCode) +{ + return statusCode >= 200 && statusCode <= 299 +} \ No newline at end of file From 43f6decf36f1a7a2bd6f6e60f2809c6feb81311e Mon Sep 17 00:00:00 2001 From: Erlite Date: Sun, 27 Nov 2022 21:02:40 +0100 Subject: [PATCH 02/19] Move to Northstar.Custom --- .../vscripts/cl_northstar_http_requests.nut | 158 ------------------ .../vscripts/northstar_http_requests.gnut | 8 +- 2 files changed, 4 insertions(+), 162 deletions(-) delete mode 100644 Northstar.Client/mod/scripts/vscripts/cl_northstar_http_requests.nut rename Northstar.CustomServers/mod/scripts/vscripts/northstar_http_requests.nut => Northstar.Custom/mod/scripts/vscripts/northstar_http_requests.gnut (92%) diff --git a/Northstar.Client/mod/scripts/vscripts/cl_northstar_http_requests.nut b/Northstar.Client/mod/scripts/vscripts/cl_northstar_http_requests.nut deleted file mode 100644 index b113eb9f2..000000000 --- a/Northstar.Client/mod/scripts/vscripts/cl_northstar_http_requests.nut +++ /dev/null @@ -1,158 +0,0 @@ -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 baseUrl - /** Headers used for this http request. Some may get overridden or ignored. */ - table headers - /** Query parameters for this http request. */ - table 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 -} - -struct HttpRequestCallbacks -{ - void functionref( int, string, string ) onSuccess - void functionref( int, string ) onFailure -} - -table pendingCallbacks - -/** - * 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 && pendingCallbacks[handle].onSuccess != null) - { - pendingCallbacks[handle].onSuccess(statusCode, body, headers) - } - - 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 && pendingCallbacks[handle].onFailure != null) - { - pendingCallbacks[handle].onFailure(errorCode, errorMessage) - } - - 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. - */ -void function NSHttpRequest(HttpRequest requestParameters, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null) -{ - int handle = NS_InternalMakeHttpRequest(requestParameters.method, requestParameters.baseUrl, 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 - } -} - -/** - * 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. - */ -void function NSHttpGet(string url, table queryParameters = {}, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null) -{ - HttpRequest request = { ... } - request.method = HttpRequestMethod.GET - request.baseUrl = url - request.queryParameters = queryParameters - - NSHttpRequest(request, NSBigSuccess, 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. - */ -void function NSHttpPostQuery(string url, table queryParameters, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null) -{ - HttpRequest request = { ... } - request.method = HttpRequestMethod.POST - request.baseUrl = url - request.queryParameters = queryParameters - - 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. - */ -void function NSHttpPostBody(string url, string body, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null) -{ - HttpRequest request = { ... } - request.method = HttpRequestMethod.POST - request.baseUrl = url - request.body = body - - NSHttpRequest(request, onSuccess, onFailure) -} - -/** Whether or not the given status code is considered successful. */ -bool function NSIsSuccessHttpCode(int statusCode) -{ - return statusCode >= 200 && statusCode <= 299 -} \ No newline at end of file diff --git a/Northstar.CustomServers/mod/scripts/vscripts/northstar_http_requests.nut b/Northstar.Custom/mod/scripts/vscripts/northstar_http_requests.gnut similarity index 92% rename from Northstar.CustomServers/mod/scripts/vscripts/northstar_http_requests.nut rename to Northstar.Custom/mod/scripts/vscripts/northstar_http_requests.gnut index 3c471fcba..aa4bf8188 100644 --- a/Northstar.CustomServers/mod/scripts/vscripts/northstar_http_requests.nut +++ b/Northstar.Custom/mod/scripts/vscripts/northstar_http_requests.gnut @@ -18,9 +18,9 @@ global struct HttpRequest /** Base URL of this http request. */ string baseUrl /** Headers used for this http request. Some may get overridden or ignored. */ - table headers + table > headers /** Query parameters for this http request. */ - table queryParameters + table > 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.*/ @@ -105,7 +105,7 @@ void function NSHttpRequest(HttpRequest requestParameters, void functionref( int * @param onSuccess The callback to execute if the request is successful. * @param onFailure The callback to execute if the request has failed. */ -void function NSHttpGet(string url, table queryParameters = {}, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null) +void function NSHttpGet(string url, table > queryParameters = {}, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null) { HttpRequest request = { ... } request.method = HttpRequestMethod.GET @@ -123,7 +123,7 @@ void function NSHttpGet(string url, table queryParameters = {}, * @param onSuccess The callback to execute if the request is successful. * @param onFailure The callback to execute if the request has failed. */ -void function NSHttpPostQuery(string url, table queryParameters, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null) +void function NSHttpPostQuery(string url, table > queryParameters, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null) { HttpRequest request = { ... } request.method = HttpRequestMethod.POST From 415f29aafd4c6b125c909859653b899b2f767b7f Mon Sep 17 00:00:00 2001 From: Erlite Date: Sun, 27 Nov 2022 21:51:03 +0100 Subject: [PATCH 03/19] Working HTTP requests from Squirrel --- Northstar.Custom/mod.json | 4 ++++ ...tar_http_requests.gnut => sh_northstar_http_requests.gnut} | 0 2 files changed, 4 insertions(+) rename Northstar.Custom/mod/scripts/vscripts/{northstar_http_requests.gnut => sh_northstar_http_requests.gnut} (100%) diff --git a/Northstar.Custom/mod.json b/Northstar.Custom/mod.json index 1a08f0d48..30b7eb25f 100644 --- a/Northstar.Custom/mod.json +++ b/Northstar.Custom/mod.json @@ -417,6 +417,10 @@ "ServerCallback": { "Before": "MessageUtils_ServerInit" } + }, + { + "Path": "sh_northstar_http_requests.gnut", + "RunOn": "CLIENT || SERVER || UI " } ], diff --git a/Northstar.Custom/mod/scripts/vscripts/northstar_http_requests.gnut b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut similarity index 100% rename from Northstar.Custom/mod/scripts/vscripts/northstar_http_requests.gnut rename to Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut From dcea336e5499a6dbddb05e09a6819a4d11bab99a Mon Sep 17 00:00:00 2001 From: Erlite Date: Tue, 29 Nov 2022 19:42:28 +0100 Subject: [PATCH 04/19] Fix overlooked delete, add comments --- .../vscripts/sh_northstar_http_requests.gnut | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut index aa4bf8188..ae0d2c952 100644 --- a/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut +++ b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut @@ -33,7 +33,16 @@ global struct HttpRequest struct HttpRequestCallbacks { + /** + * The function to call if the HTTP request was a success. + * int statusCode, string body, string headers + */ void functionref( int, string, string ) onSuccess + + /** + * The function to call if the HTTP request failed. + * int statusCode, string errorMessage. + */ void functionref( int, string ) onFailure } @@ -50,12 +59,15 @@ table pendingCallbacks */ void function NSHandleSuccessfulHttpRequest(int handle, int statusCode, string body, string headers) { - if (handle in pendingCallbacks && pendingCallbacks[handle].onSuccess != null) + if (handle in pendingCallbacks) { - pendingCallbacks[handle].onSuccess(statusCode, body, headers) - } + if (pendingCallbacks[handle].onSuccess != null) + { + pendingCallbacks[handle].onSuccess(statusCode, body, headers) + } - delete pendingCallbacks[handle] + delete pendingCallbacks[handle] + } } /** @@ -67,12 +79,15 @@ void function NSHandleSuccessfulHttpRequest(int handle, int statusCode, string b */ void function NSHandleFailedHttpRequest(int handle, int errorCode, string errorMessage) { - if (handle in pendingCallbacks && pendingCallbacks[handle].onFailure != null) + if (handle in pendingCallbacks) { - pendingCallbacks[handle].onFailure(errorCode, errorMessage) - } + if (pendingCallbacks[handle].onFailure != null) + { + pendingCallbacks[handle].onFailure(errorCode, errorMessage) + } - delete pendingCallbacks[handle] + delete pendingCallbacks[handle] + } } /** From ff06973bec632178b9ad5211c570f0eec7469f77 Mon Sep 17 00:00:00 2001 From: Erlite Date: Thu, 1 Dec 2022 23:24:00 +0100 Subject: [PATCH 05/19] Add request success bool as return in req funcs. --- .../vscripts/sh_northstar_http_requests.gnut | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut index ae0d2c952..6a2e387ae 100644 --- a/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut +++ b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut @@ -96,8 +96,9 @@ void function NSHandleFailedHttpRequest(int handle, int errorCode, string errorM * @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. */ -void function NSHttpRequest(HttpRequest requestParameters, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null) +bool function NSHttpRequest(HttpRequest requestParameters, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null) { int handle = NS_InternalMakeHttpRequest(requestParameters.method, requestParameters.baseUrl, requestParameters.headers, requestParameters.queryParameters, requestParameters.contentType, requestParameters.body, requestParameters.timeout, requestParameters.userAgent) @@ -110,6 +111,8 @@ void function NSHttpRequest(HttpRequest requestParameters, void functionref( int pendingCallbacks[handle] <- callback } + + return handle != -1 } /** @@ -119,15 +122,16 @@ void function NSHttpRequest(HttpRequest requestParameters, void functionref( int * @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. */ -void function NSHttpGet(string url, table > queryParameters = {}, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null) +bool function NSHttpGet(string url, table > queryParameters = {}, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null) { HttpRequest request = { ... } request.method = HttpRequestMethod.GET request.baseUrl = url request.queryParameters = queryParameters - NSHttpRequest(request, onSuccess, onFailure) + return NSHttpRequest(request, onSuccess, onFailure) } /** @@ -137,15 +141,16 @@ void function NSHttpGet(string url, table > queryParameter * @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. */ -void function NSHttpPostQuery(string url, table > queryParameters, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null) +bool function NSHttpPostQuery(string url, table > queryParameters, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null) { HttpRequest request = { ... } request.method = HttpRequestMethod.POST request.baseUrl = url request.queryParameters = queryParameters - NSHttpRequest(request, onSuccess, onFailure) + return NSHttpRequest(request, onSuccess, onFailure) } /** @@ -155,15 +160,16 @@ void function NSHttpPostQuery(string url, table > queryPar * @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. */ -void function NSHttpPostBody(string url, string body, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null) +bool function NSHttpPostBody(string url, string body, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null) { HttpRequest request = { ... } request.method = HttpRequestMethod.POST request.baseUrl = url request.body = body - NSHttpRequest(request, onSuccess, onFailure) + return NSHttpRequest(request, onSuccess, onFailure) } /** Whether or not the given status code is considered successful. */ From c8b60a3d745eb8163fa33443006b2a0d8fef5c01 Mon Sep 17 00:00:00 2001 From: Erlite Date: Fri, 2 Dec 2022 15:10:19 +0100 Subject: [PATCH 06/19] Formatting issues. --- .../vscripts/sh_northstar_http_requests.gnut | 54 +++++++++---------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut index 6a2e387ae..527c6fea3 100644 --- a/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut +++ b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut @@ -41,12 +41,12 @@ struct HttpRequestCallbacks /** * The function to call if the HTTP request failed. - * int statusCode, string errorMessage. + * int errorCode, string errorMessage. */ void functionref( int, string ) onFailure } -table pendingCallbacks +table< int, HttpRequestCallbacks > pendingCallbacks /** * Called from native when a HTTP request is successful. @@ -57,16 +57,16 @@ table pendingCallbacks * @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) +void function NSHandleSuccessfulHttpRequest( int handle, int statusCode, string body, string headers ) { - if (handle in pendingCallbacks) + if ( handle in pendingCallbacks ) { - if (pendingCallbacks[handle].onSuccess != null) + if ( pendingCallbacks[ handle ].onSuccess != null ) { - pendingCallbacks[handle].onSuccess(statusCode, body, headers) + pendingCallbacks[ handle ].onSuccess( statusCode, body, headers ) } - delete pendingCallbacks[handle] + delete pendingCallbacks[ handle ] } } @@ -77,16 +77,16 @@ void function NSHandleSuccessfulHttpRequest(int handle, int statusCode, string b * @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) +void function NSHandleFailedHttpRequest( int handle, int errorCode, string errorMessage ) { - if (handle in pendingCallbacks) + if ( handle in pendingCallbacks ) { - if (pendingCallbacks[handle].onFailure != null) + if ( pendingCallbacks[ handle ].onFailure != null ) { - pendingCallbacks[handle].onFailure(errorCode, errorMessage) + pendingCallbacks[ handle ].onFailure( errorCode, errorMessage ) } - delete pendingCallbacks[handle] + delete pendingCallbacks[ handle ] } } @@ -98,18 +98,18 @@ void function NSHandleFailedHttpRequest(int handle, int errorCode, string errorM * @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( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null) +bool function NSHttpRequest( HttpRequest requestParameters, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null ) { - int handle = NS_InternalMakeHttpRequest(requestParameters.method, requestParameters.baseUrl, requestParameters.headers, - requestParameters.queryParameters, requestParameters.contentType, requestParameters.body, requestParameters.timeout, requestParameters.userAgent) + int handle = NS_InternalMakeHttpRequest( requestParameters.method, requestParameters.baseUrl, requestParameters.headers, + requestParameters.queryParameters, requestParameters.contentType, requestParameters.body, requestParameters.timeout, requestParameters.userAgent ) - if (handle != -1 && (onSuccess != null || onFailure != null)) + if ( handle != -1 && ( onSuccess != null || onFailure != null ) ) { HttpRequestCallbacks callback callback.onSuccess = onSuccess callback.onFailure = onFailure - pendingCallbacks[handle] <- callback + pendingCallbacks[ handle ] <- callback } return handle != -1 @@ -124,14 +124,14 @@ bool function NSHttpRequest(HttpRequest requestParameters, void functionref( int * @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 > queryParameters = {}, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null) +bool function NSHttpGet( string url, table > queryParameters = {}, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null ) { - HttpRequest request = { ... } + HttpRequest request request.method = HttpRequestMethod.GET request.baseUrl = url request.queryParameters = queryParameters - return NSHttpRequest(request, onSuccess, onFailure) + return NSHttpRequest( request, onSuccess, onFailure ) } /** @@ -143,14 +143,14 @@ bool function NSHttpGet(string url, table > queryParameter * @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 > queryParameters, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null) +bool function NSHttpPostQuery( string url, table > queryParameters, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null ) { - HttpRequest request = { ... } + HttpRequest request request.method = HttpRequestMethod.POST request.baseUrl = url request.queryParameters = queryParameters - return NSHttpRequest(request, onSuccess, onFailure) + return NSHttpRequest( request, onSuccess, onFailure ) } /** @@ -162,18 +162,18 @@ bool function NSHttpPostQuery(string url, table > queryPar * @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( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null) +bool function NSHttpPostBody( string url, string body, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null ) { - HttpRequest request = { ... } + HttpRequest request request.method = HttpRequestMethod.POST request.baseUrl = url request.body = body - return NSHttpRequest(request, onSuccess, onFailure) + return NSHttpRequest( request, onSuccess, onFailure ) } /** Whether or not the given status code is considered successful. */ -bool function NSIsSuccessHttpCode(int statusCode) +bool function NSIsSuccessHttpCode( int statusCode ) { return statusCode >= 200 && statusCode <= 299 } \ No newline at end of file From 5d455953a5770dceadaeaf0fdf2e3d55928fba0a Mon Sep 17 00:00:00 2001 From: Erlite Date: Fri, 2 Dec 2022 17:37:48 +0100 Subject: [PATCH 07/19] More formatting fixes from code reviews. Co-authored-by: Jack <66967891+ASpoonPlaysGames@users.noreply.github.com> --- Northstar.Custom/mod.json | 2 +- .../mod/scripts/vscripts/sh_northstar_http_requests.gnut | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Northstar.Custom/mod.json b/Northstar.Custom/mod.json index 30b7eb25f..d47c42cfe 100644 --- a/Northstar.Custom/mod.json +++ b/Northstar.Custom/mod.json @@ -420,7 +420,7 @@ }, { "Path": "sh_northstar_http_requests.gnut", - "RunOn": "CLIENT || SERVER || UI " + "RunOn": "CLIENT || SERVER || UI" } ], diff --git a/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut index 527c6fea3..8bdff1412 100644 --- a/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut +++ b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut @@ -124,7 +124,7 @@ bool function NSHttpRequest( HttpRequest requestParameters, void functionref( in * @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 > queryParameters = {}, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null ) +bool function NSHttpGet( string url, table< string, array< string > > queryParameters = {}, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null ) { HttpRequest request request.method = HttpRequestMethod.GET @@ -143,7 +143,7 @@ bool function NSHttpGet( string url, table > queryParamete * @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 > queryParameters, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null ) +bool function NSHttpPostQuery( string url, table< string, array< string > > queryParameters, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null ) { HttpRequest request request.method = HttpRequestMethod.POST @@ -176,4 +176,4 @@ bool function NSHttpPostBody( string url, string body, void functionref( int, st bool function NSIsSuccessHttpCode( int statusCode ) { return statusCode >= 200 && statusCode <= 299 -} \ No newline at end of file +} From b918a39b30b6411138484a7a2aab2ecd1ea341ac Mon Sep 17 00:00:00 2001 From: Erlite Date: Sun, 4 Dec 2022 17:43:12 +0100 Subject: [PATCH 08/19] Success callback uses struct now. --- .../vscripts/sh_northstar_http_requests.gnut | 63 ++++++++++++++++--- 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut index 527c6fea3..c6a3acb26 100644 --- a/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut +++ b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut @@ -31,13 +31,25 @@ global struct HttpRequest 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 > headers +} + struct HttpRequestCallbacks { /** * The function to call if the HTTP request was a success. - * int statusCode, string body, string headers + * Passes in the response received from the remote. */ - void functionref( int, string, string ) onSuccess + void functionref( HttpRequestResponse ) onSuccess /** * The function to call if the HTTP request failed. @@ -59,15 +71,46 @@ table< int, HttpRequestCallbacks > pendingCallbacks */ void function NSHandleSuccessfulHttpRequest( int handle, int statusCode, string body, string headers ) { - if ( handle in pendingCallbacks ) + if ( !( handle in pendingCallbacks ) ) { - if ( pendingCallbacks[ handle ].onSuccess != null ) + 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 values = split( headers, "\n" ) + + foreach ( string header in values ) { - pendingCallbacks[ handle ].onSuccess( statusCode, body, headers ) + var index = header.find( ":" ) + if ( index == null ) + { + continue; + } + + string name = strip( header.slice( 0, expect int(index) ) ) + string value = strip( header.slice( expect int(index) + 1 ) ); + + if ( name in response.headers ) + { + response.headers[name].append(value) + } + else + { + response.headers[name] <- [ value ] + } } - delete pendingCallbacks[ handle ] + pendingCallbacks[ handle ].onSuccess( response ) } + + delete pendingCallbacks[ handle ] } /** @@ -98,7 +141,7 @@ void function NSHandleFailedHttpRequest( int handle, int errorCode, string error * @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( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null ) +bool function NSHttpRequest( HttpRequest requestParameters, void functionref( HttpRequestResponse ) onSuccess = null, void functionref( int, string ) onFailure = null ) { int handle = NS_InternalMakeHttpRequest( requestParameters.method, requestParameters.baseUrl, requestParameters.headers, requestParameters.queryParameters, requestParameters.contentType, requestParameters.body, requestParameters.timeout, requestParameters.userAgent ) @@ -124,7 +167,7 @@ bool function NSHttpRequest( HttpRequest requestParameters, void functionref( in * @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 > queryParameters = {}, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null ) +bool function NSHttpGet( string url, table > queryParameters = {}, void functionref( HttpRequestResponse ) onSuccess = null, void functionref( int, string ) onFailure = null ) { HttpRequest request request.method = HttpRequestMethod.GET @@ -143,7 +186,7 @@ bool function NSHttpGet( string url, table > queryParamete * @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 > queryParameters, void functionref( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null ) +bool function NSHttpPostQuery( string url, table > queryParameters, void functionref( HttpRequestResponse ) onSuccess = null, void functionref( int, string ) onFailure = null ) { HttpRequest request request.method = HttpRequestMethod.POST @@ -162,7 +205,7 @@ bool function NSHttpPostQuery( string url, table > queryPa * @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( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null ) +bool function NSHttpPostBody( string url, string body, void functionref( HttpRequestResponse ) onSuccess = null, void functionref( int, string ) onFailure = null ) { HttpRequest request request.method = HttpRequestMethod.POST From 890dea1b2b423c3ed37198c5ade8b31a2057879e Mon Sep 17 00:00:00 2001 From: Erlite Date: Mon, 5 Dec 2022 12:52:17 +0100 Subject: [PATCH 09/19] Never fix conflicts on mobile browsers, noted. --- .../mod/scripts/vscripts/sh_northstar_http_requests.gnut | 2 -- 1 file changed, 2 deletions(-) diff --git a/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut index 4649e8c9f..da346cd2b 100644 --- a/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut +++ b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut @@ -167,7 +167,6 @@ bool function NSHttpRequest( HttpRequest requestParameters, void functionref( Ht * @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( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null ) bool function NSHttpGet( string url, table > queryParameters = {}, void functionref( HttpRequestResponse ) onSuccess = null, void functionref( int, string ) onFailure = null ) { HttpRequest request @@ -187,7 +186,6 @@ bool function NSHttpGet( string url, table > queryParamete * @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( int, string, string ) onSuccess = null, void functionref( int, string ) onFailure = null ) bool function NSHttpPostQuery( string url, table > queryParameters, void functionref( HttpRequestResponse ) onSuccess = null, void functionref( int, string ) onFailure = null ) { HttpRequest request From f255b76f60f60871e8649febc954fc0d48e91c4d Mon Sep 17 00:00:00 2001 From: Erlite Date: Mon, 5 Dec 2022 12:53:46 +0100 Subject: [PATCH 10/19] HttpRequest.baseUrl -> url --- .../scripts/vscripts/sh_northstar_http_requests.gnut | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut index da346cd2b..c1dd0f9d5 100644 --- a/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut +++ b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut @@ -16,7 +16,7 @@ global struct HttpRequest /** Method used for this http request. */ int method /** Base URL of this http request. */ - string baseUrl + string url /** Headers used for this http request. Some may get overridden or ignored. */ table > headers /** Query parameters for this http request. */ @@ -143,7 +143,7 @@ void function NSHandleFailedHttpRequest( int handle, int errorCode, string error */ bool function NSHttpRequest( HttpRequest requestParameters, void functionref( HttpRequestResponse ) onSuccess = null, void functionref( int, string ) onFailure = null ) { - int handle = NS_InternalMakeHttpRequest( requestParameters.method, requestParameters.baseUrl, requestParameters.headers, + 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 ) ) @@ -171,7 +171,7 @@ bool function NSHttpGet( string url, table > queryParamete { HttpRequest request request.method = HttpRequestMethod.GET - request.baseUrl = url + request.url = url request.queryParameters = queryParameters return NSHttpRequest( request, onSuccess, onFailure ) @@ -190,7 +190,7 @@ bool function NSHttpPostQuery( string url, table > queryPa { HttpRequest request request.method = HttpRequestMethod.POST - request.baseUrl = url + request.url = url request.queryParameters = queryParameters return NSHttpRequest( request, onSuccess, onFailure ) @@ -209,7 +209,7 @@ bool function NSHttpPostBody( string url, string body, void functionref( HttpReq { HttpRequest request request.method = HttpRequestMethod.POST - request.baseUrl = url + request.url = url request.body = body return NSHttpRequest( request, onSuccess, onFailure ) From 85b878bff6b326b78ef6d5687abf123aa0bd343d Mon Sep 17 00:00:00 2001 From: Erlite Date: Mon, 5 Dec 2022 18:14:29 +0100 Subject: [PATCH 11/19] More formatting changes. Co-authored-by: Jack <66967891+ASpoonPlaysGames@users.noreply.github.com> --- .../scripts/vscripts/sh_northstar_http_requests.gnut | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut index c1dd0f9d5..fdb140012 100644 --- a/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut +++ b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut @@ -18,9 +18,9 @@ global struct HttpRequest /** Base URL of this http request. */ string url /** Headers used for this http request. Some may get overridden or ignored. */ - table > headers + table< string, array< string > > headers /** Query parameters for this http request. */ - table > queryParameters + 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.*/ @@ -40,7 +40,7 @@ global struct HttpRequestResponse /** The raw headers returned by the remote. */ string rawHeaders /** A key -> values table of headers returned by the remote. */ - table > headers + table< string, array< string > > headers } struct HttpRequestCallbacks @@ -167,7 +167,7 @@ bool function NSHttpRequest( HttpRequest requestParameters, void functionref( Ht * @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 > queryParameters = {}, void functionref( HttpRequestResponse ) onSuccess = null, void functionref( int, string ) onFailure = null ) +bool function NSHttpGet( string url, table< string, array< string > > queryParameters = {}, void functionref( HttpRequestResponse ) onSuccess = null, void functionref( int, string ) onFailure = null ) { HttpRequest request request.method = HttpRequestMethod.GET @@ -186,7 +186,7 @@ bool function NSHttpGet( string url, table > queryParamete * @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 > queryParameters, void functionref( HttpRequestResponse ) onSuccess = null, void functionref( int, string ) onFailure = null ) +bool function NSHttpPostQuery( string url, table< string, array< string > > queryParameters, void functionref( HttpRequestResponse ) onSuccess = null, void functionref( int, string ) onFailure = null ) { HttpRequest request request.method = HttpRequestMethod.POST From 69d02ce4f0b1411c2f8a00289d64c36d93d33f2a Mon Sep 17 00:00:00 2001 From: Erlite Date: Mon, 5 Dec 2022 21:51:50 +0100 Subject: [PATCH 12/19] Use struct for http failures --- .../vscripts/sh_northstar_http_requests.gnut | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut index c1dd0f9d5..0135b50c4 100644 --- a/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut +++ b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut @@ -43,6 +43,14 @@ global struct HttpRequestResponse table > 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 { /** @@ -53,9 +61,8 @@ struct HttpRequestCallbacks /** * The function to call if the HTTP request failed. - * int errorCode, string errorMessage. */ - void functionref( int, string ) onFailure + void functionref( HttpRequestFailure ) onFailure } table< int, HttpRequestCallbacks > pendingCallbacks @@ -126,7 +133,11 @@ void function NSHandleFailedHttpRequest( int handle, int errorCode, string error { if ( pendingCallbacks[ handle ].onFailure != null ) { - pendingCallbacks[ handle ].onFailure( errorCode, errorMessage ) + HttpRequestFailure failure + failure.errorCode = errorCode + failure.errorMessage = errorMessage + + pendingCallbacks[ handle ].onFailure( failure ) } delete pendingCallbacks[ handle ] @@ -141,7 +152,7 @@ void function NSHandleFailedHttpRequest( int handle, int errorCode, string error * @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( int, string ) onFailure = null ) +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 ) @@ -167,7 +178,7 @@ bool function NSHttpRequest( HttpRequest requestParameters, void functionref( Ht * @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 > queryParameters = {}, void functionref( HttpRequestResponse ) onSuccess = null, void functionref( int, string ) onFailure = null ) +bool function NSHttpGet( string url, table > queryParameters = {}, void functionref( HttpRequestResponse ) onSuccess = null, void functionref( HttpRequestFailure ) onFailure = null ) { HttpRequest request request.method = HttpRequestMethod.GET @@ -186,7 +197,7 @@ bool function NSHttpGet( string url, table > queryParamete * @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 > queryParameters, void functionref( HttpRequestResponse ) onSuccess = null, void functionref( int, string ) onFailure = null ) +bool function NSHttpPostQuery( string url, table > queryParameters, void functionref( HttpRequestResponse ) onSuccess = null, void functionref( HttpRequestFailure ) onFailure = null ) { HttpRequest request request.method = HttpRequestMethod.POST @@ -205,7 +216,7 @@ bool function NSHttpPostQuery( string url, table > queryPa * @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( int, string ) onFailure = null ) +bool function NSHttpPostBody( string url, string body, void functionref( HttpRequestResponse ) onSuccess = null, void functionref( HttpRequestFailure ) onFailure = null ) { HttpRequest request request.method = HttpRequestMethod.POST From 4e40a7a2fbae299ba962849bc89be28c97ec91de Mon Sep 17 00:00:00 2001 From: Erlite Date: Mon, 5 Dec 2022 22:07:03 +0100 Subject: [PATCH 13/19] Merge tool is stupid --- .../mod/scripts/vscripts/sh_northstar_http_requests.gnut | 8 -------- 1 file changed, 8 deletions(-) diff --git a/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut index 80f0fc7db..fed2dcfc6 100644 --- a/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut +++ b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut @@ -51,14 +51,6 @@ global struct HttpRequestFailure string errorMessage } -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 { /** From 9724d976eeb6493d01134d077cafe7dba6f0502c Mon Sep 17 00:00:00 2001 From: Erlite Date: Mon, 5 Dec 2022 22:09:59 +0100 Subject: [PATCH 14/19] Fix compile issues --- .../mod/scripts/vscripts/sh_northstar_http_requests.gnut | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut index fed2dcfc6..8dc861e36 100644 --- a/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut +++ b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut @@ -178,7 +178,7 @@ bool function NSHttpRequest( HttpRequest requestParameters, void functionref( Ht * @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 > queryParameters = {}, void functionref( HttpRequestResponse ) onSuccess = null, void functionref( HttpRequestResponse ) onFailure = null ) +bool function NSHttpGet( string url, table< string, array > queryParameters = {}, void functionref( HttpRequestResponse ) onSuccess = null, void functionref( HttpRequestFailure ) onFailure = null ) { HttpRequest request request.method = HttpRequestMethod.GET @@ -197,7 +197,7 @@ bool function NSHttpGet( string url, table< string, array > queryParame * @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 > queryParameters, void functionref( HttpRequestResponse ) onSuccess = null, void functionref( HttpRequestResponse ) onFailure = null ) +bool function NSHttpPostQuery( string url, table< string, array > queryParameters, void functionref( HttpRequestResponse ) onSuccess = null, void functionref( HttpRequestFailure ) onFailure = null ) { HttpRequest request request.method = HttpRequestMethod.POST From 9401815effab24102e7eac5936cbab2f2903bc4e Mon Sep 17 00:00:00 2001 From: Erlite Date: Tue, 6 Dec 2022 23:12:59 +0100 Subject: [PATCH 15/19] Remove leftover semicolons --- .../mod/scripts/vscripts/sh_northstar_http_requests.gnut | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut index 8dc861e36..735e48931 100644 --- a/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut +++ b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut @@ -98,11 +98,11 @@ void function NSHandleSuccessfulHttpRequest( int handle, int statusCode, string var index = header.find( ":" ) if ( index == null ) { - continue; + continue } string name = strip( header.slice( 0, expect int(index) ) ) - string value = strip( header.slice( expect int(index) + 1 ) ); + string value = strip( header.slice( expect int(index) + 1 ) ) if ( name in response.headers ) { From c1564c83cd7bb417d12d7e544ea0309c1e56f53d Mon Sep 17 00:00:00 2001 From: Erlite Date: Thu, 8 Dec 2022 17:04:30 +0100 Subject: [PATCH 16/19] Formatting fixes. --- .../scripts/vscripts/sh_northstar_http_requests.gnut | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut index 735e48931..74fa2c446 100644 --- a/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut +++ b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut @@ -101,16 +101,18 @@ void function NSHandleSuccessfulHttpRequest( int handle, int statusCode, string continue } - string name = strip( header.slice( 0, expect int(index) ) ) - string value = strip( header.slice( expect int(index) + 1 ) ) + 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) + response.headers[ name ].append( value ) } else { - response.headers[name] <- [ value ] + response.headers[ name ] <- [ value ] } } From 6146c4d05ea203169be2f844cda74f12b811b9aa Mon Sep 17 00:00:00 2001 From: Erlite Date: Thu, 8 Dec 2022 17:13:31 +0100 Subject: [PATCH 17/19] Formatting. Co-authored-by: Jack <66967891+ASpoonPlaysGames@users.noreply.github.com> --- .../mod/scripts/vscripts/sh_northstar_http_requests.gnut | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut index 74fa2c446..0f75d9b84 100644 --- a/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut +++ b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut @@ -180,7 +180,7 @@ bool function NSHttpRequest( HttpRequest requestParameters, void functionref( Ht * @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 > queryParameters = {}, void functionref( HttpRequestResponse ) onSuccess = null, void functionref( HttpRequestFailure ) onFailure = null ) +bool function NSHttpGet( string url, table< string, array< string > > queryParameters = {}, void functionref( HttpRequestResponse ) onSuccess = null, void functionref( HttpRequestFailure ) onFailure = null ) { HttpRequest request request.method = HttpRequestMethod.GET From f89add66da24d7640b03341a3b7fa78158501ee6 Mon Sep 17 00:00:00 2001 From: Erlite Date: Thu, 8 Dec 2022 17:16:18 +0100 Subject: [PATCH 18/19] Update Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut Co-authored-by: Jack <66967891+ASpoonPlaysGames@users.noreply.github.com> --- .../mod/scripts/vscripts/sh_northstar_http_requests.gnut | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut index 0f75d9b84..228ccf028 100644 --- a/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut +++ b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut @@ -199,7 +199,7 @@ bool function NSHttpGet( string url, table< string, array< string > > queryParam * @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 > queryParameters, void functionref( HttpRequestResponse ) onSuccess = null, void functionref( HttpRequestFailure ) onFailure = null ) +bool function NSHttpPostQuery( string url, table< string, array< string > > queryParameters, void functionref( HttpRequestResponse ) onSuccess = null, void functionref( HttpRequestFailure ) onFailure = null ) { HttpRequest request request.method = HttpRequestMethod.POST From 9c6ecbbfa38a428219372b12eb3b3667bd8db452 Mon Sep 17 00:00:00 2001 From: Erlite Date: Sat, 10 Dec 2022 17:19:53 +0100 Subject: [PATCH 19/19] Tabs not spaces --- .../vscripts/sh_northstar_http_requests.gnut | 252 +++++++++--------- 1 file changed, 126 insertions(+), 126 deletions(-) diff --git a/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut index 228ccf028..8ff55eaeb 100644 --- a/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut +++ b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_http_requests.gnut @@ -2,67 +2,67 @@ globalize_all_functions global enum HttpRequestMethod { - GET = 0, - POST = 1 - HEAD = 2, - PUT = 3, - DELETE = 4, - PATCH = 5, - OPTIONS = 6, + 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 + /** 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 + /** 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 + /** 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 + /** + * 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 @@ -78,48 +78,48 @@ table< int, HttpRequestCallbacks > pendingCallbacks */ 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 values = split( headers, "\n" ) - - foreach ( string header in values ) - { - var index = header.find( ":" ) - 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 ] + 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 values = split( headers, "\n" ) + + foreach ( string header in values ) + { + var index = header.find( ":" ) + 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 ] } /** @@ -131,19 +131,19 @@ void function NSHandleSuccessfulHttpRequest( int handle, int statusCode, string */ 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 ] - } + if ( handle in pendingCallbacks ) + { + if ( pendingCallbacks[ handle ].onFailure != null ) + { + HttpRequestFailure failure + failure.errorCode = errorCode + failure.errorMessage = errorMessage + + pendingCallbacks[ handle ].onFailure( failure ) + } + + delete pendingCallbacks[ handle ] + } } /** @@ -156,19 +156,19 @@ void function NSHandleFailedHttpRequest( int handle, int errorCode, string error */ 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 ) + 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 + if ( handle != -1 && ( onSuccess != null || onFailure != null ) ) + { + HttpRequestCallbacks callback + callback.onSuccess = onSuccess + callback.onFailure = onFailure - pendingCallbacks[ handle ] <- callback - } + pendingCallbacks[ handle ] <- callback + } - return handle != -1 + return handle != -1 } /** @@ -182,12 +182,12 @@ bool function NSHttpRequest( HttpRequest requestParameters, void functionref( Ht */ bool function NSHttpGet( string url, table< string, array< string > > queryParameters = {}, void functionref( HttpRequestResponse ) onSuccess = null, void functionref( HttpRequestFailure ) onFailure = null ) { - HttpRequest request - request.method = HttpRequestMethod.GET - request.url = url - request.queryParameters = queryParameters + HttpRequest request + request.method = HttpRequestMethod.GET + request.url = url + request.queryParameters = queryParameters - return NSHttpRequest( request, onSuccess, onFailure ) + return NSHttpRequest( request, onSuccess, onFailure ) } /** @@ -201,12 +201,12 @@ bool function NSHttpGet( string url, table< string, array< string > > queryParam */ bool function NSHttpPostQuery( string url, table< string, array< string > > queryParameters, void functionref( HttpRequestResponse ) onSuccess = null, void functionref( HttpRequestFailure ) onFailure = null ) { - HttpRequest request - request.method = HttpRequestMethod.POST - request.url = url - request.queryParameters = queryParameters + HttpRequest request + request.method = HttpRequestMethod.POST + request.url = url + request.queryParameters = queryParameters - return NSHttpRequest( request, onSuccess, onFailure ) + return NSHttpRequest( request, onSuccess, onFailure ) } /** @@ -220,16 +220,16 @@ bool function NSHttpPostQuery( string url, table< string, array< string > > quer */ 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 + HttpRequest request + request.method = HttpRequestMethod.POST + request.url = url + request.body = body - return NSHttpRequest( request, onSuccess, onFailure ) + 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 + return statusCode >= 200 && statusCode <= 299 }