Skip to content

Commit 828ed34

Browse files
committed
refactor: Always send the content-type header when sending JSON
1 parent 5df6d22 commit 828ed34

8 files changed

Lines changed: 42 additions & 9 deletions

File tree

activity/fly-http/impl/src/app.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::generated::exports::obelisk_flyio::activity_fly_http::apps;
2+
use crate::wstd_util::JsonRequest as _;
23
use crate::{API_BASE_URL, AppName, OrgSlug, request_with_api_token};
34
use anyhow::anyhow;
45
use serde::{Deserialize, Serialize};
@@ -43,7 +44,7 @@ async fn put(org_slug: OrgSlug, app_name: AppName) -> Result<apps::App, anyhow::
4344
let post_request = request_with_api_token()?
4445
.method(Method::POST)
4546
.uri(format!("{API_BASE_URL}/apps"))
46-
.body(Body::from_json(&request_body)?)?;
47+
.json(&request_body)?;
4748

4849
let mut response = client.send(post_request).await?;
4950

activity/fly-http/impl/src/ips.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::generated::exports::obelisk_flyio::activity_fly_http::ips::{
44
self, IpVariant, Ipv4Config, Ipv6Config,
55
};
66
use crate::generated::obelisk_flyio::activity_fly_http::regions::Region;
7+
use crate::wstd_util::JsonRequest as _;
78
use crate::{API_BASE_URL, AppName, request_with_api_token};
89
use anyhow::anyhow;
910
use serde::{Deserialize, Deserializer, Serialize};
@@ -45,8 +46,7 @@ async fn allocate_ip(app_name: &AppName, config: &IpVariant) -> Result<String, a
4546
let request = request_with_api_token()?
4647
.method(Method::POST)
4748
.uri(format!("{API_BASE_URL}/apps/{app_name}/ip_assignments"))
48-
.header("content-type", "application/json")
49-
.body(Body::from_json(&body)?)?;
49+
.json(&body)?;
5050

5151
let response = Client::new().send(request).await?;
5252
let resp_status = response.status();

activity/fly-http/impl/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ mod ips;
33
mod machine;
44
mod secret;
55
mod volume;
6+
mod wstd_util;
67
mod generated {
78
#![allow(clippy::empty_line_after_outer_attr)]
89
include!(concat!(env!("OUT_DIR"), "/generated.rs"));

activity/fly-http/impl/src/machine.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::generated::exports::obelisk_flyio::activity_fly_http::machines::{
22
ExecResponse, Guest, Machine, MachineConfig,
33
};
44
use crate::generated::obelisk_flyio::activity_fly_http::regions::Region;
5+
use crate::wstd_util::JsonRequest as _;
56
use crate::{API_BASE_URL, AppName, Component, MachineId, request_with_api_token};
67
use anyhow::{Context, anyhow, bail, ensure};
78
use ser::{
@@ -134,7 +135,7 @@ async fn create(
134135
let request = request_with_api_token()?
135136
.method(Method::POST)
136137
.uri(url)
137-
.body(Body::from_json(&request_payload)?)?;
138+
.json(&request_payload)?;
138139

139140
let response = Client::new().send(request).await?;
140141
let resp_status = response.status();
@@ -175,7 +176,7 @@ async fn update(
175176
let request = request_with_api_token()?
176177
.method(Method::POST)
177178
.uri(url)
178-
.body(Body::from_json(&request_payload)?)?;
179+
.json(&request_payload)?;
179180

180181
let response = Client::new().send(request).await?;
181182
let resp_status = response.status();
@@ -208,7 +209,7 @@ async fn exec(
208209
let request = request_with_api_token()?
209210
.method(Method::POST)
210211
.uri(url)
211-
.body(Body::from_json(&body)?)?;
212+
.json(&body)?;
212213
let response = Client::new().send(request).await?;
213214
let resp_status = response.status();
214215
let mut response = response.into_body();

activity/fly-http/impl/src/volume.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::generated::exports::obelisk_flyio::activity_fly_http::volumes::{
22
Volume, VolumeCreateRequest,
33
};
4+
use crate::wstd_util::JsonRequest as _;
45
use crate::{API_BASE_URL, AppName, Component, VolumeId, request_with_api_token};
56
use anyhow::{Context, anyhow};
67
use wstd::http::{Body, Client, Method};
@@ -31,7 +32,7 @@ async fn create(app_name: AppName, request: VolumeCreateRequest) -> Result<Volum
3132
let http_request = request_with_api_token()?
3233
.method(Method::POST)
3334
.uri(url)
34-
.body(Body::from_json(&request)?)?;
35+
.json(&request)?;
3536

3637
let response = Client::new().send(http_request).await?;
3738
let resp_status = response.status();
@@ -97,7 +98,7 @@ async fn extend(
9798
let request = request_with_api_token()?
9899
.method(Method::PUT)
99100
.uri(url)
100-
.body(Body::from_json(&body)?)?;
101+
.json(&body)?;
101102

102103
let response = Client::new().send(request).await?;
103104
let resp_status = response.status();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use wstd::http::{Body, Request, request};
2+
3+
pub trait JsonRequest {
4+
fn json<T: serde::Serialize>(self, value: &T) -> Result<Request<Body>, anyhow::Error>;
5+
}
6+
7+
impl JsonRequest for request::Builder {
8+
fn json<T: serde::Serialize>(self, value: &T) -> Result<Request<Body>, anyhow::Error> {
9+
Ok(self
10+
.header("content-type", "application/json")
11+
.body(Body::from_json(value)?)?)
12+
}
13+
}

webhook/fly-secrets-updater/impl/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
mod wstd_util;
2+
3+
use crate::wstd_util::JsonRequest as _;
14
use anyhow::{Context, anyhow};
25
use serde::{Deserialize, Serialize};
36
use wstd::http::body::Body;
@@ -31,7 +34,7 @@ async fn put_secret(
3134
.uri(format!(
3235
"{API_BASE_URL}/apps/{app_name}/secrets/{secret_name}"
3336
))
34-
.body(Body::from_json(&body)?)?;
37+
.json(&body)?;
3538

3639
let response = client.send(request).await?;
3740

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use wstd::http::{Body, Request, request};
2+
3+
pub trait JsonRequest {
4+
fn json<T: serde::Serialize>(self, value: &T) -> Result<Request<Body>, anyhow::Error>;
5+
}
6+
7+
impl JsonRequest for request::Builder {
8+
fn json<T: serde::Serialize>(self, value: &T) -> Result<Request<Body>, anyhow::Error> {
9+
Ok(self
10+
.header("content-type", "application/json")
11+
.body(Body::from_json(value)?)?)
12+
}
13+
}

0 commit comments

Comments
 (0)