aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Edgecumbe <git@esotericnonsense.com>2019-10-25 08:04:28 +0200
committerDaniel Edgecumbe <git@esotericnonsense.com>2019-10-25 08:04:28 +0200
commitdac3de198487a310da8f87335736951ada518d88 (patch)
tree253927342df71f1f1c1d13e62f6d0ddc2ed0b9eb
parent10b5b166ccf6bc2b55ee80233f55a8bcd0f6de72 (diff)
All generated functions now take a RequestBuilder
-rwxr-xr-xgenapi/main.py30
-rw-r--r--src/json_rpc.rs17
-rw-r--r--src/main.rs45
3 files changed, 54 insertions, 38 deletions
diff --git a/genapi/main.py b/genapi/main.py
index 9fd1a4f..b76ed41 100755
--- a/genapi/main.py
+++ b/genapi/main.py
@@ -581,8 +581,9 @@ def generate_rust_functions(operations: List[Operation]) -> str:
_type: str = python_type_to_rust_type(param._type, param.mandatory)
params_converted.append((name, _type))
- formatted_params: str = ", ".join(
- f"{x[0]}: {x[1]}" for x in params_converted
+ formatted_params_args: str = ", ".join(
+ ["rb: RequestBuilder"]
+ + [f"{x[0]}: {x[1]}" for x in params_converted]
)
resp_type: str = python_type_to_rust_type(
@@ -615,14 +616,30 @@ pub struct {struct_name} {{ {formatted_params_struct} }}"""
f"{x[0]}" for x in params_converted
)
- function_interior = f"let req: {struct_name} = {struct_name} {{ {formatted_params_declare} }};"
+ function_interior = f"""
+let req: {struct_name} = {struct_name} {{ {formatted_params_declare} }};
+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())
+"""
else:
- function_interior = ""
+ # TODO this smells, repetition
+ function_interior = f"""
+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())
+"""
functions.append(
# TODO: implement the actual functions
# f"pub fn {operation.name}({formatted_params}) -> {resp_type} {{}}"
- f"""pub fn {operation.name}({formatted_params}) -> () {{
+ f"""pub fn {operation.name}({formatted_params_args}) -> Result<{resp_type}, AnyError> {{
{function_interior}
}}"""
)
@@ -642,6 +659,9 @@ 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::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 b7d3755..fc66460 100644
--- a/src/json_rpc.rs
+++ b/src/json_rpc.rs
@@ -8,13 +8,6 @@ pub struct RpcRequest<T> {
id: String,
}
-#[derive(Deserialize)]
-pub struct RpcResponse<T> {
- jsonrpc: String,
- result: T,
- id: String,
-}
-
impl<T> RpcRequest<T> {
pub fn new(method: String, params: T) -> Self {
let r: u128 = rand::random();
@@ -27,8 +20,18 @@ impl<T> RpcRequest<T> {
}
}
+#[derive(Deserialize)]
+pub struct RpcResponse<T> {
+ jsonrpc: String,
+ result: T,
+ id: String,
+}
+
impl<T> RpcResponse<T> {
pub fn into_inner(self) -> T {
+ // TODO check these? do we care?
+ let _ = self.jsonrpc;
+ let _ = self.id;
self.result
}
}
diff --git a/src/main.rs b/src/main.rs
index a67f930..dd1b07a 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -35,7 +35,7 @@ const USERFILE: &str = "/home/esotericnonsense/betfair/betfair-user";
const PASSFILE: &str = "/home/esotericnonsense/betfair/betfair-pass";
#[derive(Debug)]
-enum AnyError {
+pub enum AnyError {
Io(std::io::Error),
Reqwest(reqwest::Error),
Other,
@@ -94,8 +94,7 @@ fn get_session_token() -> Result<String, AnyError> {
}
}
-use generated_api::{listMarketBookRequest, MarketBook, MarketId};
-use json_rpc::{RpcRequest, RpcResponse};
+use generated_api::{listMarketBook, MarketBook, MarketId};
fn try_lmb(
session_token: String,
market_id: MarketId,
@@ -105,32 +104,26 @@ fn try_lmb(
let proxy = reqwest::Proxy::all("socks5h://127.0.0.1:40001")?;
let cl: Client = Client::builder().proxy(proxy).build()?;
- let method = "SportsAPING/v1.0/listMarketBook".to_owned();
- let params = listMarketBookRequest {
- marketIds: vec![market_id],
- priceProjection: None,
- orderProjection: None,
- matchProjection: None,
- includeOverallPosition: None,
- partitionMatchedByStrategyRef: None,
- customerStrategyRefs: None,
- currencyCode: None,
- locale: None,
- matchedSince: None,
- betIds: None,
- };
- let rpc_request = RpcRequest::new(method, params);
-
// TODO handle exceptions
- let rpc_response: RpcResponse<Vec<MarketBook>> = cl
+ let rb: reqwest::RequestBuilder = cl
.post(JSONRPC_URI)
.header("X-Application", app_key)
- .header("X-Authentication", session_token)
- .json(&rpc_request)
- .send()?
- .json()?;
-
- Ok(rpc_response.into_inner())
+ .header("X-Authentication", session_token);
+
+ listMarketBook(
+ rb,
+ vec![market_id],
+ None,
+ None,
+ None,
+ None,
+ None,
+ None,
+ None,
+ None,
+ None,
+ None,
+ )
}
fn main() -> Result<(), AnyError> {