op_api_sdk/
options.rs

1//! This module contains Options for the clients needed
2//! to make requests to [OP API](https://op-developer.fi).
3
4/// Struct containing needed options for API clients.
5#[derive(Default, Clone)]
6pub struct Options {
7    api_key: String,
8    authorization: String,
9    version: String,
10    base_url: String,
11}
12
13impl Options {
14    /// Creates new Options struct for production access.
15    ///
16    /// Keep in mind that authorization must be feched from the
17    /// OAuth and passed here without 'Bearer' included.
18    pub fn new(api_key: String, authorization: String) -> Options {
19        Options {
20            api_key,
21            authorization,
22            version: String::from("v1"),
23            base_url: String::from("https://prod.apis.op-palvelut.fi/"),
24        }
25    }
26
27    /// Creates new Options struct for sandbox access.
28    ///
29    /// Authorization for this is not required as it uses one of the
30    /// predefined authorization keys in https://op-developer.fi/docs
31    pub fn new_dev(api_key: String) -> Options {
32        Options {
33            api_key,
34            authorization: String::from("b6910384440ce06f495976f96a162e2ab1bafbb4"),
35            version: String::from("v1"),
36            base_url: String::from("https://sandbox.apis.op-palvelut.fi/"),
37        }
38    }
39
40    /// Gets base URL for requests.
41    ///
42    /// This is different for production and sandbox environments.
43    pub fn base_url(&self) -> &str {
44        &self.base_url
45    }
46
47    /// Sets base URL for all API requests.
48    ///
49    /// This is useful when testing the clients and can be used
50    /// together with local test or mock server.
51    pub fn with_base_url(mut self, base_url: String) -> Self {
52        self.base_url = base_url;
53        self
54    }
55
56    /// Gets API key for requests.
57    ///
58    /// This is used as HTTP header x-api-key.
59    pub fn api_key(&self) -> &str {
60        &self.api_key
61    }
62
63    /// Sets API key for API requests using these options.
64    pub fn with_api_key(mut self, api_key: String) -> Self {
65        self.api_key = api_key;
66        self
67    }
68
69    /// Gets Authorization for requests.
70    ///
71    /// This is used together with 'Bearer ' in the Authorization HTTP header.
72    pub fn authorization(&self) -> &str {
73        &self.authorization
74    }
75
76    /// Sets authorization header value for API requests using these options.
77    ///
78    /// The authorization should not contain 'Bearer ' in front of the
79    /// authorization key as it will be automatically preprended by the
80    /// clients.
81    pub fn with_authorization(mut self, authorization: String) -> Self {
82        self.authorization = authorization;
83        self
84    }
85
86    /// Sets API version for requests.
87    ///
88    /// Some APIs are on different version (AccountsV3 for example).
89    /// This is information is used to construct the request URL.
90    pub fn with_version(mut self, version: String) -> Self {
91        self.version = version;
92        self
93    }
94
95    /// Returns API version for requests.
96    pub fn version(&self) -> &str {
97        &self.version
98    }
99}