paypal_rust/client/
endpoint.rs

1use std::borrow::Cow;
2use std::fmt::Debug;
3
4use reqwest::Url;
5use serde::de::DeserializeOwned;
6use serde::{Deserialize, Serialize};
7
8use crate::client::auth::AuthStrategy;
9use crate::client::paypal::Environment;
10use crate::client::request::{HttpRequestHeaders, RequestStrategy, RequestUrl};
11
12pub trait Endpoint: Send + Sync {
13    /// The query parameters the endpoint accepts.
14    type QueryParams: Serialize;
15    /// The request body the endpoint accepts.
16    type RequestBody: Serialize;
17    /// The response body the endpoint returns.
18    type ResponseBody: DeserializeOwned + Debug;
19
20    /// The path of the endpoint. Not including the base URL.
21    fn path(&self) -> Cow<str>;
22
23    /// The headers to send with the request.
24    fn headers(&self) -> HttpRequestHeaders {
25        HttpRequestHeaders::default()
26    }
27
28    /// The query parameters to send with the request.
29    fn query(&self) -> Option<Self::QueryParams> {
30        None
31    }
32
33    /// The request body to send with the request.
34    fn request_body(&self) -> Option<Self::RequestBody> {
35        None
36    }
37
38    /// The HTTP method to use for the request.
39    fn request_method(&self) -> reqwest::Method {
40        reqwest::Method::GET
41    }
42
43    /// The request strategy to use.
44    fn request_strategy(&self) -> RequestStrategy {
45        RequestStrategy::default()
46    }
47
48    /// The authorization strategy to use.
49    fn auth_strategy(&self) -> AuthStrategy {
50        AuthStrategy::default()
51    }
52
53    /// The URL to send the request to. DO NOT OVERRIDE THIS METHOD.
54    fn request_url(&self, environment: Environment) -> Url {
55        let path = self.path();
56        let path = path.strip_prefix('/').unwrap_or(&path);
57
58        let mut request_url = match environment {
59            // Expects are fine here, as we can be sure that the URL is valid.
60            Environment::Sandbox => RequestUrl::Sandbox.as_url().expect("Invalid URL"),
61            Environment::Live => RequestUrl::Live.as_url().expect("Invalid URL"),
62        };
63
64        request_url.set_path(path);
65        request_url
66    }
67}
68
69#[derive(Debug, Copy, Clone, Deserialize, Default)]
70pub struct EmptyResponseBody {}