Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/cred custom headers #68

Merged
merged 2 commits into from
Sep 13, 2024
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
8 changes: 8 additions & 0 deletions py_integ/test_integ.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,13 @@ def test_generic_dl(client: Fusion) -> None:
print(f"Passed integ tests for {test_nm}") # noqa: T201


def test_custom_headers(client: Fusion) -> None:
client.credentials.headers = {"my_special_header": "my_special_value", "my_special_header2": "my_special_value2"}
r = client.session.get(client.root_url + "catalogs/common")
assert r.request.headers["my_special_header"] == "my_special_value"
assert r.request.headers["my_special_header2"] == "my_special_value2"
print("Passed custom headers test") # noqa: T201


if __name__ == "__main__":
gen_generic_dl()
42 changes: 38 additions & 4 deletions src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ struct FusionCredsPersistent {
grant_type: String,
//#[serde(deserialize_with = "deserialize_fusion_e2e")]
fusion_e2e: Option<String>,
headers: Option<HashMap<String, String>>,
}

#[pyclass(module = "fusion._fusion")]
Expand Down Expand Up @@ -314,6 +315,9 @@ pub struct FusionCredentials {
#[pyo3(get, set)]
fusion_e2e: Option<String>,

#[pyo3(get, set)]
headers: HashMap<String, String>,

#[pyo3(get)]
proxies: HashMap<String, String>,

Expand Down Expand Up @@ -341,6 +345,7 @@ impl Default for FusionCredentials {
proxies: HashMap::new(),
grant_type: "client_credentials".to_string(),
fusion_e2e: None,
headers: HashMap::new(),
http_client: None,
}
}
Expand Down Expand Up @@ -371,6 +376,7 @@ impl FusionCredentials {
Option<HashMap<String, String>>,
Option<String>,
Option<String>,
Option<HashMap<String, String>>,
)> {
Ok((
self.client_id.clone(),
Expand All @@ -383,11 +389,12 @@ impl FusionCredentials {
Some(self.proxies.clone()),
Some(self.grant_type.clone()),
self.fusion_e2e.clone(),
Some(self.headers.clone()),
))
}

#[classmethod]
#[pyo3(signature = (client_id=None, client_secret=None, resource=None, auth_url=None, proxies=None, fusion_e2e=None))]
#[pyo3(signature = (client_id=None, client_secret=None, resource=None, auth_url=None, proxies=None, fusion_e2e=None, headers=None))]
fn from_client_id(
_cls: &Bound<'_, PyType>,
client_id: Option<String>,
Expand All @@ -396,6 +403,7 @@ impl FusionCredentials {
auth_url: Option<String>,
proxies: Option<HashMap<String, String>>,
fusion_e2e: Option<String>,
headers: Option<HashMap<String, String>>,
) -> PyResult<Self> {
Ok(Self {
client_id,
Expand All @@ -405,6 +413,7 @@ impl FusionCredentials {
proxies: proxies.unwrap_or_default(),
grant_type: "client_credentials".to_string(),
fusion_e2e,
headers: headers.unwrap_or_default(),
fusion_token: HashMap::new(),
bearer_token: None,
username: None,
Expand All @@ -422,7 +431,7 @@ impl FusionCredentials {
}

#[classmethod]
#[pyo3(signature = (client_id=None, username=None, password=None, resource=None, auth_url=None, proxies=None, fusion_e2e=None))]
#[pyo3(signature = (client_id=None, username=None, password=None, resource=None, auth_url=None, proxies=None, fusion_e2e=None, headers=None))]
fn from_user_id(
_cls: &Bound<'_, PyType>,
client_id: Option<String>,
Expand All @@ -432,6 +441,7 @@ impl FusionCredentials {
auth_url: Option<String>,
proxies: Option<HashMap<String, String>>,
fusion_e2e: Option<String>,
headers: Option<HashMap<String, String>>,
) -> PyResult<Self> {
Ok(Self {
client_id,
Expand All @@ -442,6 +452,7 @@ impl FusionCredentials {
proxies: proxies.unwrap_or_default(),
grant_type: "password".to_string(),
fusion_e2e,
headers: headers.unwrap_or_default(),
fusion_token: HashMap::new(),
bearer_token: None,
client_secret: None,
Expand All @@ -450,20 +461,22 @@ impl FusionCredentials {
}

#[classmethod]
#[pyo3(signature = (bearer_token=None, bearer_token_expiry=None, proxies=None, fusion_e2e=None))]
#[pyo3(signature = (bearer_token=None, bearer_token_expiry=None, proxies=None, fusion_e2e=None, headers=None))]
fn from_bearer_token(
_cls: &Bound<'_, PyType>,
bearer_token: Option<String>,
bearer_token_expiry: Option<&Bound<PyDate>>,
proxies: Option<HashMap<String, String>>,
fusion_e2e: Option<String>,
headers: Option<HashMap<String, String>>,
) -> PyResult<Self> {
Ok(Self {
resource: None,
auth_url: None,
proxies: proxies.unwrap_or_default(),
grant_type: "bearer".to_string(),
fusion_e2e,
headers: headers.unwrap_or_default(),
fusion_token: HashMap::new(),
bearer_token: Some(AuthToken {
token: match bearer_token {
Expand All @@ -490,7 +503,7 @@ impl FusionCredentials {

#[allow(clippy::too_many_arguments)]
#[new]
#[pyo3(signature = (client_id=None, client_secret=None, username=None, password=None, resource=None, auth_url=None, bearer_token=None, proxies=None, grant_type=None, fusion_e2e=None))]
#[pyo3(signature = (client_id=None, client_secret=None, username=None, password=None, resource=None, auth_url=None, bearer_token=None, proxies=None, grant_type=None, fusion_e2e=None, headers=None))]
fn new(
client_id: Option<String>,
client_secret: Option<String>,
Expand All @@ -502,6 +515,7 @@ impl FusionCredentials {
proxies: Option<HashMap<String, String>>,
grant_type: Option<String>,
fusion_e2e: Option<String>,
headers: Option<HashMap<String, String>>,
) -> PyResult<Self> {
Ok(FusionCredentials {
client_id,
Expand All @@ -515,6 +529,7 @@ impl FusionCredentials {
proxies: proxies.unwrap_or_default(),
grant_type: grant_type.unwrap_or_else(|| "client_credentials".to_string()),
fusion_e2e,
headers: headers.unwrap_or_default(),
http_client: None,
})
}
Expand Down Expand Up @@ -695,6 +710,12 @@ impl FusionCredentials {
self.fusion_e2e.as_ref().unwrap().clone(),
);
}
// if headers are not empty add each key value pair to the headers
if !self.headers.is_empty() {
for (key, value) in self.headers.iter() {
ret.insert(key.clone(), value.clone());
}
}

self._refresh_bearer_token(py, false, 30)?;
let bearer_token_tup = self
Expand Down Expand Up @@ -797,13 +818,15 @@ impl FusionCredentials {
credentials.auth_url,
Some(untyped_proxies(credentials.proxies)),
credentials.fusion_e2e,
credentials.headers,
)?,
"bearer" => FusionCredentials::from_bearer_token(
cls,
None,
None,
Some(untyped_proxies(credentials.proxies)),
credentials.fusion_e2e,
credentials.headers,
)?,
"password" => FusionCredentials::from_user_id(
cls,
Expand All @@ -814,6 +837,7 @@ impl FusionCredentials {
credentials.auth_url,
Some(untyped_proxies(credentials.proxies)),
credentials.fusion_e2e,
credentials.headers,
)?,
_ => {
return Err(pyo3::exceptions::PyValueError::new_err(
Expand Down Expand Up @@ -1024,6 +1048,7 @@ mod tests {
Some(HashMap::new()),
Some("grant_type".to_string()),
Some("fusion_e2e".to_string()),
Some(HashMap::new()),
)
.unwrap();

Expand All @@ -1050,6 +1075,7 @@ mod tests {
assert!(creds.fusion_e2e.is_none());
assert!(creds.bearer_token.is_none());
assert!(creds.proxies.is_empty());
assert!(creds.headers.is_empty());
assert!(creds.fusion_token.is_empty());
}

Expand All @@ -1066,6 +1092,7 @@ mod tests {
Some(HashMap::new()),
Some("grant_type".to_string()),
Some("fusion_e2e".to_string()),
Some(HashMap::new()),
)
.unwrap();

Expand Down Expand Up @@ -1095,6 +1122,7 @@ mod tests {
Some(HashMap::new()),
Some("grant_type".to_string()),
Some("fusion_e2e".to_string()),
Some(HashMap::new()),
)
.unwrap();

Expand Down Expand Up @@ -1125,6 +1153,7 @@ mod tests {
Some(HashMap::new()),
Some("grant_type".to_string()),
Some("fusion_e2e".to_string()),
Some(HashMap::new()),
)
.unwrap();

Expand All @@ -1139,6 +1168,7 @@ mod tests {
proxies,
grant_type,
fusion_e2e,
headers,
) = creds.__getnewargs__().unwrap();

assert_eq!(client_id, Some("client_id".to_string()));
Expand All @@ -1149,6 +1179,7 @@ mod tests {
assert_eq!(auth_url, Some("auth_url".to_string()));
assert!(bearer_token.is_none());
assert!(proxies.is_some());
assert!(headers.is_some());
assert_eq!(grant_type, Some("grant_type".to_string()));
assert_eq!(fusion_e2e, Some("fusion_e2e".to_string()));
}
Expand All @@ -1165,6 +1196,7 @@ mod tests {
Some("auth_url".to_string()),
None,
None,
None,
)
.unwrap();

Expand All @@ -1189,6 +1221,7 @@ mod tests {
Some("auth_url".to_string()),
None,
None,
None,
)
.unwrap();

Expand All @@ -1211,6 +1244,7 @@ mod tests {
Some(&expiry_date),
None,
None,
None,
)
.unwrap();

Expand Down