Skip to content

Commit a36ef35

Browse files
committed
Implement running (?) example
postgrest::PostgrestClient::new("<postgrest url>") .from("todos") .select("id") .execute() .await?; It works (?) 🎉
1 parent 2e11c32 commit a36ef35

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-1
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Generated by Cargo
22
# will have compiled files and executables
3-
/target/
3+
/target
44

55
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
66
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html

Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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"] }

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
# postgrest-rs
22
PostgREST client-side library
3+
4+
## Usage
5+
6+
### Run Example
7+
8+
``` sh
9+
cargo run --example test
10+
```

examples/test.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
}

src/lib.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
}

0 commit comments

Comments
 (0)