Skip to content

Commit

Permalink
Remove generic types for Endpoint::body and Endpoint::query
Browse files Browse the repository at this point in the history
Simplifies the ergonomics for Endpoint users by removing
some irrelevant generic parameters and hiding implementation
details. Slightly improves codegen (~15% smaller) and build times.
  • Loading branch information
jeff-hiner committed Mar 23, 2023
1 parent 93040ed commit 0fbc167
Show file tree
Hide file tree
Showing 41 changed files with 246 additions and 162 deletions.
7 changes: 2 additions & 5 deletions cloudflare-examples/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,9 @@ fn delete_route(arg_matches: &ArgMatches, api_client: &HttpApiClient) {
}

/// Add and leak a mock (so it runs for 'static)
fn add_static_mock<ResultType, QueryType, BodyType>(
endpoint: &dyn Endpoint<ResultType, QueryType, BodyType>,
) where
fn add_static_mock<ResultType>(endpoint: &dyn Endpoint<ResultType>)
where
ResultType: ApiResult,
QueryType: Serialize,
BodyType: Serialize,
{
let body = ApiErrors {
errors: vec![ApiError {
Expand Down
2 changes: 2 additions & 0 deletions cloudflare/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ default = ["default-tls"]
blocking = ["reqwest/blocking"]
default-tls = ["reqwest/default-tls"]
rustls-tls = ["reqwest/rustls-tls"]
spec = []

[dependencies]
chrono = { version = "0.4", default-features = false, features = [
Expand All @@ -28,6 +29,7 @@ reqwest = { version = "0.11.4", default-features = false, features = ["json"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_with = { version = "2.0", features = ["base64"] }
serde_urlencoded = "0.7.1"
thiserror = "1"
url = "2.2"
uuid = { version = "1.0", features = ["serde"] }
9 changes: 5 additions & 4 deletions cloudflare/src/endpoints/account/list_accounts.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::Account;

use crate::framework::endpoint::{Endpoint, Method};
use crate::framework::endpoint::{serialize_query, EndpointSpec, Method};
use crate::framework::OrderDirection;

use serde::Serialize;
Expand All @@ -13,15 +13,16 @@ pub struct ListAccounts {
pub params: Option<ListAccountsParams>,
}

impl Endpoint<Vec<Account>, ListAccountsParams> for ListAccounts {
impl EndpointSpec<Vec<Account>> for ListAccounts {
fn method(&self) -> Method {
Method::GET
}
fn path(&self) -> String {
"accounts".to_string()
}
fn query(&self) -> Option<ListAccountsParams> {
self.params.clone()
#[inline]
fn query(&self) -> Option<String> {
serialize_query(&self.params)
}
}

Expand Down
10 changes: 6 additions & 4 deletions cloudflare/src/endpoints/argo_tunnel/create_tunnel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serde_with::{
serde_as,
};

use crate::framework::endpoint::{Endpoint, Method};
use crate::framework::endpoint::{EndpointSpec, Method};

use super::Tunnel;

Expand All @@ -19,15 +19,17 @@ pub struct CreateTunnel<'a> {
pub params: Params<'a>,
}

impl<'a> Endpoint<Tunnel, (), Params<'a>> for CreateTunnel<'a> {
impl<'a> EndpointSpec<Tunnel> for CreateTunnel<'a> {
fn method(&self) -> Method {
Method::POST
}
fn path(&self) -> String {
format!("accounts/{}/tunnels", self.account_identifier)
}
fn body(&self) -> Option<Params<'a>> {
Some(self.params.clone())
#[inline]
fn body(&self) -> Option<String> {
let body = serde_json::to_string(&self.params).unwrap();
Some(body)
}
}

Expand Down
4 changes: 2 additions & 2 deletions cloudflare/src/endpoints/argo_tunnel/delete_tunnel.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::framework::endpoint::{Endpoint, Method};
use crate::framework::endpoint::{EndpointSpec, Method};

use super::Tunnel;

Expand All @@ -12,7 +12,7 @@ pub struct DeleteTunnel<'a> {
pub cascade: bool,
}

impl<'a> Endpoint<Tunnel> for DeleteTunnel<'a> {
impl<'a> EndpointSpec<Tunnel> for DeleteTunnel<'a> {
fn method(&self) -> Method {
Method::DELETE
}
Expand Down
9 changes: 5 additions & 4 deletions cloudflare/src/endpoints/argo_tunnel/list_tunnels.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use chrono::{DateTime, Utc};
use serde::Serialize;

use crate::framework::endpoint::{Endpoint, Method};
use crate::framework::endpoint::{serialize_query, EndpointSpec, Method};

use super::Tunnel;

Expand All @@ -13,15 +13,16 @@ pub struct ListTunnels<'a> {
pub params: Params,
}

impl<'a> Endpoint<Vec<Tunnel>, Params> for ListTunnels<'a> {
impl<'a> EndpointSpec<Vec<Tunnel>> for ListTunnels<'a> {
fn method(&self) -> Method {
Method::GET
}
fn path(&self) -> String {
format!("accounts/{}/tunnels", self.account_identifier)
}
fn query(&self) -> Option<Params> {
Some(self.params.clone())
#[inline]
fn query(&self) -> Option<String> {
serialize_query(&self.params)
}
}

Expand Down
10 changes: 6 additions & 4 deletions cloudflare/src/endpoints/argo_tunnel/route_dns.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::framework::endpoint::{Endpoint, Method};
use crate::framework::endpoint::{EndpointSpec, Method};

use super::RouteResult;
use serde::Serialize;
Expand All @@ -16,15 +16,17 @@ pub struct RouteTunnel<'a> {
pub params: Params<'a>,
}

impl<'a> Endpoint<RouteResult, (), Params<'a>> for RouteTunnel<'a> {
impl<'a> EndpointSpec<RouteResult> for RouteTunnel<'a> {
fn method(&self) -> Method {
Method::PUT
}
fn path(&self) -> String {
format!("zones/{}/tunnels/{}/routes", self.zone_tag, self.tunnel_id)
}
fn body(&self) -> Option<Params<'a>> {
Some(self.params.clone())
#[inline]
fn body(&self) -> Option<String> {
let body = serde_json::to_string(&self.params).unwrap();
Some(body)
}
}

Expand Down
27 changes: 16 additions & 11 deletions cloudflare/src/endpoints/dns.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::framework::{
endpoint::{Endpoint, Method},
endpoint::{serialize_query, EndpointSpec, Method},
response::ApiResult,
};
/// <https://api.cloudflare.com/#dns-records-for-a-zone-properties>
Expand All @@ -16,15 +16,16 @@ pub struct ListDnsRecords<'a> {
pub zone_identifier: &'a str,
pub params: ListDnsRecordsParams,
}
impl<'a> Endpoint<Vec<DnsRecord>, ListDnsRecordsParams> for ListDnsRecords<'a> {
impl<'a> EndpointSpec<Vec<DnsRecord>> for ListDnsRecords<'a> {
fn method(&self) -> Method {
Method::GET
}
fn path(&self) -> String {
format!("zones/{}/dns_records", self.zone_identifier)
}
fn query(&self) -> Option<ListDnsRecordsParams> {
Some(self.params.clone())
#[inline]
fn query(&self) -> Option<String> {
serialize_query(&self.params)
}
}

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

impl<'a> Endpoint<DnsRecord, (), CreateDnsRecordParams<'a>> for CreateDnsRecord<'a> {
impl<'a> EndpointSpec<DnsRecord> for CreateDnsRecord<'a> {
fn method(&self) -> Method {
Method::POST
}
fn path(&self) -> String {
format!("zones/{}/dns_records", self.zone_identifier)
}
fn body(&self) -> Option<CreateDnsRecordParams<'a>> {
Some(self.params.clone())
#[inline]
fn body(&self) -> Option<String> {
let body = serde_json::to_string(&self.params).unwrap();
Some(body)
}
}

Expand Down Expand Up @@ -72,7 +75,7 @@ pub struct DeleteDnsRecord<'a> {
pub zone_identifier: &'a str,
pub identifier: &'a str,
}
impl<'a> Endpoint<DeleteDnsRecordResponse> for DeleteDnsRecord<'a> {
impl<'a> EndpointSpec<DeleteDnsRecordResponse> for DeleteDnsRecord<'a> {
fn method(&self) -> Method {
Method::DELETE
}
Expand All @@ -93,7 +96,7 @@ pub struct UpdateDnsRecord<'a> {
pub params: UpdateDnsRecordParams<'a>,
}

impl<'a> Endpoint<DnsRecord, (), UpdateDnsRecordParams<'a>> for UpdateDnsRecord<'a> {
impl<'a> EndpointSpec<DnsRecord> for UpdateDnsRecord<'a> {
fn method(&self) -> Method {
Method::PUT
}
Expand All @@ -103,8 +106,10 @@ impl<'a> Endpoint<DnsRecord, (), UpdateDnsRecordParams<'a>> for UpdateDnsRecord<
self.zone_identifier, self.identifier
)
}
fn body(&self) -> Option<UpdateDnsRecordParams<'a>> {
Some(self.params.clone())
#[inline]
fn body(&self) -> Option<String> {
let body = serde_json::to_string(&self.params).unwrap();
Some(body)
}
}

Expand Down
10 changes: 6 additions & 4 deletions cloudflare/src/endpoints/load_balancing/create_lb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::endpoints::load_balancing::{
LbPoolId, LbPoolMapping, LoadBalancer, SessionAffinity, SessionAffinityAttributes,
SteeringPolicy,
};
use crate::framework::endpoint::{Endpoint, Method};
use crate::framework::endpoint::{EndpointSpec, Method};

use serde::Serialize;

Expand Down Expand Up @@ -55,14 +55,16 @@ pub struct OptionalParams<'a> {
pub session_affinity_ttl: Option<u32>,
}

impl<'a> Endpoint<LoadBalancer, (), Params<'a>> for CreateLoadBalancer<'a> {
impl<'a> EndpointSpec<LoadBalancer> for CreateLoadBalancer<'a> {
fn method(&self) -> Method {
Method::POST
}
fn path(&self) -> String {
format!("zones/{}/load_balancers", self.zone_identifier)
}
fn body(&self) -> Option<Params<'a>> {
Some(self.params.clone())
#[inline]
fn body(&self) -> Option<String> {
let body = serde_json::to_string(&self.params).unwrap();
Some(body)
}
}
10 changes: 6 additions & 4 deletions cloudflare/src/endpoints/load_balancing/create_pool.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::endpoints::load_balancing::{Origin, Pool};
use crate::framework::endpoint::{Endpoint, Method};
use crate::framework::endpoint::{EndpointSpec, Method};

use serde::Serialize;

Expand Down Expand Up @@ -51,14 +51,16 @@ pub struct OptionalParams<'a> {
pub notification_email: Option<&'a str>,
}

impl<'a> Endpoint<Pool, (), Params<'a>> for CreatePool<'a> {
impl<'a> EndpointSpec<Pool> for CreatePool<'a> {
fn method(&self) -> Method {
Method::POST
}
fn path(&self) -> String {
format!("accounts/{}/load_balancers/pools", self.account_identifier)
}
fn body(&self) -> Option<Params<'a>> {
Some(self.params.clone())
#[inline]
fn body(&self) -> Option<String> {
let body = serde_json::to_string(&self.params).unwrap();
Some(body)
}
}
4 changes: 2 additions & 2 deletions cloudflare/src/endpoints/load_balancing/delete_lb.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::framework::endpoint::{Endpoint, Method};
use crate::framework::endpoint::{EndpointSpec, Method};
use crate::framework::response::ApiResult;

use serde::Deserialize;
Expand All @@ -13,7 +13,7 @@ pub struct DeleteLoadBalancer<'a> {
pub identifier: &'a str,
}

impl<'a> Endpoint<Response, (), ()> for DeleteLoadBalancer<'a> {
impl<'a> EndpointSpec<Response> for DeleteLoadBalancer<'a> {
fn method(&self) -> Method {
Method::DELETE
}
Expand Down
4 changes: 2 additions & 2 deletions cloudflare/src/endpoints/load_balancing/delete_pool.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::framework::endpoint::{Endpoint, Method};
use crate::framework::endpoint::{EndpointSpec, Method};
use crate::framework::response::ApiResult;

use serde::Deserialize;
Expand All @@ -13,7 +13,7 @@ pub struct DeletePool<'a> {
pub identifier: &'a str,
}

impl<'a> Endpoint<Response, (), ()> for DeletePool<'a> {
impl<'a> EndpointSpec<Response> for DeletePool<'a> {
fn method(&self) -> Method {
Method::DELETE
}
Expand Down
4 changes: 2 additions & 2 deletions cloudflare/src/endpoints/load_balancing/list_lb.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::endpoints::load_balancing::LoadBalancer;
use crate::framework::endpoint::{Endpoint, Method};
use crate::framework::endpoint::{EndpointSpec, Method};
use crate::framework::response::ApiResult;

/// List Load Balancers
Expand All @@ -10,7 +10,7 @@ pub struct ListLoadBalancers<'a> {
pub zone_identifier: &'a str,
}

impl<'a> Endpoint<Vec<LoadBalancer>, ()> for ListLoadBalancers<'a> {
impl<'a> EndpointSpec<Vec<LoadBalancer>> for ListLoadBalancers<'a> {
fn method(&self) -> Method {
Method::GET
}
Expand Down
4 changes: 2 additions & 2 deletions cloudflare/src/endpoints/load_balancing/pool_details.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::endpoints::load_balancing::Pool;
use crate::framework::endpoint::{Endpoint, Method};
use crate::framework::endpoint::{EndpointSpec, Method};

/// Pool Details
/// <https://api.cloudflare.com/#account-load-balancer-pools-pool-details>
Expand All @@ -11,7 +11,7 @@ pub struct PoolDetails<'a> {
pub identifier: &'a str,
}

impl<'a> Endpoint<Pool, (), ()> for PoolDetails<'a> {
impl<'a> EndpointSpec<Pool> for PoolDetails<'a> {
fn method(&self) -> Method {
Method::GET
}
Expand Down
8 changes: 4 additions & 4 deletions cloudflare/src/endpoints/r2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use chrono::DateTime;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use crate::framework::endpoint::{Endpoint, Method};
use crate::framework::endpoint::{EndpointSpec, Method};
use crate::framework::response::ApiResult;

/// A Bucket is a collection of Objects stored in R2.
Expand Down Expand Up @@ -31,7 +31,7 @@ pub struct ListBuckets<'a> {
pub account_identifier: &'a str,
}

impl<'a> Endpoint<ListBucketsResult> for ListBuckets<'a> {
impl<'a> EndpointSpec<ListBucketsResult> for ListBuckets<'a> {
fn method(&self) -> Method {
Method::GET
}
Expand All @@ -49,7 +49,7 @@ pub struct CreateBucket<'a> {
pub bucket_name: &'a str,
}

impl<'a> Endpoint<EmptyMap> for CreateBucket<'a> {
impl<'a> EndpointSpec<EmptyMap> for CreateBucket<'a> {
fn method(&self) -> Method {
Method::PUT
}
Expand All @@ -68,7 +68,7 @@ pub struct DeleteBucket<'a> {
pub bucket_name: &'a str,
}

impl<'a> Endpoint<EmptyMap> for DeleteBucket<'a> {
impl<'a> EndpointSpec<EmptyMap> for DeleteBucket<'a> {
fn method(&self) -> Method {
Method::DELETE
}
Expand Down
Loading

0 comments on commit 0fbc167

Please sign in to comment.