Skip to content

Commit aa03ff8

Browse files
authored
Introduce API instance pattern (#3)
1 parent 849c77f commit aa03ff8

14 files changed

+1120
-951
lines changed

.generator/src/generator/openapi.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ def return_type(operation, version):
306306
for response in operation.get("responses", {}).values():
307307
for content in response.get("content", {}).values():
308308
if "schema" in content:
309-
return type_to_rust(content["schema"], version=version)
309+
return type_to_rust(content["schema"], version=version, render_option=False)
310310
return
311311

312312

.generator/src/generator/templates/api.j2

+261-223
Large diffs are not rendered by default.

.generator/src/generator/templates/function_mappings.j2

+40-8
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,48 @@ use futures::executor::block_on;
33
use serde_json::Value;
44
use std::collections::HashMap;
55

6-
use datadog_api_client::datadog::*;{#
6+
use datadog_api_client::datadog::*;
7+
{#
78
{% for name, operations in apis["v1"].items() %}
89
{%- set classname = "api_"+name%}
910
use datadog_api_client::datadogV1::{{classname | snake_case}}::*;
1011
{%- endfor %}
11-
#}{%- for version, apis in all_apis.items() %}
12+
#}
13+
{%- for version, apis in all_apis.items() %}
1214
{%- for name, operations in apis.items() %}
1315
{%- set classname = "api_"+name %}
1416
use datadog_api_client::datadog{{ version.upper() }}::api::{{classname | snake_case}}::*;
1517
{%- endfor %}
1618
{%- endfor %}
1719

20+
#[derive(Debug, Default)]
21+
pub struct ApiInstances {
22+
{%- for version, apis in all_apis.items() %}
23+
{%- for name, operations in apis.items() %}
24+
{%- set fieldName = "api_"+name %}
25+
{%- set structName = name|camel_case +"API" %}
26+
pub {{fieldName | snake_case}}: Option<{{structName}}>,
27+
{%- endfor %}
28+
{%- endfor %}
29+
}
30+
31+
pub fn initialize_api_instance(world: &mut DatadogWorld, api: String) {
32+
match api.as_str() {
33+
{%- for version, apis in all_apis.items() %}
34+
{%- for name, operations in apis.items() %}
35+
{%- set fieldName = "api_"+name %}
36+
{%- set structName = name|camel_case +"API" %}
37+
"{{name|camel_case}}" => {
38+
if world.api_instances.api_fastly_integration.is_none() {
39+
world.api_instances.{{fieldName | snake_case}} = Some({{structName}}::with_config(world.config.clone()));
40+
}
41+
},
42+
{%- endfor %}
43+
{%- endfor %}
44+
_ => panic!("{api} API instance not found"),
45+
}
46+
}
47+
1848
pub fn collect_function_calls(world: &mut DatadogWorld) {
1949
{%- for _, apis in all_apis.items() %}
2050
{%- for _, operations in apis.items() %}
@@ -26,26 +56,28 @@ pub fn collect_function_calls(world: &mut DatadogWorld) {
2656
}
2757

2858
{%- for _, apis in all_apis.items() %}
29-
{%- for _, operations in apis.items() %}
59+
{%- for name, operations in apis.items() %}
60+
{%- set apiName = "api_"+name | snake_case %}
3061
{% for _, _, operation in operations %}
3162
{%- set operationParams = operation|parameters|list %}
3263
fn test_{{ operation['operationId'] | snake_case }}(world: &mut DatadogWorld, _parameters: &HashMap<String, Value>) {
64+
let api = world.api_instances.{{ apiName }}.as_ref().expect("api instance not found");
3365
{%- if operationParams|length > 0 -%}
3466
let params = {{ operation['operationId'] }}Params {
3567
{%- for param in operationParams %}
3668
{{ param[0] }}: serde_json::from_value(_parameters.get("{{ param[0] }}").unwrap().clone()).unwrap(),
3769
{%- endfor %}
3870
};
39-
let response = match block_on({{ operation['operationId'] | snake_case}}(&world.config, params)) {
71+
let response = match block_on(api.{{ operation['operationId'] | snake_case}}_with_http_info(params)) {
4072
{%- else %}
41-
let response = match block_on({{ operation['operationId'] | snake_case}}(&world.config)) {
73+
let response = match block_on(api.{{ operation['operationId'] | snake_case}}_with_http_info()) {
4274
{%- endif %}
4375
Ok(response) => response,
4476
Err(error) => {
4577
return match error {
46-
Error::Reqwest(e) => panic!("reqwest error: {}", e.to_string()),
47-
Error::Serde(e) => panic!("serde error: {}", e.to_string()),
48-
Error::Io(e) => panic!("io error: {}", e.to_string()),
78+
Error::Reqwest(e) => panic!("reqwest error: {}", e),
79+
Error::Serde(e) => panic!("serde error: {}", e),
80+
Error::Io(e) => panic!("io error: {}", e),
4981
Error::ResponseError(e) => world.response.code = e.status.as_u16(),
5082
};
5183
}

.pre-commit-config.yaml

+7-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ repos:
99
pass_filenames: false
1010
- id: lint
1111
name: Lint
12+
entry: cargo clippy
13+
language: system
14+
pass_filenames: false
15+
stages: [manual]
16+
- id: format
17+
name: Format
1218
language: rust
1319
entry: cargo fmt
14-
stages: [manual]
15-
# files: '^api/.*\.go'
16-
# types: ["file", "go"]
1720
pass_filenames: false
21+
stages: [manual]
1822
- id: generator
1923
name: generator
2024
language: python

generate.sh

+1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ pre_commit_wrapper () {
2525

2626
rm -rf src/* examples/*
2727
pre_commit_wrapper generator
28+
pre_commit_wrapper format
2829
pre_commit_wrapper lint

rustfmt.toml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
max_width = 120
2+
edition = "2021"
3+
use_small_heuristics = "Default"

src/datadog/mod.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,15 @@ pub fn parse_deep_object(prefix: &str, value: &serde_json::Value) -> Vec<(String
6767

6868
for (key, value) in object {
6969
match value {
70-
serde_json::Value::Object(_) => params.append(&mut parse_deep_object(
71-
&format!("{}[{}]", prefix, key),
72-
value,
73-
)),
70+
serde_json::Value::Object(_) => {
71+
params.append(&mut parse_deep_object(&format!("{}[{}]", prefix, key), value))
72+
}
7473
serde_json::Value::Array(array) => {
7574
for (i, value) in array.iter().enumerate() {
76-
params.append(&mut parse_deep_object(
77-
&format!("{}[{}][{}]", prefix, key, i),
78-
value,
79-
));
75+
params.append(&mut parse_deep_object(&format!("{}[{}][{}]", prefix, key, i), value));
8076
}
8177
}
82-
serde_json::Value::String(s) => {
83-
params.push((format!("{}[{}]", prefix, key), s.clone()))
84-
}
78+
serde_json::Value::String(s) => params.push((format!("{}[{}]", prefix, key), s.clone())),
8579
_ => params.push((format!("{}[{}]", prefix, key), value.to_string())),
8680
}
8781
}

0 commit comments

Comments
 (0)