Skip to content

Commit 0fbc167

Browse files
committed
Remove generic types for Endpoint::body and Endpoint::query
Simplifies the ergonomics for Endpoint users by removing some irrelevant generic parameters and hiding implementation details. Slightly improves codegen (~15% smaller) and build times.
1 parent 93040ed commit 0fbc167

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+246
-162
lines changed

cloudflare-examples/src/main.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,9 @@ fn delete_route(arg_matches: &ArgMatches, api_client: &HttpApiClient) {
215215
}
216216

217217
/// Add and leak a mock (so it runs for 'static)
218-
fn add_static_mock<ResultType, QueryType, BodyType>(
219-
endpoint: &dyn Endpoint<ResultType, QueryType, BodyType>,
220-
) where
218+
fn add_static_mock<ResultType>(endpoint: &dyn Endpoint<ResultType>)
219+
where
221220
ResultType: ApiResult,
222-
QueryType: Serialize,
223-
BodyType: Serialize,
224221
{
225222
let body = ApiErrors {
226223
errors: vec![ApiError {

cloudflare/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ default = ["default-tls"]
1313
blocking = ["reqwest/blocking"]
1414
default-tls = ["reqwest/default-tls"]
1515
rustls-tls = ["reqwest/rustls-tls"]
16+
spec = []
1617

1718
[dependencies]
1819
chrono = { version = "0.4", default-features = false, features = [
@@ -28,6 +29,7 @@ reqwest = { version = "0.11.4", default-features = false, features = ["json"] }
2829
serde = { version = "1.0", features = ["derive"] }
2930
serde_json = "1.0"
3031
serde_with = { version = "2.0", features = ["base64"] }
32+
serde_urlencoded = "0.7.1"
3133
thiserror = "1"
3234
url = "2.2"
3335
uuid = { version = "1.0", features = ["serde"] }

cloudflare/src/endpoints/account/list_accounts.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::Account;
22

3-
use crate::framework::endpoint::{Endpoint, Method};
3+
use crate::framework::endpoint::{serialize_query, EndpointSpec, Method};
44
use crate::framework::OrderDirection;
55

66
use serde::Serialize;
@@ -13,15 +13,16 @@ pub struct ListAccounts {
1313
pub params: Option<ListAccountsParams>,
1414
}
1515

16-
impl Endpoint<Vec<Account>, ListAccountsParams> for ListAccounts {
16+
impl EndpointSpec<Vec<Account>> for ListAccounts {
1717
fn method(&self) -> Method {
1818
Method::GET
1919
}
2020
fn path(&self) -> String {
2121
"accounts".to_string()
2222
}
23-
fn query(&self) -> Option<ListAccountsParams> {
24-
self.params.clone()
23+
#[inline]
24+
fn query(&self) -> Option<String> {
25+
serialize_query(&self.params)
2526
}
2627
}
2728

cloudflare/src/endpoints/argo_tunnel/create_tunnel.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use serde_with::{
55
serde_as,
66
};
77

8-
use crate::framework::endpoint::{Endpoint, Method};
8+
use crate::framework::endpoint::{EndpointSpec, Method};
99

1010
use super::Tunnel;
1111

@@ -19,15 +19,17 @@ pub struct CreateTunnel<'a> {
1919
pub params: Params<'a>,
2020
}
2121

22-
impl<'a> Endpoint<Tunnel, (), Params<'a>> for CreateTunnel<'a> {
22+
impl<'a> EndpointSpec<Tunnel> for CreateTunnel<'a> {
2323
fn method(&self) -> Method {
2424
Method::POST
2525
}
2626
fn path(&self) -> String {
2727
format!("accounts/{}/tunnels", self.account_identifier)
2828
}
29-
fn body(&self) -> Option<Params<'a>> {
30-
Some(self.params.clone())
29+
#[inline]
30+
fn body(&self) -> Option<String> {
31+
let body = serde_json::to_string(&self.params).unwrap();
32+
Some(body)
3133
}
3234
}
3335

cloudflare/src/endpoints/argo_tunnel/delete_tunnel.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::framework::endpoint::{Endpoint, Method};
1+
use crate::framework::endpoint::{EndpointSpec, Method};
22

33
use super::Tunnel;
44

@@ -12,7 +12,7 @@ pub struct DeleteTunnel<'a> {
1212
pub cascade: bool,
1313
}
1414

15-
impl<'a> Endpoint<Tunnel> for DeleteTunnel<'a> {
15+
impl<'a> EndpointSpec<Tunnel> for DeleteTunnel<'a> {
1616
fn method(&self) -> Method {
1717
Method::DELETE
1818
}

cloudflare/src/endpoints/argo_tunnel/list_tunnels.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use chrono::{DateTime, Utc};
22
use serde::Serialize;
33

4-
use crate::framework::endpoint::{Endpoint, Method};
4+
use crate::framework::endpoint::{serialize_query, EndpointSpec, Method};
55

66
use super::Tunnel;
77

@@ -13,15 +13,16 @@ pub struct ListTunnels<'a> {
1313
pub params: Params,
1414
}
1515

16-
impl<'a> Endpoint<Vec<Tunnel>, Params> for ListTunnels<'a> {
16+
impl<'a> EndpointSpec<Vec<Tunnel>> for ListTunnels<'a> {
1717
fn method(&self) -> Method {
1818
Method::GET
1919
}
2020
fn path(&self) -> String {
2121
format!("accounts/{}/tunnels", self.account_identifier)
2222
}
23-
fn query(&self) -> Option<Params> {
24-
Some(self.params.clone())
23+
#[inline]
24+
fn query(&self) -> Option<String> {
25+
serialize_query(&self.params)
2526
}
2627
}
2728

cloudflare/src/endpoints/argo_tunnel/route_dns.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::framework::endpoint::{Endpoint, Method};
1+
use crate::framework::endpoint::{EndpointSpec, Method};
22

33
use super::RouteResult;
44
use serde::Serialize;
@@ -16,15 +16,17 @@ pub struct RouteTunnel<'a> {
1616
pub params: Params<'a>,
1717
}
1818

19-
impl<'a> Endpoint<RouteResult, (), Params<'a>> for RouteTunnel<'a> {
19+
impl<'a> EndpointSpec<RouteResult> for RouteTunnel<'a> {
2020
fn method(&self) -> Method {
2121
Method::PUT
2222
}
2323
fn path(&self) -> String {
2424
format!("zones/{}/tunnels/{}/routes", self.zone_tag, self.tunnel_id)
2525
}
26-
fn body(&self) -> Option<Params<'a>> {
27-
Some(self.params.clone())
26+
#[inline]
27+
fn body(&self) -> Option<String> {
28+
let body = serde_json::to_string(&self.params).unwrap();
29+
Some(body)
2830
}
2931
}
3032

cloudflare/src/endpoints/dns.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::framework::{
2-
endpoint::{Endpoint, Method},
2+
endpoint::{serialize_query, EndpointSpec, Method},
33
response::ApiResult,
44
};
55
/// <https://api.cloudflare.com/#dns-records-for-a-zone-properties>
@@ -16,15 +16,16 @@ pub struct ListDnsRecords<'a> {
1616
pub zone_identifier: &'a str,
1717
pub params: ListDnsRecordsParams,
1818
}
19-
impl<'a> Endpoint<Vec<DnsRecord>, ListDnsRecordsParams> for ListDnsRecords<'a> {
19+
impl<'a> EndpointSpec<Vec<DnsRecord>> for ListDnsRecords<'a> {
2020
fn method(&self) -> Method {
2121
Method::GET
2222
}
2323
fn path(&self) -> String {
2424
format!("zones/{}/dns_records", self.zone_identifier)
2525
}
26-
fn query(&self) -> Option<ListDnsRecordsParams> {
27-
Some(self.params.clone())
26+
#[inline]
27+
fn query(&self) -> Option<String> {
28+
serialize_query(&self.params)
2829
}
2930
}
3031

@@ -36,15 +37,17 @@ pub struct CreateDnsRecord<'a> {
3637
pub params: CreateDnsRecordParams<'a>,
3738
}
3839

39-
impl<'a> Endpoint<DnsRecord, (), CreateDnsRecordParams<'a>> for CreateDnsRecord<'a> {
40+
impl<'a> EndpointSpec<DnsRecord> for CreateDnsRecord<'a> {
4041
fn method(&self) -> Method {
4142
Method::POST
4243
}
4344
fn path(&self) -> String {
4445
format!("zones/{}/dns_records", self.zone_identifier)
4546
}
46-
fn body(&self) -> Option<CreateDnsRecordParams<'a>> {
47-
Some(self.params.clone())
47+
#[inline]
48+
fn body(&self) -> Option<String> {
49+
let body = serde_json::to_string(&self.params).unwrap();
50+
Some(body)
4851
}
4952
}
5053

@@ -72,7 +75,7 @@ pub struct DeleteDnsRecord<'a> {
7275
pub zone_identifier: &'a str,
7376
pub identifier: &'a str,
7477
}
75-
impl<'a> Endpoint<DeleteDnsRecordResponse> for DeleteDnsRecord<'a> {
78+
impl<'a> EndpointSpec<DeleteDnsRecordResponse> for DeleteDnsRecord<'a> {
7679
fn method(&self) -> Method {
7780
Method::DELETE
7881
}
@@ -93,7 +96,7 @@ pub struct UpdateDnsRecord<'a> {
9396
pub params: UpdateDnsRecordParams<'a>,
9497
}
9598

96-
impl<'a> Endpoint<DnsRecord, (), UpdateDnsRecordParams<'a>> for UpdateDnsRecord<'a> {
99+
impl<'a> EndpointSpec<DnsRecord> for UpdateDnsRecord<'a> {
97100
fn method(&self) -> Method {
98101
Method::PUT
99102
}
@@ -103,8 +106,10 @@ impl<'a> Endpoint<DnsRecord, (), UpdateDnsRecordParams<'a>> for UpdateDnsRecord<
103106
self.zone_identifier, self.identifier
104107
)
105108
}
106-
fn body(&self) -> Option<UpdateDnsRecordParams<'a>> {
107-
Some(self.params.clone())
109+
#[inline]
110+
fn body(&self) -> Option<String> {
111+
let body = serde_json::to_string(&self.params).unwrap();
112+
Some(body)
108113
}
109114
}
110115

cloudflare/src/endpoints/load_balancing/create_lb.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::endpoints::load_balancing::{
22
LbPoolId, LbPoolMapping, LoadBalancer, SessionAffinity, SessionAffinityAttributes,
33
SteeringPolicy,
44
};
5-
use crate::framework::endpoint::{Endpoint, Method};
5+
use crate::framework::endpoint::{EndpointSpec, Method};
66

77
use serde::Serialize;
88

@@ -55,14 +55,16 @@ pub struct OptionalParams<'a> {
5555
pub session_affinity_ttl: Option<u32>,
5656
}
5757

58-
impl<'a> Endpoint<LoadBalancer, (), Params<'a>> for CreateLoadBalancer<'a> {
58+
impl<'a> EndpointSpec<LoadBalancer> for CreateLoadBalancer<'a> {
5959
fn method(&self) -> Method {
6060
Method::POST
6161
}
6262
fn path(&self) -> String {
6363
format!("zones/{}/load_balancers", self.zone_identifier)
6464
}
65-
fn body(&self) -> Option<Params<'a>> {
66-
Some(self.params.clone())
65+
#[inline]
66+
fn body(&self) -> Option<String> {
67+
let body = serde_json::to_string(&self.params).unwrap();
68+
Some(body)
6769
}
6870
}

cloudflare/src/endpoints/load_balancing/create_pool.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::endpoints::load_balancing::{Origin, Pool};
2-
use crate::framework::endpoint::{Endpoint, Method};
2+
use crate::framework::endpoint::{EndpointSpec, Method};
33

44
use serde::Serialize;
55

@@ -51,14 +51,16 @@ pub struct OptionalParams<'a> {
5151
pub notification_email: Option<&'a str>,
5252
}
5353

54-
impl<'a> Endpoint<Pool, (), Params<'a>> for CreatePool<'a> {
54+
impl<'a> EndpointSpec<Pool> for CreatePool<'a> {
5555
fn method(&self) -> Method {
5656
Method::POST
5757
}
5858
fn path(&self) -> String {
5959
format!("accounts/{}/load_balancers/pools", self.account_identifier)
6060
}
61-
fn body(&self) -> Option<Params<'a>> {
62-
Some(self.params.clone())
61+
#[inline]
62+
fn body(&self) -> Option<String> {
63+
let body = serde_json::to_string(&self.params).unwrap();
64+
Some(body)
6365
}
6466
}

0 commit comments

Comments
 (0)