1mod models;
2
3use reqwest::Client;
4use serde_derive::{Deserialize, Serialize};
5use std::error::Error;
6use std::collections::HashMap;
7
8
9use crate::models::organization::{Organization, OrganizationCreate};
10
11#[derive(Serialize, Deserialize)]
13pub struct Resource {
14 }
16
17const ORGANIZATION_URL: &str = "/organization";
19const BASE_ADDRESS: &str = "http://localhost:8080";
23const BASE_ADDRESS_SANDBOX: &str = "https://sandbox.metriport.com";
24
25pub struct MetriportSDK {
26 client: Client,
27 base_url: String,
28 api_key: String,
29}
30
31#[allow(dead_code)]
32pub struct Options {
33 timeout: Option<u64>,
34 additional_headers: Option<HashMap<String, String>>,
35 sandbox: Option<bool>,
36 base_address: Option<String>,
37}
38
39impl MetriportSDK {
40 pub fn new(api_key: String, options: Options) -> Self {
41 let base_url = match (options.sandbox, options.base_address) {
43 (Some(true), _) => String::from(BASE_ADDRESS_SANDBOX),
44 (_, Some(address)) => address,
45 _ => String::from(BASE_ADDRESS),
46 };
47
48 Self {
49 client: Client::new(),
50 base_url,
51 api_key,
52 }
53 }
54
55 pub async fn create_organization(&self, data: OrganizationCreate) -> Result<Organization, Box<dyn Error>> {
56 let url = format!("{}{}", self.base_url, ORGANIZATION_URL);
57 let response: reqwest::Response = self
58 .client
59 .post(&url)
60 .header("Authorization", format!("Bearer {}", self.api_key))
61 .json(&data)
62 .send()
63 .await?;
64
65 if response.status().is_success() {
66 let organization: Organization = response.json().await?;
67 Ok(organization)
68 } else {
69 Err(Box::new(std::io::Error::new(
70 std::io::ErrorKind::Other,
71 "Failed to create organization",
72 )))
73 }
74 }
75}