diff options
author | Daniel Edgecumbe <git@esotericnonsense.com> | 2019-10-26 21:42:07 +0200 |
---|---|---|
committer | Daniel Edgecumbe <git@esotericnonsense.com> | 2019-10-26 21:42:07 +0200 |
commit | 18c838442762789c4781dae3a532d9af7b9e13bd (patch) | |
tree | 791cfb6d2606b4688263a0aee4ba94a07c8c294b | |
parent | 8a659ca0f732c08f64d28e47107041d2c3674ab8 (diff) |
Working implementation of BFApiCalls trait
-rwxr-xr-x | genapi/main.py | 36 | ||||
-rw-r--r-- | src/json_rpc.rs | 30 | ||||
-rw-r--r-- | src/main.rs | 7 |
3 files changed, 21 insertions, 52 deletions
diff --git a/genapi/main.py b/genapi/main.py index 372a102..ff481ac 100755 --- a/genapi/main.py +++ b/genapi/main.py @@ -573,6 +573,7 @@ def generate_rust_functions(operations: List[Operation]) -> str: structs: List[str] = [] functions: List[str] = [] + fn_signatures: List[str] = [] for operation in operations: # type: Operation # print(operation) @@ -583,8 +584,7 @@ def generate_rust_functions(operations: List[Operation]) -> str: params_converted.append((name, _type)) formatted_params_args: str = ", ".join( - ["rb: RequestBuilder"] - + [f"{x[0]}: {x[1]}" for x in params_converted] + ["&self"] + [f"{x[0]}: {x[1]}" for x in params_converted] ) resp_type: str = python_type_to_rust_type( @@ -623,8 +623,7 @@ let rpc_request: RpcRequest<{struct_name}> = RpcRequest::new( \"SportsAPING/v1.0/{operation.name}\".to_owned(), req ); -let resp: RpcResponse<{resp_type}> = rb.json(&rpc_request).send()?.json()?; -Ok(resp.into_inner()) +self.req(rpc_request).map(|x| x.into_inner()) """ else: # TODO this smells, repetition @@ -633,18 +632,26 @@ let rpc_request: RpcRequest<()> = RpcRequest::new( \"SportsAPING/v1.0/{operation.name}\".to_owned(), () ); -let resp: RpcResponse<{resp_type}> = rb.json(&rpc_request).send()?.json()?; -Ok(resp.into_inner()) +self.req(rpc_request).map(|x| x.into_inner()) """ - functions.append( - f"""pub fn {operation.name}({formatted_params_args}) -> -Result<{resp_type}, AnyError> {{ - {function_interior} -}}""" - ) + fn_signature = f"""fn {operation.name}({formatted_params_args}) -> +Result<{resp_type}, AnyError>""" + + fn_signatures.append(fn_signature + ";") + functions.append(f"{fn_signature} {{ {function_interior} }}") - return "\n\n".join(structs + functions) + structs_out = "\n".join(structs) + fn_signatures_out = "\n".join(fn_signatures) + functions_out = "\n".join(functions) + + return "\n".join( + [ + structs_out, + f"pub trait BFApiCalls {{ {fn_signatures_out} }}", + f"impl BFApiCalls for crate::BFClient {{ {functions_out} }}", + ] + ) def main() -> None: @@ -662,9 +669,8 @@ def main() -> None: print("use std::collections::HashMap;") print("use chrono::{DateTime, Utc};") print("use serde::{Deserialize, Serialize};") - print("use crate::json_rpc::{RpcRequest, RpcResponse};") + print("use crate::json_rpc::RpcRequest;") print("use crate::AnyError;") - print("use reqwest::RequestBuilder;") print(generate_rust_functions(aping.operations)) print(generate_rust_types(aping.simple_types)) print(generate_rust_data_types(aping.data_types)) diff --git a/src/json_rpc.rs b/src/json_rpc.rs index fc66460..22ab04e 100644 --- a/src/json_rpc.rs +++ b/src/json_rpc.rs @@ -35,33 +35,3 @@ impl<T> RpcResponse<T> { self.result } } - -// listEventTypes Request -// [ -// { -// "jsonrpc": "2.0", -// "method": "SportsAPING/v1.0/listEventTypes", -// "params": { -// "filter": {} -// }, -// "id": 1 -// } -// ] -// listEventTypes Response -// [ -// { -// "jsonrpc": "2.0", -// "result": [ -// { -// "eventType": { -// "id": "468328", -// "name": "Handball" -// }, -// "marketCount": 11 -// }, -// ... removed -// ], -// "id": 1 -// } -// ] -// diff --git a/src/main.rs b/src/main.rs index 159440a..8bacd89 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,6 @@ use crate::json_rpc::{RpcRequest, RpcResponse}; pub enum AnyError { Io(std::io::Error), Reqwest(reqwest::Error), - SerdeJson(serde_json::Error), BFLoginFailure(String), General(String), Other, @@ -35,12 +34,6 @@ impl From<reqwest::Error> for AnyError { } } -impl From<serde_json::Error> for AnyError { - fn from(e: serde_json::Error) -> Self { - AnyError::SerdeJson(e) - } -} - #[derive(Debug, Serialize)] struct LoginRequestForm { username: String, |