File tree Expand file tree Collapse file tree 5 files changed +79
-1
lines changed Expand file tree Collapse file tree 5 files changed +79
-1
lines changed Original file line number Diff line number Diff line change 1
1
# Generated by Cargo
2
2
# will have compiled files and executables
3
- /target /
3
+ /target
4
4
5
5
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
6
6
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Original file line number Diff line number Diff line change
1
+ [package ]
2
+ name = " postgrest"
3
+ version = " 0.1.0"
4
+ authors = [
" Bobbie Soedirgo <[email protected] >" ]
5
+ edition = " 2018"
6
+
7
+ [dependencies ]
8
+ reqwest = { version = " 0.10" , features = [" json" ] }
9
+ tokio = { version = " 0.2" , features = [" full" ] }
Original file line number Diff line number Diff line change 1
1
# postgrest-rs
2
2
PostgREST client-side library
3
+
4
+ ## Usage
5
+
6
+ ### Run Example
7
+
8
+ ``` sh
9
+ cargo run --example test
10
+ ```
Original file line number Diff line number Diff line change
1
+ use postgrest:: PostgrestClient ;
2
+
3
+ #[ tokio:: main]
4
+ async fn main ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
5
+ let client = PostgrestClient :: new ( "https://hacks.soedirgo.dev/postgrest" ) ;
6
+ let resp = client
7
+ . from ( "todos" )
8
+ . select ( "*" )
9
+ . execute ( )
10
+ . await ?;
11
+ println ! ( "{}" , resp) ;
12
+ Ok ( ( ) )
13
+ }
Original file line number Diff line number Diff line change
1
+ use reqwest:: Method ;
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
+ }
30
+
31
+ pub struct PostgrestClient {
32
+ rest_url : String ,
33
+ }
34
+
35
+ impl PostgrestClient {
36
+ pub fn new ( rest_url : & str ) -> PostgrestClient {
37
+ PostgrestClient {
38
+ rest_url : rest_url. to_owned ( ) ,
39
+ }
40
+ }
41
+
42
+ pub fn from ( & self , table : & str ) -> Request {
43
+ let mut url = self . rest_url . clone ( ) ;
44
+ url. push ( '/' ) ;
45
+ url. push_str ( table) ;
46
+ Request :: new ( & url)
47
+ }
48
+ }
You can’t perform that action at this time.
0 commit comments