Skip to content

Commit c46f9bd

Browse files
authored
feat: Generic header handler (#26)
* Added a builder method to add arbitrary headers one by one. * Updated README with examples of auth() and insert_header() and section leading text about query building.
1 parent af5a9a4 commit c46f9bd

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,77 @@ let body = resp
3232
.await?;
3333
```
3434

35+
Simple example with JWT auth
36+
37+
```rust
38+
use postgrest::Postgrest;
39+
40+
let client = Postgrest::new("https://your.postgrest.endpoint");
41+
let resp = client
42+
.from("your_table")
43+
.auth("VerySensitiveJWTValueAsStringOrStr")
44+
.select("*")
45+
.execute()
46+
.await?;
47+
let body = resp
48+
.text()
49+
.await?;
50+
```
51+
52+
Simplified example using an authenticated API gateway and JWT authorization (like SupaBase).
53+
54+
```rust
55+
use postgrest::Postgrest;
56+
57+
static HEADER_KEY: &str = "apikey";
58+
let header_value: String = "ExampleAPIKeyValue".to_string(); // EXAMPLE ONLY!
59+
// Don't actually hard code this value, that's really bad. Use environment
60+
// variables like with the dotenv(https://crates.io/crates/dotenv) crate to inject
61+
62+
let client = Postgrest::new("https://your.supabase.endpoint/rest/v1/");
63+
let resp = client
64+
.from("your_table")
65+
.auth(header_value)
66+
.insert_header(HEADER_KEY, header_value)
67+
.select("*")
68+
.execute()
69+
.await?;
70+
let body = resp
71+
.text()
72+
.await?;
73+
74+
```
75+
76+
**Secure** example with authenticated API gateway and JWT authorization using the dotenv crate to correctly retrieve sensitive values.
77+
78+
```rust
79+
use postgrest::Postgrest;
80+
use dotenv;
81+
82+
dotenv().ok();
83+
84+
static HEADER_KEY: &str = "apikey";
85+
86+
let client = Postgrest::new("https://your.supabase.endpoint/rest/v1/");
87+
let resp = client
88+
.from("your_table")
89+
.auth(env::var("supabase_public_api_key").unwrap().to_string()))
90+
.insert_header(
91+
HEADER_KEY,
92+
env::var("supabase_public_api_key").unwrap().to_string())
93+
.select("*")
94+
.execute()
95+
.await?;
96+
let body = resp
97+
.text()
98+
.await?;
99+
100+
```
101+
102+
### Building Queries
103+
104+
These examples assume you've already initialized the client. The methods `.from()` and `.rpc()` initalizes the query builder inside the client.
105+
35106
Using filters:
36107

37108
```rust

src/builder.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,31 @@ impl Builder {
5959
self
6060
}
6161

62+
/// Add arbitrary headers to the request. For instance when you may want to connect
63+
/// through an api gateway that needs an API key header in addition to the JWT.
64+
///
65+
/// # Example
66+
///
67+
/// ```
68+
/// use postgrest::Postgrest;
69+
///
70+
/// static header_name: &'static str = "foo";
71+
/// let header_value: String = "bar".to_string();
72+
///
73+
/// let client = Postgrest::new("https://your.postgrest.endpoint/rest/v1/");
74+
/// client
75+
/// .from("table")
76+
/// .insert_header(header_name, header_value);
77+
/// ```
78+
pub fn insert_header(mut self, header_name: &'static str, header_value: String) -> Self {
79+
self.headers.insert(
80+
header_name,
81+
HeaderValue::from_str(&header_value)
82+
.expect("Couldn't convert supplied header value from String to str."),
83+
);
84+
self
85+
}
86+
6287
/// Performs horizontal filtering with SELECT.
6388
///
6489
/// # Note
@@ -469,6 +494,17 @@ mod tests {
469494
);
470495
}
471496

497+
#[test]
498+
fn with_insert_header() {
499+
static A_HEADER_KEY: &str = "foo";
500+
let a_header_value: String = "bar".to_string();
501+
let builder = Builder::new(TABLE_URL, None).insert_header(A_HEADER_KEY, a_header_value);
502+
assert_eq!(
503+
builder.headers.get("foo").unwrap(),
504+
HeaderValue::from_static("bar")
505+
);
506+
}
507+
472508
#[test]
473509
fn select_assert_query() {
474510
let builder = Builder::new(TABLE_URL, None).select("some_table");

0 commit comments

Comments
 (0)