Skip to content

Commit

Permalink
fix: add an optional return type in headers (#805)
Browse files Browse the repository at this point in the history
* fix: add an optional return type in headers

* add documentation
  • Loading branch information
sansyrox committed May 23, 2024
1 parent beca136 commit 7b18851
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,9 @@ Either, by using the `headers` field in the `Request` object:
headers = request.headers

print("These are the request headers: ", headers)
existing_header = headers.get("exisiting_header")
existing_header = headers.get("exisiting_header", "default_value")
exisiting_header = headers["exisiting_header"] # This syntax is also valid

headers.set("modified", "modified_value")
headers["new_header"] = "new_value" # This syntax is also valid
Expand All @@ -563,8 +566,12 @@ Either, by using the `headers` field in the `Request` object:
headers = request.headers

print("These are the request headers: ", headers)
existing_header = headers.get("exisiting_header")
existing_header = headers.get("exisiting_header", "default_value")
exisiting_header = headers["exisiting_header"] # This syntax is also valid

headers.set("modified", "modified_value")
headers["new_header"] = "new_value" # This syntax is also valid

print("These are the modified request headers: ", headers)

Expand Down
3 changes: 3 additions & 0 deletions integration_tests/base_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ def global_after_request(response: Response):

@app.get("/sync/global/middlewares")
def sync_global_middlewares(request: Request):
print(request.headers)
print(request.headers.get("txt"))
print(request.headers["txt"])
assert "global_before" in request.headers
assert request.headers.get("global_before") == "global_before_request"
return "sync global middlewares"
Expand Down
11 changes: 4 additions & 7 deletions src/types/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,15 @@ impl Headers {
}
}

pub fn get(&self, key: String) -> PyResult<String> {
pub fn get(&self, key: String) -> Option<String> {
// return the last value
match self.headers.get(&key.to_lowercase()) {
Some(iter) => {
let (_, values) = iter.pair();
let last_value = values.last().unwrap();
Ok(last_value.to_string())
Some(last_value.to_string())
}
None => Err(pyo3::exceptions::PyKeyError::new_err(format!(
"KeyError: {}",
key
))),
None => None,
}
}

Expand Down Expand Up @@ -151,7 +148,7 @@ impl Headers {
self.set(key, value);
}

pub fn __getitem__(&self, key: String) -> PyResult<String> {
pub fn __getitem__(&self, key: String) -> Option<String> {
self.get(key)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl Request {
let body: Vec<u8> = if headers.contains(String::from("content-type"))
&& headers
.get(String::from("content-type"))
.is_ok_and(|val| val.contains("multipart/form-data"))
.is_some_and(|val| val.contains("multipart/form-data"))
{
let h = headers.get(String::from("content-type")).unwrap();
debug!("Content-Type: {:?}", h);
Expand Down

0 comments on commit 7b18851

Please sign in to comment.