Skip to content
Manolo Cantón edited this page Sep 6, 2024 · 52 revisions

The wiki always refers to the latest code version on GitHub.

Introduction

Build your API requests using resources!

There are two main resources to create:

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 as Dictionary. It supports null, bool, int, float, String and Array values. Do not use nested Array.
  • SceneManagerRequest.start() method has an optional param to set listeners (complete, success, failure).

Project Tips

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 your HTTPManagerClient to store data from requests. More info here.
    • routes subfolder that contains all the HTTPManagerRoutes for this client. You can create subfolders with api.gd that extends HTTPManagerAPIGroup to split routes. So you add var posts := preload("path/to/api.gd").new() in your main api.gd and use it with _api.posts.search("query").start(func(response): print(response.parse())).
    • api.gd. See API.

Body

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.

Listeners

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,
})
Clone this wiki locally