forked from Azure/azure-sdk-for-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c0eeef5
commit 3230f7f
Showing
4 changed files
with
76 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use async_trait::async_trait; | ||
use std::sync::Arc; | ||
|
||
use azure_core::{ | ||
auth::Secret, | ||
headers::{HeaderName, HeaderValue}, | ||
Context, Header, Policy, PolicyResult, Request, | ||
}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct AzureKeyCredential(Secret); | ||
|
||
impl AzureKeyCredential { | ||
pub fn new(api_key: impl Into<String>) -> Self { | ||
Self(Secret::new(api_key.into())) | ||
} | ||
} | ||
|
||
impl Header for AzureKeyCredential { | ||
fn name(&self) -> HeaderName { | ||
HeaderName::from_static("api-key") | ||
} | ||
|
||
fn value(&self) -> HeaderValue { | ||
HeaderValue::from_cow(format!("{}", self.0.secret())) | ||
} | ||
} | ||
|
||
// code lifted from BearerTokenCredentialPolicy | ||
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] | ||
#[cfg_attr(not(target_arch = "wasm32"), async_trait)] | ||
impl Policy for AzureKeyCredential { | ||
async fn send( | ||
&self, | ||
ctx: &Context, | ||
request: &mut Request, | ||
next: &[Arc<dyn Policy>], | ||
) -> PolicyResult { | ||
request.insert_header(Header::name(self), Header::value(self)); | ||
next[0].send(ctx, request, &next[1..]).await | ||
} | ||
} | ||
|
||
impl Into<Arc<dyn Policy>> for AzureKeyCredential { | ||
fn into(self) -> Arc<dyn Policy> { | ||
Arc::new(self) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,5 @@ | ||
use async_trait::async_trait; | ||
use std::sync::Arc; | ||
mod azure_key_credential; | ||
mod openai_key_credential; | ||
|
||
use azure_core::{ | ||
auth::Secret, | ||
headers::{HeaderName, HeaderValue, AUTHORIZATION}, | ||
Context, Header, Policy, PolicyResult, Request, | ||
}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct AzureKeyCredential(Secret); | ||
|
||
pub struct OpenAIKeyCredential(Secret); | ||
|
||
impl OpenAIKeyCredential { | ||
pub fn new(access_token: String) -> Self { | ||
Self(Secret::new(access_token)) | ||
} | ||
} | ||
|
||
impl AzureKeyCredential { | ||
pub fn new(api_key: String) -> Self { | ||
Self(Secret::new(api_key)) | ||
} | ||
} | ||
|
||
impl Header for AzureKeyCredential { | ||
fn name(&self) -> HeaderName { | ||
HeaderName::from_static("api-key") | ||
} | ||
|
||
fn value(&self) -> HeaderValue { | ||
HeaderValue::from_cow(format!("{}", self.0.secret())) | ||
} | ||
} | ||
|
||
// code lifted from BearerTokenCredentialPolicy | ||
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] | ||
#[cfg_attr(not(target_arch = "wasm32"), async_trait)] | ||
impl Policy for AzureKeyCredential { | ||
async fn send( | ||
&self, | ||
ctx: &Context, | ||
request: &mut Request, | ||
next: &[Arc<dyn Policy>], | ||
) -> PolicyResult { | ||
request.insert_header(Header::name(self), Header::value(self)); | ||
next[0].send(ctx, request, &next[1..]).await | ||
} | ||
} | ||
|
||
impl Into<Arc<dyn Policy>> for AzureKeyCredential { | ||
fn into(self) -> Arc<dyn Policy> { | ||
Arc::new(self) | ||
} | ||
} | ||
|
||
impl Header for OpenAIKeyCredential { | ||
fn name(&self) -> HeaderName { | ||
AUTHORIZATION | ||
} | ||
|
||
fn value(&self) -> HeaderValue { | ||
HeaderValue::from_cow(format!("Bearer {}", &self.0.secret())) | ||
} | ||
} | ||
pub(crate) use azure_key_credential::*; | ||
pub(crate) use openai_key_credential::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use azure_core::{ | ||
auth::Secret, | ||
headers::{HeaderName, HeaderValue, AUTHORIZATION}, | ||
Header, | ||
}; | ||
|
||
pub struct OpenAIKeyCredential(Secret); | ||
|
||
impl OpenAIKeyCredential { | ||
pub fn new(access_token: String) -> Self { | ||
Self(Secret::new(access_token)) | ||
} | ||
} | ||
|
||
impl Header for OpenAIKeyCredential { | ||
fn name(&self) -> HeaderName { | ||
AUTHORIZATION | ||
} | ||
|
||
fn value(&self) -> HeaderValue { | ||
HeaderValue::from_cow(format!("Bearer {}", &self.0.secret())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters