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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use crate::{http, common::Method, error::HostError};
pub struct Supabase {
url: String,
headers: Vec<(String, String)>,
}
impl Supabase {
pub fn new<T: Into<String>>(url: T) -> Self {
Self {
url: url.into(),
headers: Vec::new(),
}
}
pub fn insert_header<T, U>(mut self, header: T, value: U) -> Self where T: Into<String>, U: Into<String> {
self.headers.push((header.into(), value.into()));
self
}
pub fn from<T: Into<String>>(&self, url: T) -> Builder {
Builder {
client: self,
url: url.into(),
method: Method::GET,
body: None,
headers: self.headers.clone(),
queries: Vec::new(),
}
}
}
pub struct Builder<'a> {
client: &'a Supabase,
url: String,
method: Method,
body: Option<String>,
headers: Vec<(String, String)>,
queries: Vec<(String, String)>,
}
impl Builder<'_> {
pub fn auth<T: Into<String>>(mut self, token: T) -> Self {
self.headers.push((
String::from("Authorization"),
format!("Bearer {}", token.into()),
));
self
}
pub fn select<T: Into<String>>(mut self, columns: T) -> Self {
self.queries.push((String::from("select"), columns.into()));
self
}
pub fn order<T: Into<String>>(mut self, columns: T) -> Self {
self.queries.push((String::from("order"), columns.into()));
self
}
pub fn limit(mut self, count: usize) -> Self {
self.headers
.push((String::from("Range-Unit"), String::from("items")));
self.headers
.push((String::from("Range"), format!("0-{}", count - 1)));
self
}
pub fn range(mut self, low: usize, high: usize) -> Self {
self.headers
.push((String::from("Range-Unit"), String::from("items")));
self.headers
.push((String::from("Range"), format!("{low}-{high}")));
self
}
fn count(mut self, method: &str) -> Self {
self.headers
.push((String::from("Range-Unit"), String::from("items")));
self.headers.push((String::from("Range"), String::from("0-0")));
self.headers
.push((String::from("Insert"), format!("count={method}")));
self
}
pub fn exact_count(self) -> Self {
self.count("exact")
}
pub fn planned_count(self) -> Self {
self.count("planned")
}
pub fn estimated_count(self) -> Self {
self.count("estimated")
}
pub fn single(mut self) -> Self {
self.headers.push((
String::from("Accept"),
String::from("application/vnd.pgrst.object+json"),
));
self
}
pub fn insert<T: Into<String>>(mut self, body: T) -> Self {
self.method = Method::POST;
self.headers.push((String::from("Prefer"), String::from("return=representation")));
self.body = Some(body.into());
self
}
pub fn upsert<T: Into<String>>(mut self, body: T) -> Self {
self.method = Method::POST;
self.headers.push((String::from("Prefer"), String::from("return=representation,resolution=merge-duplicates")));
self.body = Some(body.into());
self
}
pub fn on_conflict<T: Into<String>>(mut self, columns: T) -> Self {
self.queries.push((String::from("on_conflict"), columns.into()));
self
}
pub fn update<T: Into<String>>(mut self, body: T) -> Self {
self.method = Method::PATCH;
self.headers.push((String::from("Prefer"), String::from("return=representation")));
self.body = Some(body.into());
self
}
pub fn delete(mut self) -> Self {
self.method = Method::DELETE;
self.headers.push((String::from("Prefer"), String::from("return=representation")));
self
}
pub fn not<T, U, V>(mut self, operator: T, column: U, filter: V) -> Self
where
T: Into<String>,
U: Into<String>,
V: Into<String>,
{
self.queries.push((
column.into(),
format!("not.{}.{}", operator.into(), filter.into()),
));
self
}
pub fn execute(self) -> Result<http::Response, HostError> {
let url = format!("{}/rest/v1/{}", self.client.url, self.url);
let mut request = http::Request::new(url, self.method);
for (header, value) in self.headers {
request = request.set(header, value);
}
for (param, value) in self.queries {
request = request.query(param, value);
}
match self.body {
Some(body) => request.send_string(body),
None => request.call(),
}
}
}