-
-
Notifications
You must be signed in to change notification settings - Fork 1
Home
Manolo Cantón edited this page Sep 6, 2024
·
52 revisions
The wiki always refers to the latest code version on GitHub.
Build your API requests using resources!
There are two main resources to create:
- SceneManagerClient: One for each base url.
-
SceneManagerRoute: One for each client URI (
/posts
withGET
method,/posts/{id}
withPOST
method).
When you have these resources, you can start to request using the following code:
func _start_request() -> void:
var route: SceneManagerRoute = load("res://path/to/route.tres")
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
,String
andArray
values. 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_manager
folder. Things will disappear when you want to update the plugin. - Add a
http
folder in your project. For each different API, create a subfolder where you add:-
HTTPManagerClient
resource. Sometimes you want to store multiple clients due to API versions:/v1
,/v2
. Also OAuth 2.0 routes can have different base URLs. -
HTTPManagerClientData
resource. Set this resource in yourHTTPManagerClient
to store data from requests. More info here. -
routes
subfolder that contains all theHTTPManagerRoute
s for this client. You can create subfolders withapi.gd
that extendsHTTPManagerAPIGroup
to split routes. So you addvar posts := preload("path/to/api.gd").new()
in your mainapi.gd
and use it with_api.posts.search("query").start(func(response): print(response.parse()))
. -
api.gd
. See API.
-
You can set a body using HTTPManagerRequest.set_body()
. Uses the second and third params to add a MIME type to parse and set Content-Type header.
request.set_body({
a_body_param = "hello",
other_param = 3,
}, MIME.Type.URL_ENCODED)
route.create_request().set_body({
key = "value",
}, MIME.Type.JSON, {
charset = "utf-8",
}) # Adds "Content-Type: application/json; charset=utf-8" as header
Also, there are set_json_body()
and set_urlencoded_body()
methods which add the corresponding MIME type.
SceneManagerRequest.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,
})