-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from cloudflare/achalmers/23
fix #23: separate modules for endpoints and framework
- Loading branch information
Showing
21 changed files
with
205 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
Every PR should have a corresponding issue, and the issue number should appear in the PR's description. | ||
|
||
PRs should be squashed to one commit before merging. | ||
|
||
There are two root-level modules in `cloudflare-rs`. Most PRs will only touch one of them. If you're | ||
working on the API framework itself, all the relevant code lives under the `framework/` | ||
module. If you're looking to add or edit an endpoint, read on. | ||
|
||
# Adding New Endpoints | ||
|
||
Every Cloudflare product should have its own directory under `endpoints/`. For example, all the | ||
DNS endpoints live under `endpoints/dns`. | ||
|
||
If your product's module gets big enough, we suggest structuring it like so: | ||
|
||
``` | ||
src/ | ||
endpoints/ | ||
myproduct/ | ||
data_structures.rs | ||
endpoint_a.rs | ||
endpoint_b.rs | ||
mod.rs | ||
``` | ||
|
||
In this structure, every endpoint gets its own module, which includes | ||
|
||
* Endpoint struct | ||
* Request struct | ||
* Response struct (if necessary) | ||
* Params struct (if necessary) | ||
|
||
Common data structures which are used in multiple endpoints should be put in `data_structures.rs`. | ||
`mod.rs` should then make all its submodules public, like so: | ||
|
||
```rust | ||
mod data_structures; | ||
mod endpoint_a; | ||
mod endpoint_b; | ||
|
||
pub use data_structures; | ||
pub use endpoint_a; | ||
pub use endpoint_b; | ||
``` | ||
|
||
## Documentation | ||
|
||
Endpoint structs should have a docstring with a link to the [Cloudflare API docs](https://api.cloudflare.com). | ||
|
||
Fields which represent endpoint parameters should be commented with their description in the | ||
Cloudflare API docs. | ||
|
||
If in doubt, follow the docstring structure for modules like `dns`. Ideally, someone reading your | ||
endpoint code shouldn't need to open up api.cloudflare.com for documentation. Your comments should | ||
be documentation enough. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/*! | ||
Implementations of the Endpoint trait for individual Cloudflare API endpoints, e.g. DNS or Workers. | ||
If you want to add a new Cloudflare API to this crate, simply add a new submodule of this `endpoints` | ||
module. | ||
*/ | ||
pub mod account; | ||
pub mod dns; | ||
pub mod plan; | ||
pub mod workerskv; | ||
pub mod zone; |
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
src/workerskv/create_namespace.rs → src/endpoints/workerskv/create_namespace.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/workerskv/list_namespace_keys.rs → ...ndpoints/workerskv/list_namespace_keys.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/workerskv/list_namespaces.rs → src/endpoints/workerskv/list_namespaces.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/workerskv/remove_namespace.rs → src/endpoints/workerskv/remove_namespace.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/workerskv/rename_namespace.rs → src/endpoints/workerskv/rename_namespace.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/*! | ||
This module controls how requests are sent to Cloudflare's API, and how responses are parsed from it. | ||
*/ | ||
pub mod apiclient; | ||
pub mod auth; | ||
pub mod endpoint; | ||
pub mod mock; | ||
pub mod response; | ||
|
||
use crate::framework::{ | ||
apiclient::ApiClient, auth::AuthClient, endpoint::Method, response::map_api_response, | ||
}; | ||
use serde::Serialize; | ||
|
||
#[derive(Serialize, Clone, Debug)] | ||
pub enum OrderDirection { | ||
#[serde(rename = "asc")] | ||
Ascending, | ||
#[serde(rename = "desc")] | ||
Descending, | ||
} | ||
|
||
#[derive(Serialize, Clone, Debug)] | ||
#[serde(rename_all = "lowercase")] | ||
pub enum SearchMatch { | ||
All, | ||
Any, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum Environment { | ||
Production, | ||
} | ||
|
||
impl<'a> From<&'a Environment> for url::Url { | ||
fn from(environment: &Environment) -> Self { | ||
match environment { | ||
Environment::Production => { | ||
url::Url::parse("https://api.cloudflare.com/client/v4/").unwrap() | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub struct HttpApiClient { | ||
environment: Environment, | ||
credentials: auth::Credentials, | ||
http_client: reqwest::Client, | ||
} | ||
|
||
impl HttpApiClient { | ||
pub fn new(credentials: auth::Credentials) -> HttpApiClient { | ||
HttpApiClient { | ||
environment: Environment::Production, | ||
credentials, | ||
http_client: reqwest::Client::new(), | ||
} | ||
} | ||
} | ||
|
||
// TODO: This should probably just implement request for the Reqwest client itself :) | ||
// TODO: It should also probably be called `ReqwestApiClient` rather than `HttpApiClient`. | ||
impl<'a> ApiClient for HttpApiClient { | ||
fn request<ResultType, QueryType, BodyType>( | ||
&self, | ||
endpoint: &dyn endpoint::Endpoint<ResultType, QueryType, BodyType>, | ||
) -> response::ApiResponse<ResultType> | ||
where | ||
ResultType: response::ApiResult, | ||
QueryType: Serialize, | ||
BodyType: Serialize, | ||
{ | ||
fn match_reqwest_method(method: Method) -> reqwest::Method { | ||
match method { | ||
Method::Get => reqwest::Method::GET, | ||
Method::Post => reqwest::Method::POST, | ||
Method::Delete => reqwest::Method::DELETE, | ||
Method::Put => reqwest::Method::PUT, | ||
Method::Patch => reqwest::Method::PATCH, | ||
} | ||
} | ||
|
||
// Build the request | ||
let mut request = self | ||
.http_client | ||
.request( | ||
match_reqwest_method(endpoint.method()), | ||
endpoint.url(&self.environment), | ||
) | ||
.query(&endpoint.query()); | ||
|
||
if let Some(body) = endpoint.body() { | ||
request = request.body(serde_json::to_string(&body).unwrap()); | ||
request = request.header(reqwest::header::CONTENT_TYPE, endpoint.content_type()); | ||
} | ||
|
||
request = request.auth(&self.credentials); | ||
|
||
let response = request.send()?; | ||
|
||
map_api_response(response) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.