File tree Expand file tree Collapse file tree 3 files changed +54
-40
lines changed Expand file tree Collapse file tree 3 files changed +54
-40
lines changed Original file line number Diff line number Diff line change 1
- use postgrest:: PostgrestClient ;
1
+ use postgrest:: Postgrest ;
2
2
3
3
#[ tokio:: main]
4
4
async fn main ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
5
- let client = PostgrestClient :: new ( "https://hacks.soedirgo.dev/postgrest" ) ;
5
+ let client = Postgrest :: new ( "https://hacks.soedirgo.dev/postgrest" ) ;
6
6
let resp = client
7
7
. from ( "todos" )
8
8
. select ( "*" )
9
9
. execute ( )
10
10
. await ?;
11
- println ! ( "{}" , resp) ;
11
+ println ! ( "{}" , resp. text ( ) . await ? ) ;
12
12
Ok ( ( ) )
13
13
}
Original file line number Diff line number Diff line change
1
+ use reqwest:: { Client , Error , Method , Response } ;
2
+
3
+ pub struct Builder {
4
+ method : Option < Method > ,
5
+ url : String ,
6
+ queries : Vec < ( String , String ) > ,
7
+ headers : Vec < ( String , String ) > ,
8
+ }
9
+
10
+ impl Builder {
11
+ pub fn new ( url : & str ) -> Self {
12
+ Builder {
13
+ method : None ,
14
+ url : url. to_string ( ) ,
15
+ queries : Vec :: new ( ) ,
16
+ headers : Vec :: new ( ) ,
17
+ }
18
+ }
19
+
20
+ pub fn select ( mut self , column : & str ) -> Self {
21
+ self . method = Some ( Method :: GET ) ;
22
+ self . queries . push ( ( "select" . to_string ( ) , column. to_string ( ) ) ) ;
23
+ self
24
+ }
25
+
26
+ pub async fn execute ( self ) -> Result < Response , Error > {
27
+ let mut req = Client :: new ( ) . request (
28
+ self . method . unwrap ( ) ,
29
+ & self . url ,
30
+ ) ;
31
+ for ( k, v) in & self . headers {
32
+ req = req. header ( k, v) ;
33
+ }
34
+ req = req. query ( & self . queries ) ;
35
+
36
+ let resp = req. send ( )
37
+ . await ?;
38
+
39
+ Ok ( resp)
40
+ }
41
+ }
Original file line number Diff line number Diff line change 1
- use reqwest :: Method ;
1
+ use builder :: Builder ;
2
2
3
- pub struct Request {
4
- method : Option < Method > ,
5
- url : String ,
6
- }
7
-
8
- impl Request {
9
- pub fn new ( url : & str ) -> Request {
10
- Request {
11
- method : None ,
12
- url : url. to_owned ( ) ,
13
- }
14
- }
15
-
16
- pub fn select ( mut self , column_query : & str ) -> Request {
17
- self . method = Some ( Method :: GET ) ;
18
- self . url . push_str ( & format ! ( "?select={}" , column_query) ) ;
19
- self
20
- }
21
-
22
- pub async fn execute ( self ) -> Result < String , Box < dyn std:: error:: Error > > {
23
- let resp = reqwest:: get ( & self . url )
24
- . await ?
25
- . text ( )
26
- . await ?;
27
- Ok ( resp)
28
- }
29
- }
3
+ mod builder;
30
4
31
- pub struct PostgrestClient {
5
+ pub struct Postgrest {
32
6
rest_url : String ,
33
7
}
34
8
35
- impl PostgrestClient {
36
- pub fn new ( rest_url : & str ) -> PostgrestClient {
37
- PostgrestClient {
38
- rest_url : rest_url. to_owned ( ) ,
9
+ impl Postgrest {
10
+ pub fn new ( rest_url : & str ) -> Postgrest {
11
+ Postgrest {
12
+ rest_url : rest_url. to_string ( ) ,
39
13
}
40
14
}
41
15
42
- pub fn from ( & self , table : & str ) -> Request {
16
+ pub fn from ( & self , table : & str ) -> Builder {
43
17
let mut url = self . rest_url . clone ( ) ;
44
- url. push ( '/' ) ;
45
- url. push_str ( table) ;
46
- Request :: new ( & url)
18
+ url = format ! ( "{}/{}" , url, table) ;
19
+ Builder :: new ( & url)
47
20
}
48
21
}
You can’t perform that action at this time.
0 commit comments