Skip to content
Manolo Cantón edited this page Aug 16, 2024 · 52 revisions

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:

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 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
    • routes subfolder that contains all the HTTPManagerRoutes for this client.

Body

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 header

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