-
-
Couldn't load subscription status.
- Fork 3
Home
Manolo Cantón edited this page Aug 16, 2024
·
52 revisions
Build your API requests using resources!
There are two main resources to create:
- SceneManagerClient: One for each host (domain, IP, different ports).
-
SceneManagerRoute: One for each client URI (
/postswithGETmethod,/posts/{id}withPOSTmethod).
When you have these resources, you can start to request using the following code:
const A_ROUTE := preload("res://path/to/route.tres") # SceneManagerRoute resource
func _start_request() -> void:
A_ROUTE.create_request({
a_url_param = "value"
}).start([func(response): print(response.parse())])-
HTTPManagerRoute.create_request()param are the url params asDictionary. It supportsnull,bool,int,float,StringandArrayvalues. Do not use nestedArray. -
SceneManagerRequest.start()method has an optional param to set listeners (complete, success, failure).
I recommend following these guidelines:
- Do not edit the
addons/http_managerfolder. Things will disappear when you want to update the plugin. - Add a
httpfolder in your project. For each different API, create a subfolder where you add:-
HTTPManagerClientresource. Sometimes you want to store multiple clients due to API versions:/v1,/v2 -
routessubfolder that contains all theHTTPManagerRoutes for this client.
-
You can set a body using HTTPManagerRequest.set_body(). You can add it with HTTPManagerRoute.create_request() with the optional params.
request.set_body({
a_body_param = "hello",
other_param = 3,
}, MIME.Type.URL_ENCODED)
ROUTE_RESOURCE.create_request({}, {
key = "value",
}, MIME.Type.JSON, {
charset = "utf-8",
}) # Adds "Content-Type: application/json; charset=utf-8" as headerSceneManagerRequest.start() has an optional argument to pass listeners (callables with HTTPManagerResponse argument) for complete, success and failure.
# On complete. Handle success with 'HTTPManagerResponse.successful'.
request.start(_on_complete)
# Success and failure independents.
request.start([_on_success, _on_failure])
# Use a Dictionary instead.
request.start({
HTTPManagerRequest.Listener.COMPLETE: _on_complete,
HTTPManagerRequest.Listener.SUCCESS: _on_success,
HTTPManagerRequest.Listener.FAILURE: _on_failure,
})