Skip to content

Commit f2f621a

Browse files
committed
feat(aggregator-client): send mithril api version header on all requests
1 parent f494875 commit f2f621a

File tree

1 file changed

+40
-2
lines changed
  • internal/mithril-aggregator-client/src

1 file changed

+40
-2
lines changed

internal/mithril-aggregator-client/src/client.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ impl AggregatorClient {
2929
}
3030

3131
pub async fn send<Q: AggregatorQuery>(&self, query: Q) -> AggregatorClientResult<Q::Response> {
32+
// Todo: error handling ? Reuse the version in `warn_if_api_version_mismatch` ?
33+
let current_api_version = self.api_version_provider.compute_current_version().unwrap();
3234
let mut request_builder = match Q::method() {
3335
QueryMethod::Get => self.client.get(self.join_aggregator_endpoint(&query.route())?),
3436
QueryMethod::Post => self.client.post(self.join_aggregator_endpoint(&query.route())?),
35-
};
37+
}
38+
.header(MITHRIL_API_VERSION_HEADER, current_api_version.to_string());
3639

3740
if let Some(body) = query.body() {
3841
request_builder = request_builder.json(&body);
@@ -209,6 +212,20 @@ mod tests {
209212
}
210213
)
211214
}
215+
216+
#[tokio::test]
217+
async fn test_get_query_send_mithril_api_version_header() {
218+
let (server, mut client) = setup_server_and_client();
219+
client.api_version_provider =
220+
APIVersionProvider::new_with_default_version(Version::parse("1.2.9").unwrap());
221+
server.mock(|when, then| {
222+
when.method(httpmock::Method::GET)
223+
.header(MITHRIL_API_VERSION_HEADER, "1.2.9");
224+
then.status(200).body(r#"{"foo": "a", "bar": 1}"#);
225+
});
226+
227+
client.send(TestGetQuery).await.expect("should not fail");
228+
}
212229
}
213230

214231
mod post {
@@ -243,13 +260,34 @@ mod tests {
243260

244261
assert_eq!(response, ())
245262
}
263+
264+
#[tokio::test]
265+
async fn test_post_query_send_mithril_api_version_header() {
266+
let (server, mut client) = setup_server_and_client();
267+
client.api_version_provider =
268+
APIVersionProvider::new_with_default_version(Version::parse("1.2.9").unwrap());
269+
server.mock(|when, then| {
270+
when.method(httpmock::Method::POST)
271+
.header(MITHRIL_API_VERSION_HEADER, "1.2.9");
272+
then.status(201);
273+
});
274+
275+
client
276+
.send(TestPostQuery {
277+
body: TestBody {
278+
pika: "a".to_string(),
279+
chu: 3,
280+
},
281+
})
282+
.await
283+
.expect("should not fail");
284+
}
246285
}
247286

248287
mod warn_if_api_version_mismatch {
249288
use http::response::Builder as HttpResponseBuilder;
250289
use reqwest::Response;
251290

252-
use mithril_common::test::api_version_extensions::ApiVersionProviderExtensions;
253291
use mithril_common::test::logging::MemoryDrainForTestInspector;
254292

255293
use super::*;

0 commit comments

Comments
 (0)