Skip to content

Commit

Permalink
feat(rust): Rust set headers (#172)
Browse files Browse the repository at this point in the history
* feat(java): allow setting arbitrary headers

* chore: fmt

Signed-off-by: Mark Phelps <209477+markphelps@users.noreply.github.com>

* chore: convert everything to builder

* feat(java): allow setting reference

* chore: remove setting reference for now, needs more thought

* feat(rust): add arbitrary headers and ref

* chore: only allow setting headers for now

* chore(rust): use duration for timeout

---------

Signed-off-by: Mark Phelps <209477+markphelps@users.noreply.github.com>
  • Loading branch information
markphelps authored Apr 11, 2024
1 parent abe7d27 commit d87028c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 28 deletions.
2 changes: 1 addition & 1 deletion flipt-rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "flipt"
description = "Flipt Server SDK"
version = "1.0.0"
edition = "2021"
author = ["Flipt Devs <dev@flipt.io>"]
authors = ["Flipt Devs <dev@flipt.io>"]
license = "MIT"
keywords = ["flipt"]

Expand Down
15 changes: 10 additions & 5 deletions flipt-rust/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::error::ClientError;
use crate::evaluation::Evaluation;
use crate::{AuthenticationStrategy, Config, NoneAuthentication};
use std::time::Duration;
use crate::{AuthenticationStrategy, Config, ConfigBuilder, NoneAuthentication};

pub struct FliptClient {
pub evaluation: Evaluation,
Expand All @@ -12,10 +11,15 @@ impl FliptClient {
where
T: AuthenticationStrategy,
{
let header_map = config.auth_strategy.authenticate();
let mut header_map = config.headers.unwrap_or_default();

if let Some(auth_strategy) = config.auth_strategy {
let auth_headers = auth_strategy.authenticate();
header_map.extend(auth_headers);
}

let client = match reqwest::Client::builder()
.timeout(Duration::from_secs(config.timeout))
.timeout(config.timeout)
.default_headers(header_map)
.build()
{
Expand All @@ -33,6 +37,7 @@ impl FliptClient {

impl Default for FliptClient {
fn default() -> Self {
Self::new::<NoneAuthentication>(Config::default()).unwrap()
Self::new::<NoneAuthentication>(ConfigBuilder::<NoneAuthentication>::default().build())
.unwrap()
}
}
57 changes: 42 additions & 15 deletions flipt-rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod evaluation;
pub mod util;

use reqwest::header::HeaderMap;
use std::time::Duration;
use url::Url;

#[derive(Debug, Clone)]
Expand All @@ -12,29 +13,55 @@ where
T: AuthenticationStrategy,
{
endpoint: Url,
auth_strategy: T,
timeout: u64,
timeout: Duration,
auth_strategy: Option<T>,
headers: Option<HeaderMap>,
}

impl Default for Config<NoneAuthentication> {
#[derive(Debug, Clone)]
pub struct ConfigBuilder<T>
where
T: AuthenticationStrategy,
{
endpoint: Option<Url>,
auth_strategy: Option<T>,
timeout: Option<Duration>,
headers: Option<HeaderMap>,
}

impl<T: AuthenticationStrategy> Default for ConfigBuilder<T> {
fn default() -> Self {
Self {
endpoint: Url::parse("http://localhost:8080").unwrap(),
auth_strategy: NoneAuthentication::default(),
timeout: 60,
endpoint: Url::parse("http://localhost:8080").ok(),
auth_strategy: None,
timeout: Some(Duration::from_secs(60)), // Default timeout is 60 seconds
headers: None,
}
}
}

impl<T> Config<T>
where
T: AuthenticationStrategy,
{
pub fn new(endpoint: Url, auth_strategy: T, timeout: u64) -> Self {
Self {
endpoint,
auth_strategy,
timeout,
impl<T: AuthenticationStrategy> ConfigBuilder<T> {
pub fn with_endpoint(mut self, endpoint: Url) -> Self {
self.endpoint = Some(endpoint);
self
}

pub fn with_auth_strategy(mut self, auth_strategy: T) -> Self {
self.auth_strategy = Some(auth_strategy);
self
}

pub fn with_timeout(mut self, timeout: Duration) -> Self {
self.timeout = Some(timeout);
self
}

pub fn build(self) -> Config<T> {
Config {
endpoint: self.endpoint.unwrap(),
auth_strategy: self.auth_strategy,
timeout: self.timeout.unwrap(),
headers: self.headers,
}
}
}
Expand Down
16 changes: 9 additions & 7 deletions flipt-rust/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use flipt::evaluation::models::{
BatchEvaluationRequest, ErrorEvaluationReason, EvaluationReason, EvaluationRequest,
EvaluationResponseType,
};
use flipt::{ClientTokenAuthentication, Config};
use flipt::{ClientTokenAuthentication, ConfigBuilder};
use std::time::Duration;
use std::{collections::HashMap, env};
use url::Url;

Expand All @@ -12,12 +13,13 @@ async fn tests() {
let url = env::var("FLIPT_URL").unwrap();
let token = env::var("FLIPT_AUTH_TOKEN").unwrap();

let flipt_client = FliptClient::new(Config::new(
Url::parse(&url).unwrap(),
ClientTokenAuthentication::new(token),
60,
))
.unwrap();
let config = ConfigBuilder::default()
.with_endpoint(Url::parse(&url).unwrap())
.with_auth_strategy(ClientTokenAuthentication::new(token))
.with_timeout(Duration::from_secs(60))
.build();

let flipt_client = FliptClient::new(config).unwrap();

let mut context: HashMap<String, String> = HashMap::new();
context.insert("fizz".into(), "buzz".into());
Expand Down

0 comments on commit d87028c

Please sign in to comment.