Skip to content

Commit

Permalink
feat(standard): updated v1_network_http_get standard
Browse files Browse the repository at this point in the history
Fixes #2

Breaks non-experimental integration scripts compatibility
  • Loading branch information
krypt0nn committed Jan 20, 2024
1 parent e83ce74 commit 64a6693
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
45 changes: 36 additions & 9 deletions repository/integrations/V1_SPECIFICATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

## Built-in APIs

| API | Methods | Description |
| - | - | - |
| Network | | Work with the network |
| | `v1_network_http_get(uri)` | Perform GET request to the given URI |
| JSON | | Work with JSON |
| | `v1_json_decode(json)` | Decode JSON string |
| API | Method | Output | Description |
| - | - | - | - |
| Network | | | Work with the network |
| | `v1_network_http_get(uri)` | `Response` | Perform GET request to the given URI |
| JSON | | | Work with JSON |
| | `v1_json_decode(json)` | `table` | Decode JSON string |

## Required APIs (should be implemented by the maintainer)

Expand Down Expand Up @@ -40,22 +40,49 @@

| API | Method | Output | Description |
| - | - | - | - |
| Visual | | | |
| Visual | | | Visual representation of the game in the launcher |
| | `v1_visual_get_details_background_css(edition)` | `string` | Get CSS styles for game details page background |
| Hooks | | | |
| Hooks | | | Launcher actions in different scenarios |
| | `v1_game_diff_pre_transition(game_path, edition)` | | Process game files before creating transition |
| | `v1_game_diff_transition(transition_path, edition)` | | Process game diff files before finishing transition |
| | `v1_game_diff_post_transition(game_path, edition)` | | Process game diff files after finishing transition |
| | `v1_addons_diff_pre_transition(group_name, addon_name, addon_path, edition)` | | Process addons files before creating transition |
| | `v1_addons_diff_transition(group_name, addon_name, transition_path, edition)` | | Process addons diff files before finishing transition |
| | `v1_addons_diff_post_transition(group_name, addon_name, addon_path, edition)` | | Process addons diff files after finishing transition |
| Integrity | | | |
| Integrity | | | Verification of game or addons files |
| | `v1_integrity_hash(algorithm, data)` | `string` | Hash input data |

## Types

For syntax highlighting types definition is written on typescript

### Response

```ts
type Response = {
// Requested URL
url: string,

// Response status code
status: number,

// Response status text (error reason)
statusText: string,

// A boolean indicating whether the response was successful (status in the range 200 – 299) or not
ok: boolean,

// Response headers
headers: [key: string]: string,

// Raw response output (in bytes) encoded as a lua string
body: string,

// A lambda function which will try to decode the body as json
json: () => object
};
```

### Edition

```ts
Expand Down
24 changes: 23 additions & 1 deletion src/games/integrations/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,29 @@ impl Driver {
lua.globals().set("v1_network_http_get", lua.create_function(|lua, uri: String| {
anime_game_core::network::minreq::get(uri)
.send()
.map(|result| lua.create_string(result.as_bytes()))
.map(|response| {
let result = lua.create_table()?;
let headers = lua.create_table()?;

for (key, value) in &response.headers {
headers.set(key.as_str(), value.as_str())?;
}

result.set("url", response.url.as_str())?;
result.set("status", response.status_code)?;
result.set("statusText", response.reason_phrase.as_str())?;
result.set("ok", (200..300).contains(&response.status_code))?;
result.set("headers", headers)?;
result.set("body", lua.create_string(response.as_bytes())?)?;

result.set("json", lua.create_function(move |lua, _: ()| {
response.json::<Json>()
.map(|value| lua.to_value(&value))
.map_err(LuaError::external)
})?)?;

Ok::<LuaTable<'_>, mlua::Error>(result)
})
.map_err(LuaError::external)
})?)?;

Expand Down

0 comments on commit 64a6693

Please sign in to comment.