1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use crate::client::auth::AuthStrategy;
use crate::client::paypal::Environment;
use crate::client::request::{HttpRequestHeaders, RequestStrategy, RequestUrl};
use reqwest::Url;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::fmt::Debug;

pub trait Endpoint {
    /// The query parameters the endpoint accepts.
    type QueryParams: Serialize;
    /// The request body the endpoint accepts.
    type RequestBody: Serialize;
    /// The response body the endpoint returns.
    type ResponseBody: DeserializeOwned + Debug;

    /// The path of the endpoint. Not including the base URL.
    fn path(&self) -> Cow<str>;

    /// The headers to send with the request.
    fn headers(&self) -> HttpRequestHeaders {
        HttpRequestHeaders::default()
    }

    /// The query parameters to send with the request.
    fn query(&self) -> Option<Self::QueryParams> {
        None
    }

    /// The request body to send with the request.
    fn request_body(&self) -> Option<Self::RequestBody> {
        None
    }

    /// The HTTP method to use for the request.
    fn request_method(&self) -> reqwest::Method {
        reqwest::Method::GET
    }

    /// The request strategy to use.
    fn request_strategy(&self) -> RequestStrategy {
        RequestStrategy::default()
    }

    /// The authorization strategy to use.
    fn auth_strategy(&self) -> AuthStrategy {
        AuthStrategy::default()
    }

    /// The URL to send the request to. DO NOT OVERRIDE THIS METHOD.
    fn request_url(&self, environment: Environment) -> Url {
        let path = self.path();
        let path = path.strip_prefix('/').unwrap_or(&path);

        let mut request_url = match environment {
            // Expects are fine here, as we can be sure that the URL is valid.
            Environment::Sandbox => RequestUrl::Sandbox.as_url().expect("Invalid URL"),
            Environment::Live => RequestUrl::Live.as_url().expect("Invalid URL"),
        };

        request_url.set_path(path);
        request_url
    }
}

#[derive(Debug, Copy, Clone, Deserialize, Default)]
pub struct EmptyResponseBody {}