Skip to content

Commit

Permalink
editoast: remove http core client
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre-Etienne Bougué <[email protected]>
  • Loading branch information
bougue-pe committed Oct 11, 2024
1 parent 16554ef commit 545ad37
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 159 deletions.
25 changes: 0 additions & 25 deletions editoast/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3756,30 +3756,6 @@ components:
type: string
enum:
- editoast:coreclient:BrokenPipe
EditoastCoreErrorCannotExtractResponseBody:
type: object
required:
- type
- status
- message
properties:
context:
type: object
required:
- msg
properties:
msg:
type: string
message:
type: string
status:
type: integer
enum:
- 500
type:
type: string
enum:
- editoast:coreclient:CannotExtractResponseBody
EditoastCoreErrorConnectionClosedBeforeMessageCompleted:
type: object
required:
Expand Down Expand Up @@ -4055,7 +4031,6 @@ components:
- $ref: '#/components/schemas/EditoastCacheOperationErrorDuplicateIdsProvided'
- $ref: '#/components/schemas/EditoastCacheOperationErrorObjectNotFound'
- $ref: '#/components/schemas/EditoastCoreErrorBrokenPipe'
- $ref: '#/components/schemas/EditoastCoreErrorCannotExtractResponseBody'
- $ref: '#/components/schemas/EditoastCoreErrorConnectionClosedBeforeMessageCompleted'
- $ref: '#/components/schemas/EditoastCoreErrorConnectionResetByPeer'
- $ref: '#/components/schemas/EditoastCoreErrorCoreResponseFormatError'
Expand Down
35 changes: 0 additions & 35 deletions editoast/src/core/http_client.rs

This file was deleted.

95 changes: 0 additions & 95 deletions editoast/src/core/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod conflict_detection;
mod http_client;
pub mod infra_loading;
#[cfg(test)]
pub mod mocking;
Expand All @@ -17,20 +16,14 @@ use std::marker::PhantomData;

use async_trait::async_trait;
use axum::http::StatusCode;
use colored::ColoredString;
use colored::Colorize;
use editoast_derive::EditoastError;
pub use http_client::HttpClient;
pub use http_client::HttpClientBuilder;
use reqwest::Url;
use serde::de::DeserializeOwned;
use serde::Deserialize;
use serde::Serialize;
use serde_json::Value;
use thiserror::Error;
use tracing::debug;
use tracing::error;
use tracing::info;

#[cfg(test)]
use crate::core::mocking::MockingError;
Expand All @@ -45,45 +38,14 @@ editoast_common::schemas! {
conflict_detection::schemas(),
}

const MAX_RETRIES: u8 = 5;

fn colored_method(method: &reqwest::Method) -> ColoredString {
let m = method.as_str();
match *method {
reqwest::Method::GET => m.green(),
reqwest::Method::POST => m.yellow(),
reqwest::Method::PUT => m.blue(),
reqwest::Method::PATCH => m.magenta(),
reqwest::Method::DELETE => m.red(),
_ => m.normal(),
}
.bold()
}

#[derive(Debug, Clone)]
pub enum CoreClient {
Direct(HttpClient),
MessageQueue(RabbitMQClient),
#[cfg(test)]
Mocked(mocking::MockingClient),
}

