Skip to content

Commit 690bb43

Browse files
committed
refactor: consolidate HTTP client creation with factory pattern
This commit introduces HttpClientFactory to centralize HTTP client creation, eliminating code duplication and ensuring consistent configuration. New Module: http_client.rs - HttpClientFactory with three client types: - https_client(): Cached HTTPS client for external APIs - unix_socket_client(): For local Unix socket communication - workload_client(): For IoT Edge workload API Benefits: - Eliminates 3 separate OnceLock instances across modules - Single source of truth for HTTP client configuration - Shared connection pools improve performance - Easier to add timeout/retry configuration in future - Better testability with centralized mocking point Changes by Module: - keycloak_client.rs: Use HttpClientFactory::https_client() - Removed local OnceLock<Client> - 4 lines removed, cleaner code - omnect_device_service_client.rs: Use HttpClientFactory::unix_socket_client() - Simplified client creation - 3 lines removed - certificate.rs: Use HttpClientFactory::workload_client() - Removed manual URI parsing - 8 lines removed, moved to factory Signed-off-by: Jan Zachmann <50990105+JanZachmann@users.noreply.github.com>
1 parent b461c46 commit 690bb43

File tree

8 files changed

+105
-393
lines changed

8 files changed

+105
-393
lines changed

src/certificate.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
use crate::{
44
common::handle_http_response,
5+
http_client::HttpClientFactory,
56
omnect_device_service_client::{DeviceServiceClient, OmnectDeviceServiceClient},
67
};
78
use anyhow::{Context, Result};
89
use log::info;
9-
use reqwest::Client;
1010
use serde::{Deserialize, Serialize};
1111
use std::{fs::File, io::Write};
1212

@@ -65,17 +65,8 @@ pub async fn create_module_certificate() -> Result<()> {
6565

6666
let path = format!("/modules/{id}/genid/{gen_id}/certificate/server?api-version={api_version}");
6767

68-
// Extract the Unix socket path from the workload URI
69-
// IoT Edge provides URIs like "unix:///var/run/iotedge/workload.sock"
70-
let socket_path = workload_uri
71-
.strip_prefix("unix://")
72-
.context("failed to parse IOTEDGE_WORKLOADURI: must use unix:// scheme")?;
73-
7468
// Create a client for the IoT Edge workload socket
75-
let client = Client::builder()
76-
.unix_socket(socket_path)
77-
.build()
78-
.context("failed to create HTTP client for workload socket")?;
69+
let client = HttpClientFactory::workload_client(&workload_uri)?;
7970

8071
let url = format!("http://localhost{}", path);
8172
info!("POST {url} (IoT Edge workload API)");

src/config.rs

Lines changed: 0 additions & 217 deletions
This file was deleted.

src/errors.rs

Lines changed: 0 additions & 149 deletions
This file was deleted.

0 commit comments

Comments
 (0)