Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Added

- **Rust:** `Config::header(key, value)` builder method to inject custom headers into every HTTP request and WebSocket upgrade request.
- **Rust, Python:** `ContentContext` adds three new methods:
- `topic_detail(topic_id)` — get detail of a single topic.
- `list_topic_replies(opts)` — list replies for a topic, with optional page/size filtering.
Expand Down
29 changes: 27 additions & 2 deletions rust/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
sync::Arc,
};

pub(crate) use http::{HeaderValue, Request, header};
pub(crate) use http::{HeaderName, HeaderValue, Request, header};
use longbridge_httpcli::{HttpClient, HttpClientConfig, is_cn};
use longbridge_oauth::OAuth;
use num_enum::IntoPrimitive;
Expand Down Expand Up @@ -127,6 +127,8 @@ pub struct Config {
pub(crate) enable_print_quote_packages: bool,
pub(crate) language: Language,
pub(crate) log_path: Option<PathBuf>,
/// Extra headers injected into every HTTP and WebSocket upgrade request.
pub(crate) custom_headers: HashMap<String, String>,
}

/// Reads an env var by trying `LONGBRIDGE_<suffix>` first, then falling back
Expand Down Expand Up @@ -218,6 +220,7 @@ impl Config {
push_candlestick_mode: extras.push_candlestick_mode,
enable_print_quote_packages: extras.enable_print_quote_packages,
log_path: extras.log_path,
custom_headers: Default::default(),
}
}

Expand Down Expand Up @@ -266,6 +269,7 @@ impl Config {
push_candlestick_mode: extras.push_candlestick_mode,
enable_print_quote_packages: extras.enable_print_quote_packages,
log_path: extras.log_path,
custom_headers: Default::default(),
}
}

Expand Down Expand Up @@ -320,6 +324,7 @@ impl Config {
push_candlestick_mode: extras.push_candlestick_mode,
enable_print_quote_packages: extras.enable_print_quote_packages,
log_path: extras.log_path,
custom_headers: Default::default(),
})
}

Expand Down Expand Up @@ -419,7 +424,12 @@ impl Config {
config = config.http_url(url.clone());
}

HttpClient::new(config).header(header::ACCEPT_LANGUAGE, self.language.as_str())
let mut client =
HttpClient::new(config).header(header::ACCEPT_LANGUAGE, self.language.as_str());
for (key, value) in &self.custom_headers {
client = client.header(key.as_str(), value.as_str());
}
client
}

fn create_ws_request(&self, url: &str) -> tokio_tungstenite::tungstenite::Result<Request<()>> {
Expand All @@ -428,6 +438,14 @@ impl Config {
header::ACCEPT_LANGUAGE,
HeaderValue::from_str(self.language.as_str()).unwrap(),
);
for (key, value) in &self.custom_headers {
if let (Ok(name), Ok(val)) = (
HeaderName::from_bytes(key.as_bytes()),
HeaderValue::from_str(value),
) {
request.headers_mut().append(name, val);
}
}
Ok(request)
}

Expand Down Expand Up @@ -471,6 +489,13 @@ impl Config {
self
}

/// Add a custom header to every HTTP request and WebSocket upgrade request.
#[must_use]
pub fn header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.custom_headers.insert(key.into(), value.into());
self
}

/// Set the HTTP endpoint URL in place.
pub fn set_http_url(&mut self, url: impl Into<String>) {
self.http_url = Some(url.into());
Expand Down
Loading