1use std::collections::HashMap;
2
3use reqwest::Url;
4use serde::Serialize;
5
6pub mod body;
7pub mod objects;
8pub struct Spotify {
9 pub authorization: String,
10}
11
12pub enum Method {
13 GET,
14 POST,
15 PUT,
16 DELETE,
17}
18
19impl PartialEq for Method {
20 fn eq(&self, other: &Self) -> bool {
21 self == other
22 }
23}
24
25impl Spotify {
26 pub fn new(authorization: &str) -> Self {
27 Spotify {
28 authorization: authorization.to_string(),
29 }
30 }
31
32 pub fn request<T>(
33 &self,
34 method: Method,
35 url: String,
36 query: Option<HashMap<&str, String>>,
37 body: Option<&T>,
38 ) -> Option<(String, bool)>
39 where
40 T: Serialize + ?Sized,
41 {
42 let url = {
43 if let Some(query) = query {
44 Url::parse_with_params(
45 format!("https://api.spotify.com/v1/{}", url).as_str(),
46 query,
47 )
48 .unwrap()
49 .to_string()
50 } else {
51 format!("https://api.spotify.com/v1/{}", url)
52 }
53 };
54
55 let client = reqwest::blocking::Client::new();
56
57 match method {
58 Method::GET => {
59 let response = client
60 .get(url)
61 .header("Accept", "application/json")
62 .header("Content-Type", "application/json")
63 .bearer_auth(self.authorization.as_str())
64 .send()
65 .unwrap();
66 if response.status().is_success() {
67 Some((response.text().unwrap(), true))
68 } else {
69 Some((response.text().unwrap(), false))
70 }
71 }
72 Method::POST => {
73 client
74 .post(url)
75 .bearer_auth(self.authorization.as_str())
76 .json(body.unwrap())
77 .send()
78 .unwrap();
79 None
80 }
81 Method::PUT => {
82 client
83 .put(url)
84 .bearer_auth(self.authorization.as_str())
85 .json(body.unwrap())
86 .send()
87 .unwrap();
88 None
89 }
90 Method::DELETE => {
91 client
92 .put(url)
93 .bearer_auth(self.authorization.as_str())
94 .send()
95 .unwrap();
96 None
97 }
98 }
99 }
100}