This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add idempotency to publish newsletter endpoint
- Loading branch information
Showing
13 changed files
with
368 additions
and
8 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
.sqlx/query-429f0897a3c43dca32af247947c1ddd6d6ddb3240e39400ebd68f60cc6f07bbd.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
58 changes: 58 additions & 0 deletions
58
.sqlx/query-a00b9ba5641078266d469ca4374e636dab1e6741441501f384a1a94f53c70489.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
CREATE TYPE header_pair AS ( | ||
name TEXT, | ||
value BYTEA | ||
); | ||
|
||
CREATE TABLE idempotency ( | ||
user_id uuid NOT NULL REFERENCES users(user_id), | ||
idempotency_key TEXT NOT NULL, | ||
response_status_code SMALLINT NOT NULL, | ||
response_headers header_pair[] NOT NULL, | ||
response_body BYTEA NOT NULL, | ||
created_at timestamptz NOT NULL, | ||
PRIMARY KEY(user_id, idempotency_key) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#[derive(Debug)] | ||
pub struct IdempotencyKey(String); | ||
|
||
impl TryFrom<String> for IdempotencyKey { | ||
type Error = anyhow::Error; | ||
|
||
fn try_from(s: String) -> Result<Self, Self::Error> { | ||
if s.is_empty() { | ||
anyhow::bail!("The idempotency key cannot be empty"); | ||
} | ||
|
||
let max_len = 50; | ||
if s.len() >= max_len { | ||
anyhow::bail!("The idempotency key must be shorter than {max_len} characters") | ||
} | ||
|
||
Ok(Self(s)) | ||
} | ||
} | ||
|
||
impl From<IdempotencyKey> for String { | ||
fn from(k: IdempotencyKey) -> Self { | ||
k.0 | ||
} | ||
} | ||
|
||
impl AsRef<str> for IdempotencyKey { | ||
fn as_ref(&self) -> &str { | ||
&self.0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mod key; | ||
mod persistence; | ||
|
||
pub use key::IdempotencyKey; | ||
pub use persistence::{get_saved_response, save_response}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
use super::IdempotencyKey; | ||
use axum::{ | ||
body::{to_bytes, Body}, | ||
http::{Response, StatusCode}, | ||
}; | ||
use sqlx::{postgres::PgHasArrayType, PgPool}; | ||
use uuid::Uuid; | ||
|
||
#[tracing::instrument(skip(db_pool, idempotency_key, user_id))] | ||
pub async fn get_saved_response( | ||
db_pool: &PgPool, | ||
idempotency_key: &IdempotencyKey, | ||
user_id: Uuid, | ||
) -> Result<Option<Response<Body>>, anyhow::Error> { | ||
let saved_response = sqlx::query!( | ||
r#" | ||
SELECT | ||
response_status_code, | ||
response_headers AS "response_headers: Vec<HeaderPairRecord>", | ||
response_body | ||
FROM idempotency | ||
WHERE | ||
idempotency_key = $1 AND | ||
user_id = $2 | ||
"#, | ||
idempotency_key.as_ref(), | ||
user_id | ||
) | ||
.fetch_optional(db_pool) | ||
.await?; | ||
|
||
if let Some(r) = saved_response { | ||
let status_code = StatusCode::from_u16(r.response_status_code.try_into()?)?; | ||
let mut response = Response::builder().status(status_code); | ||
for header in r.response_headers { | ||
response = response.header(header.name, header.value); | ||
} | ||
Ok(Some(response.body(Body::from(r.response_body))?)) | ||
} else { | ||
Ok(None) | ||
} | ||
} | ||
|
||
#[tracing::instrument(skip(db_pool, idempotency_key, user_id, response))] | ||
pub async fn save_response( | ||
db_pool: &PgPool, | ||
idempotency_key: &IdempotencyKey, | ||
user_id: Uuid, | ||
response: Response<Body>, | ||
) -> Result<Response<Body>, anyhow::Error> { | ||
let status_code = response.status().as_u16() as i16; | ||
let headers = { | ||
let mut h = Vec::with_capacity(response.headers().len()); | ||
for (name, value) in response.headers() { | ||
let name = name.to_string(); | ||
let value = value.as_bytes().to_owned(); | ||
h.push(HeaderPairRecord { name, value }); | ||
} | ||
h | ||
}; | ||
let (parts, body) = response.into_parts(); | ||
let body = to_bytes(body, usize::MAX).await?; | ||
|
||
sqlx::query_unchecked!( | ||
r#" | ||
INSERT INTO idempotency ( | ||
user_id, | ||
idempotency_key, | ||
response_status_code, | ||
response_headers, | ||
response_body, | ||
created_at | ||
) | ||
VALUES ($1, $2, $3, $4, $5, now()) | ||
"#, | ||
user_id, | ||
idempotency_key.as_ref(), | ||
status_code, | ||
headers, | ||
body.as_ref() | ||
) | ||
.execute(db_pool) | ||
.await?; | ||
|
||
let response = Response::from_parts(parts, Body::from(body)); | ||
Ok(response) | ||
} | ||
|
||
#[derive(Debug, sqlx::Type)] | ||
#[sqlx(type_name = "header_pair")] | ||
struct HeaderPairRecord { | ||
name: String, | ||
value: Vec<u8>, | ||
} | ||
|
||
impl PgHasArrayType for HeaderPairRecord { | ||
fn array_type_info() -> sqlx::postgres::PgTypeInfo { | ||
sqlx::postgres::PgTypeInfo::with_name("_header_pair") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.