-
-
Couldn't load subscription status.
- Fork 3
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 (
/postswithGETmethod,/posts/{id}withPOSTmethod).
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,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. Also OAuth 2.0 routes can have different base URLs. -
HTTPManagerClientDataresource. Set this resource in yourHTTPManagerClientto store data from requests. More info here. -
api.gd. See API. -
routessubfolder that contains all theHTTPManagerRoutes for this client. You can create subfolders withapi.gdthat extendsHTTPManagerAPIGroupto split routes. So you addvar posts := preload("path/to/api.gd").new()in your mainapi.gdand use it with_api.posts.search("query").start(func(response): print(response.parse())).
-
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 headerAlso, there are set_json_body() and set_urlencoded_body() methods.
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,
})