feat(torn-api): add chrono for datetime support

This commit is contained in:
pyrite 2025-05-27 19:27:57 +02:00
parent 266122ea0e
commit 7a4f6462f5
Signed by: pyrite
GPG key ID: 7F1BA9170CD35D15
7 changed files with 741 additions and 22 deletions

2
Cargo.lock generated
View file

@ -227,6 +227,7 @@ dependencies = [
"iana-time-zone",
"js-sys",
"num-traits",
"serde",
"wasm-bindgen",
"windows-link",
]
@ -2275,6 +2276,7 @@ version = "1.2.0"
dependencies = [
"bon",
"bytes",
"chrono",
"futures",
"http",
"prettyplease",

View file

@ -3,7 +3,7 @@
"info": {
"title": "Torn API",
"description": "\n * The development of Torn's API v2 is still ongoing.\n * If selections remain unaltered, they will default to the API v1 version.\n * Unlike API v1, API v2 accepts both selections and IDs as path and query parameters.\n * If any discrepancies or errors are found, please submit a [bug report](https://www.torn.com/forums.php#/p=forums&f=19&b=0&a=0) on the Torn Forums.\n * In case you're using bots to check for changes on openapi.json file, make sure to specificy a custom user-agent header - CloudFlare sometimes prevents requests from default user-agents.",
"version": "1.6.1"
"version": "1.7.0"
},
"servers": [
{
@ -1226,6 +1226,69 @@
"x-stability": "Stable"
}
},
"/user/reports": {
"get": {
"tags": [
"User"
],
"summary": "Get your reports",
"description": "Requires limited access key. <br>\n * The default limit is set to 25. However, the limit can be set to 100 for the 'stats' category.",
"operationId": "6ba15a813fe1cc014564e9dba892e022",
"parameters": [
{
"name": "cat",
"in": "query",
"description": "Used to filter reports with a specific type.",
"required": false,
"schema": {
"$ref": "#/components/schemas/ReportTypeEnum"
}
},
{
"name": "target",
"in": "query",
"description": "Get reports for a specific player by passing their player ID.",
"required": false,
"schema": {
"$ref": "#/components/schemas/UserId"
}
},
{
"$ref": "#/components/parameters/ApiLimit100Default20"
},
{
"$ref": "#/components/parameters/ApiOffset"
},
{
"$ref": "#/components/parameters/ApiTimestamp"
},
{
"$ref": "#/components/parameters/ApiComment"
},
{
"$ref": "#/components/parameters/ApiKeyLimited"
}
],
"responses": {
"200": {
"description": "Successful operation",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ReportsResponse"
}
}
}
}
},
"security": [
{
"api_key": []
}
],
"x-stability": "Unstable"
}
},
"/user/revives": {
"get": {
"tags": [
@ -2787,6 +2850,69 @@
"x-stability": "Stable"
}
},
"/faction/reports": {
"get": {
"tags": [
"Faction"
],
"summary": "Get faction reports",
"description": "Requires limited access key. <br>\n * The default limit is set to 25. However, the limit can be set to 100 for the 'stats' category.",
"operationId": "e8bd37f10cc5e0e8d6694bb306f3a1e4",
"parameters": [
{
"name": "cat",
"in": "query",
"description": "Used to filter reports with a specific type.",
"required": false,
"schema": {
"$ref": "#/components/schemas/ReportTypeEnum"
}
},
{
"name": "target",
"in": "query",
"description": "Get reports for a specific player by passing their player ID.",
"required": false,
"schema": {
"$ref": "#/components/schemas/UserId"
}
},
{
"$ref": "#/components/parameters/ApiLimit100Default20"
},
{
"$ref": "#/components/parameters/ApiOffset"
},
{
"$ref": "#/components/parameters/ApiTimestamp"
},
{
"$ref": "#/components/parameters/ApiComment"
},
{
"$ref": "#/components/parameters/ApiKeyLimited"
}
],
"responses": {
"200": {
"description": "Successful operation",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ReportsResponse"
}
}
}
}
},
"security": [
{
"api_key": []
}
],
"x-stability": "Unstable"
}
},
"/faction/revives": {
"get": {
"tags": [
@ -3998,15 +4124,6 @@
{
"$ref": "#/components/parameters/ApiOffsetNoDefault"
},
{
"name": "cat",
"in": "query",
"description": "Selection category",
"required": false,
"schema": {
"type": "string"
}
},
{
"$ref": "#/components/parameters/ApiTimestamp"
},
@ -5983,6 +6100,37 @@
"E"
]
},
"CountryEnum": {
"type": "string",
"enum": [
"Mexico",
"Hawaii",
"South Africa",
"Japan",
"China",
"Argentina",
"Switzerland",
"Canada",
"United Kingdom",
"UAE",
"Cayman Islands"
]
},
"ReportTypeEnum": {
"type": "string",
"enum": [
"mostwanted",
"money",
"stats",
"references",
"friendorfoe",
"companyfinancials",
"truelevel",
"stockanalysis",
"anonymousbounties",
"investment"
]
},
"ApiKeyAccessTypeEnum": {
"type": "string",
"enum": [
@ -11450,6 +11598,551 @@
},
"type": "object"
},
"ReportBase": {
"required": [
"type",
"target_id",
"reporter_id",
"faction_id",
"timestamp"
],
"properties": {
"type": {
"$ref": "#/components/schemas/ReportTypeEnum"
},
"target_id": {
"description": "The target ID if applicable.",
"oneOf": [
{
"$ref": "#/components/schemas/UserId"
},
{
"type": "null"
}
]
},
"reporter_id": {
"$ref": "#/components/schemas/UserId"
},
"faction_id": {
"description": "Reporter's faction ID if applicable.",
"oneOf": [
{
"$ref": "#/components/schemas/FactionId"
},
{
"type": "null"
}
]
},
"timestamp": {
"type": "integer",
"format": "int32"
}
},
"type": "object"
},
"ReportWarrantDetails": {
"required": [
"id",
"name",
"warrant"
],
"properties": {
"id": {
"$ref": "#/components/schemas/UserId"
},
"name": {
"type": "string"
},
"warrant": {
"type": "integer",
"format": "int32"
}
},
"type": "object"
},
"ReportMostWanted": {
"required": [
"top",
"notable"
],
"properties": {
"top": {
"type": "array",
"items": {
"$ref": "#/components/schemas/ReportWarrantDetails"
}
},
"notable": {
"type": "array",
"items": {
"$ref": "#/components/schemas/ReportWarrantDetails"
}
}
},
"type": "object"
},
"ReportMoney": {
"required": [
"money"
],
"properties": {
"money": {
"type": "integer",
"format": "int32"
}
},
"type": "object"
},
"ReportInvestment": {
"required": [
"amount",
"until"
],
"properties": {
"amount": {
"type": "integer",
"format": "int64"
},
"until": {
"type": "integer",
"format": "int32"
}
},
"type": "object"
},
"ReportTrueLevel": {
"required": [
"level"
],
"properties": {
"level": {
"type": "integer",
"format": "int32"
}
},
"type": "object"
},
"ReportStats": {
"required": [
"strength",
"speed",
"dexterity",
"defense",
"total"
],
"properties": {
"strength": {
"oneOf": [
{
"type": "integer",
"format": "int64"
},
{
"type": "null"
}
]
},
"speed": {
"oneOf": [
{
"type": "integer",
"format": "int64"
},
{
"type": "null"
}
]
},
"dexterity": {
"oneOf": [
{
"type": "integer",
"format": "int64"
},
{
"type": "null"
}
]
},
"defense": {
"oneOf": [
{
"type": "integer",
"format": "int64"
},
{
"type": "null"
}
]
},
"total": {
"oneOf": [
{
"type": "integer",
"format": "int64"
},
{
"type": "null"
}
]
}
},
"type": "object"
},
"ReportHistoryFaction": {
"required": [
"id",
"name",
"joined",
"left"
],
"properties": {
"id": {
"$ref": "#/components/schemas/FactionId"
},
"name": {
"type": "string"
},
"joined": {
"type": "string",
"format": "date"
},
"left": {
"oneOf": [
{
"type": "string",
"format": "date"
},
{
"type": "null"
}
]
}
},
"type": "object"
},
"ReportHistoryCompany": {
"required": [
"id",
"name",
"joined",
"left"
],
"properties": {
"id": {
"$ref": "#/components/schemas/CompanyId"
},
"name": {
"type": "string"
},
"joined": {
"type": "string",
"format": "date"
},
"left": {
"oneOf": [
{
"type": "string",
"format": "date"
},
{
"type": "null"
}
]
}
},
"type": "object"
},
"ReportHistory": {
"required": [
"factions",
"companies"
],
"properties": {
"factions": {
"type": "array",
"items": {
"$ref": "#/components/schemas/ReportHistoryFaction"
}
},
"companies": {
"type": "array",
"items": {
"$ref": "#/components/schemas/ReportHistoryCompany"
}
}
},
"type": "object"
},
"ReportFriendOrFoeUser": {
"required": [
"id",
"name"
],
"properties": {
"id": {
"$ref": "#/components/schemas/UserId"
},
"name": {
"type": "string"
}
},
"type": "object"
},
"ReportFriendOrFoe": {
"required": [
"friends",
"enemies"
],
"properties": {
"friends": {
"type": "array",
"items": {
"$ref": "#/components/schemas/ReportFriendOrFoeUser"
}
},
"enemies": {
"type": "array",
"items": {
"$ref": "#/components/schemas/ReportFriendOrFoeUser"
}
}
},
"type": "object"
},
"ReportCompanyFinancials": {
"required": [
"balance",
"employees",
"wages"
],
"properties": {
"balance": {
"type": "integer",
"format": "int64"
},
"employees": {
"type": "integer",
"format": "int32"
},
"wages": {
"required": [
"highest",
"lowest",
"average"
],
"properties": {
"highest": {
"type": "integer",
"format": "int32"
},
"lowest": {
"type": "integer",
"format": "int32"
},
"average": {
"type": "integer",
"format": "int32"
}
},
"type": "object"
}
},
"type": "object"
},
"ReportStockAnalysis": {
"required": [
"items"
],
"properties": {
"items": {
"type": "array",
"items": {
"required": [
"country",
"item",
"trip_duration",
"hourly_profit"
],
"properties": {
"country": {
"$ref": "#/components/schemas/CountryEnum"
},
"item": {
"required": [
"id",
"name",
"stock",
"price",
"value",
"due"
],
"properties": {
"id": {
"$ref": "#/components/schemas/ItemId"
},
"name": {
"type": "string"
},
"price": {
"type": "integer",
"format": "int32"
},
"value": {
"type": "integer",
"format": "int32"
},
"due": {
"oneOf": [
{
"type": "integer",
"format": "int32"
},
{
"type": "null"
}
]
}
},
"type": "object"
},
"trip_duration": {
"type": "integer",
"format": "int32"
},
"hourly_profit": {
"type": "integer",
"format": "int32"
}
},
"type": "object"
}
}
},
"type": "object"
},
"ReportAnonymousBounties": {
"required": [
"bounties"
],
"properties": {
"bounties": {
"type": "array",
"items": {
"required": [
"bounty",
"user",
"text"
],
"properties": {
"text": {
"type": "string"
},
"bounty": {
"type": "integer",
"format": "int64"
},
"user": {
"oneOf": [
{
"required": [
"id",
"name"
],
"properties": {
"id": {
"$ref": "#/components/schemas/UserId"
},
"name": {
"type": "string"
}
},
"type": "object"
},
{
"type": "null"
}
]
}
},
"type": "object"
}
}
},
"type": "object"
},
"ReportReport": {
"required": [
"report"
],
"properties": {
"report": {
"oneOf": [
{
"$ref": "#/components/schemas/ReportMoney"
},
{
"$ref": "#/components/schemas/ReportStats"
},
{
"$ref": "#/components/schemas/ReportMostWanted"
},
{
"$ref": "#/components/schemas/ReportHistory"
},
{
"$ref": "#/components/schemas/ReportFriendOrFoe"
},
{
"$ref": "#/components/schemas/ReportCompanyFinancials"
},
{
"$ref": "#/components/schemas/ReportTrueLevel"
},
{
"$ref": "#/components/schemas/ReportStockAnalysis"
},
{
"$ref": "#/components/schemas/ReportAnonymousBounties"
},
{
"$ref": "#/components/schemas/ReportInvestment"
}
]
}
},
"type": "object"
},
"Report": {
"allOf": [
{
"$ref": "#/components/schemas/ReportBase"
},
{
"$ref": "#/components/schemas/ReportReport"
}
]
},
"ReportsResponse": {
"required": [
"reports",
"_metadata"
],
"properties": {
"reports": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Report"
}
},
"_metadata": {
"$ref": "#/components/schemas/RequestMetadataWithLinks"
}
},
"type": "object"
},
"UserCurrentEducation": {
"required": [
"id",
@ -12412,7 +13105,7 @@
],
"properties": {
"value": {
"type": "integer",
"type": "number",
"format": "float"
},
"rank": {
@ -12434,7 +13127,7 @@
"rank": {
"oneOf": [
{
"type": "number",
"type": "integer",
"format": "int32"
},
{
@ -21603,6 +22296,16 @@
"minimum": 1
}
},
"ApiTarget": {
"name": "target",
"in": "query",
"required": false,
"schema": {
"type": "integer",
"format": "int32",
"minimum": 1
}
},
"ApiSort": {
"name": "sort",
"in": "query",

View file

@ -61,10 +61,7 @@ impl Model {
#[cfg(test)]
mod test {
use super::*;
use crate::{
model::r#enum::{EnumRepr, EnumVariant},
openapi::schema::OpenApiSchema,
};
use crate::openapi::schema::OpenApiSchema;
#[test]
fn resolve_newtypes() {

View file

@ -15,6 +15,7 @@ pub enum PrimitiveType {
I64,
String,
Float,
DateTime,
}
#[derive(Debug, Clone, PartialEq, Eq)]
@ -35,6 +36,9 @@ impl PropertyType {
Self::Primitive(PrimitiveType::String) => {
Some(format_ident!("String").into_token_stream())
}
Self::Primitive(PrimitiveType::DateTime) => {
Some(quote! { chrono::DateTime<chrono::Utc> })
}
Self::Primitive(PrimitiveType::Float) => Some(format_ident!("f64").into_token_stream()),
Self::Ref(path) => {
let name = path.strip_prefix("#/components/schemas/")?;
@ -211,6 +215,7 @@ impl Property {
PrimitiveType::Float
}
(Some("string"), None) => PrimitiveType::String,
(Some("string"), Some("date")) => PrimitiveType::DateTime,
(Some("boolean"), None) => PrimitiveType::Bool,
_ => return None,
};

View file

@ -303,12 +303,7 @@ The default value [Self::{}](self::{}#variant.{})"#,
let mut code = inner.codegen().unwrap_or_default();
let name = format_ident!("{}", outer_name);
let inner_ty = if matches!(items.as_ref(), ParameterType::Schema { type_name: _ }) {
let inner_name = format_ident!("{}", inner_name);
quote! { crate::models::#inner_name }
} else {
items.codegen_type_name(&inner_name).to_token_stream()
};
let inner_ty = items.codegen_type_name(&inner_name);
code.extend(quote! {
#[derive(Debug, Clone)]

View file

@ -31,6 +31,7 @@ futures = { version = "0.3", default-features = false, features = [
"std",
"async-await",
] }
chrono = { version = "0.4.41", features = ["serde"] }
[dev-dependencies]
tokio = { version = "1", features = ["full"] }

View file

@ -366,6 +366,15 @@ pub(super) mod test {
faction_scope.lookup(|b| b).await.unwrap();
}
#[tokio::test]
async fn faction_reports() {
let client = test_client().await;
let faction_scope = FactionScope(&client);
faction_scope.reports(|b| b).await.unwrap();
}
#[tokio::test]
async fn forum_categories() {
let client = test_client().await;
@ -954,4 +963,11 @@ pub(super) mod test {
client.user().attacks(|b| b).await.unwrap();
}
#[tokio::test]
async fn user_reports() {
let client = test_client().await;
client.user().reports(|b| b).await.unwrap();
}
}