|
| 1 | +class_name RivetApi |
| 2 | +const RivetRequest = preload("rivet_request.gd") |
| 3 | + |
| 4 | +static var CONFIGURATION_CACHE |
| 5 | + |
| 6 | +static func _get_configuration(): |
| 7 | + if CONFIGURATION_CACHE: |
| 8 | + return CONFIGURATION_CACHE |
| 9 | + |
| 10 | + if FileAccess.file_exists(RivetPluginBridge.RIVET_CONFIGURATION_FILE_PATH): |
| 11 | + var config_file = ResourceLoader.load(RivetPluginBridge.RIVET_CONFIGURATION_FILE_PATH) |
| 12 | + if config_file and 'new' in config_file: |
| 13 | + CONFIGURATION_CACHE = config_file.new() |
| 14 | + return CONFIGURATION_CACHE |
| 15 | + |
| 16 | + if FileAccess.file_exists(RivetPluginBridge.RIVET_DEPLOYED_CONFIGURATION_FILE_PATH): |
| 17 | + var deployed_config_file = ResourceLoader.load(RivetPluginBridge.RIVET_DEPLOYED_CONFIGURATION_FILE_PATH) |
| 18 | + if deployed_config_file and 'new' in deployed_config_file: |
| 19 | + CONFIGURATION_CACHE = deployed_config_file.new() |
| 20 | + return CONFIGURATION_CACHE |
| 21 | + |
| 22 | + push_warning("Rivet configuration file not found") |
| 23 | + CONFIGURATION_CACHE = null |
| 24 | + return CONFIGURATION_CACHE |
| 25 | + |
| 26 | +static func _get_api_url(): |
| 27 | + # Use plugin config if available |
| 28 | + var plugin = RivetPluginBridge.get_plugin() |
| 29 | + if plugin: |
| 30 | + return plugin.api_endpoint |
| 31 | + |
| 32 | + # Override shipped configuration endpoint |
| 33 | + var url_env = OS.get_environment("RIVET_API_ENDPOINT") |
| 34 | + if url_env: |
| 35 | + return url_env |
| 36 | + |
| 37 | + # Use configuration shipped with game |
| 38 | + var config = _get_configuration() |
| 39 | + if config: |
| 40 | + return config.api_endpoint |
| 41 | + |
| 42 | + # Fallback |
| 43 | + return "https://api.rivet.gg" |
| 44 | + |
| 45 | +## Get authorization token used from within only the plugin for cloud-specific |
| 46 | +## actions. |
| 47 | +static func _get_cloud_token(): |
| 48 | + # Use plugin config if available |
| 49 | + var plugin = RivetPluginBridge.get_plugin() |
| 50 | + if plugin: |
| 51 | + return plugin.cloud_token |
| 52 | + |
| 53 | + OS.crash("Rivet cloud token not found, this should only be called within the plugin") |
| 54 | + |
| 55 | +## Get authorization token used for making requests from within the game. |
| 56 | +## |
| 57 | +## The priority of tokens is: |
| 58 | +## |
| 59 | +## - If in editor, use the plugin token |
| 60 | +## - If provided by environment, then use that (allows for testing) |
| 61 | +## - Assume config is provided by the game client |
| 62 | +static func _get_runtime_token(): |
| 63 | + # Use plugin config if available |
| 64 | + var plugin = RivetPluginBridge.get_plugin() |
| 65 | + if plugin: |
| 66 | + return plugin.namespace_token |
| 67 | + |
| 68 | + # Use configuration shipped with game |
| 69 | + var token_env = OS.get_environment("RIVET_TOKEN") |
| 70 | + if token_env: |
| 71 | + return token_env |
| 72 | + |
| 73 | + # Use configuration shipped with game |
| 74 | + var config = _get_configuration() |
| 75 | + if config: |
| 76 | + return config.namespace_token |
| 77 | + |
| 78 | + OS.crash("Rivet token not found, validate a config is shipped with the game in the .rivet folder") |
| 79 | + |
| 80 | +## Builds the headers for a request, including the authorization token |
| 81 | +static func _build_headers(service: String) -> PackedStringArray: |
| 82 | + var token = _get_cloud_token() if service == "cloud" else _get_runtime_token() |
| 83 | + return [ |
| 84 | + "Authorization: Bearer " + token, |
| 85 | + ] |
| 86 | + |
| 87 | +## Builds a URL to Rivet cloud services |
| 88 | +static func _build_url(path: String, service: String) -> String: |
| 89 | + var path_segments := path.split("/", false) |
| 90 | + path_segments.remove_at(0) |
| 91 | + return _get_api_url() + "/%s/%s" % [service, "/".join(path_segments)] |
| 92 | + |
| 93 | +## Gets service name from a path (e.g. /users/123 -> users) |
| 94 | +static func _get_service_from_path(path: String) -> String: |
| 95 | + var path_segments := path.split("/", false) |
| 96 | + return path_segments[0] |
| 97 | + |
| 98 | +## Creates a POST request to Rivet cloud services |
| 99 | +## @experimental |
| 100 | +static func POST(owner: Node, path: String, body: Dictionary) -> RivetRequest: |
| 101 | + var service := _get_service_from_path(path) |
| 102 | + var url := _build_url(path, service) |
| 103 | + var body_json := JSON.stringify(body) |
| 104 | + |
| 105 | + return RivetRequest.new(owner, HTTPClient.METHOD_POST, url, { |
| 106 | + "headers": _build_headers(service), |
| 107 | + "body": body_json |
| 108 | + }) |
| 109 | + |
| 110 | +## Creates a GET request to Rivet cloud services |
| 111 | +## @experimental |
| 112 | +static func GET(owner: Node, path: String, body: Dictionary) -> RivetRequest: |
| 113 | + var service := _get_service_from_path(path) |
| 114 | + var url := _build_url(path, service) |
| 115 | + var body_json := JSON.stringify(body) |
| 116 | + |
| 117 | + return RivetRequest.new(owner, HTTPClient.METHOD_GET, url, { |
| 118 | + "headers": _build_headers(service), |
| 119 | + "body": body_json |
| 120 | + }) |
| 121 | + |
| 122 | +## Creates a PUT request to Rivet cloud services |
| 123 | +## @experimental |
| 124 | +static func PUT(owner: Node, path: String, body: Dictionary) -> RivetRequest: |
| 125 | + var service := _get_service_from_path(path) |
| 126 | + var url := _build_url(path, service) |
| 127 | + var body_json := JSON.stringify(body) |
| 128 | + |
| 129 | + return RivetRequest.new(owner, HTTPClient.METHOD_PUT, url, { |
| 130 | + "headers": _build_headers(service), |
| 131 | + "body": body_json |
| 132 | + }) |
0 commit comments