impl CoreClient {
pub fn new_direct(base_url: Url, bearer_token: String) -> Self {
let client = reqwest::Client::builder()
.default_headers({
let mut headers = reqwest::header::HeaderMap::new();
headers.insert(
reqwest::header::AUTHORIZATION,
reqwest::header::HeaderValue::from_str(&format!("Bearer {}", bearer_token))
.expect("invalid bearer token"),
);
headers
})
.build_base_url(base_url);
Self::Direct(client)
}

pub async fn new_mq(
uri: String,
worker_pool_identifier: String,
Expand Down Expand Up @@ -134,54 +96,11 @@ impl CoreClient {
body: Option<&B>,
infra_id: Option<i64>,
) -> Result<R::Response> {
let method_s = colored_method(&method);
debug!(
target: "editoast::coreclient",
body = body.and_then(|b| serde_json::to_string_pretty(b).ok()).unwrap_or_default(),
"Request content");
match self {
CoreClient::Direct(client) => {
let mut i_try = 0;
let response = loop {
let mut request = client.request(method.clone(), path);
if let Some(body) = body {
request = request.json(body);
}
match request.send().await.map_err(Into::<CoreError>::into) {
// This error occurs quite often in the CI.
// It's linked to this issue https://github.com/hyperium/hyper/issues/2136.
// This is why we retry the request here.
// We also retry on broken pipe.
Err(
CoreError::ConnectionResetByPeer
| CoreError::ConnectionClosedBeforeMessageCompleted
| CoreError::BrokenPipe,
) if i_try < MAX_RETRIES => {
i_try += 1;
info!("Core request '{}: {}': Connection closed before message completed. Retry [{}/{}]", method, path, i_try, MAX_RETRIES);
continue;
}
response => break response?,
}
};

let url = response.url().to_string();
let status = response.status();
let bytes =
response
.bytes()
.await
.map_err(|err| CoreError::CannotExtractResponseBody {
msg: err.to_string(),
})?;
if status.is_success() {
info!(target: "editoast::coreclient", "{method_s} {path} {status}", status = status.to_string().bold().green());
return R::from_bytes(bytes.as_ref());
}

error!(target: "editoast::coreclient", "{method_s} {path} {status}", status = status.to_string().bold().red());
Err(self.handle_error(bytes.as_ref(), url))
}
CoreClient::MessageQueue(client) => {
// TODO: maybe implement retry?
let infra_id = infra_id.unwrap_or(1); // FIXME: don't do that!!!
Expand Down Expand Up @@ -218,17 +137,6 @@ impl CoreClient {
}
}

impl Default for CoreClient {
fn default() -> Self {
let address = std::env::var("OSRD_BACKEND").unwrap_or("http://localhost:8080".to_owned());
let bearer_token = std::env::var("OSRD_BACKEND_TOKEN").unwrap_or_default();
Self::new_direct(
address.parse().expect("invalid OSRD_BACKEND URL format"),
bearer_token,
)
}
}

/// A struct implementing this trait represents a Core request payload
///
/// For example:
Expand Down Expand Up @@ -353,9 +261,6 @@ impl CoreResponse for () {
#[derive(Debug, Error, EditoastError)]
#[editoast_error(base_id = "coreclient")]
enum CoreError {
#[error("Cannot extract Core response body: {msg}")]
#[editoast_error(status = 500)]
CannotExtractResponseBody { msg: String },
#[error("Cannot parse Core response: {msg}")]
#[editoast_error(status = 500)]
CoreResponseFormatError { msg: String },
Expand Down
4 changes: 2 additions & 2 deletions editoast/src/views/infra/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ pub mod tests {
let pool = DbConnectionPoolV2::for_tests_no_transaction();
let app = TestAppBuilder::new()
.db_pool(pool)
.core_client(CoreClient::default())
.core_client(CoreClient::Mocked(MockingClient::default()))
.build();
let db_pool = app.db_pool();
let empty_infra = create_empty_infra(&mut db_pool.get_ok()).await;
Expand Down Expand Up @@ -1229,7 +1229,7 @@ pub mod tests {
.build();
let app = TestAppBuilder::new()
.db_pool(db_pool)
.core_client(CoreClient::default())
.core_client(CoreClient::Mocked(MockingClient::default()))
.osrdyne_client(osrdyne_client)
.build();

Expand Down
4 changes: 2 additions & 2 deletions editoast/src/views/test_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use tower_http::trace::TraceLayer;

use crate::{
client::{MapLayersConfig, PostgresConfig, RedisConfig},
core::CoreClient,
core::{mocking::MockingClient, CoreClient},
generated_data::speed_limit_tags_config::SpeedLimitTagIds,
infra_cache::InfraCache,
map::MapLayers,
Expand Down Expand Up @@ -73,7 +73,7 @@ impl TestAppBuilder {

pub fn default_app() -> TestApp {
let pool = DbConnectionPoolV2::for_tests();
let core_client = CoreClient::default();
let core_client = CoreClient::Mocked(MockingClient::default());
TestAppBuilder::new()
.db_pool(pool)
.core_client(core_client)
Expand Down

0 comments on commit 545ad37

Please sign in to comment.