-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathmod.rs
More file actions
56 lines (49 loc) · 1.62 KB
/
mod.rs
File metadata and controls
56 lines (49 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
use anyhow::Result;
use async_trait::async_trait;
use azure_core::http::{
policies::{Policy, PolicyResult},
Context, Request,
};
use azure_devops_rust_api::Credential;
use azure_identity::AzureCliCredential;
use std::sync::Arc;
fn authenticate_with_cli_credential() -> Result<Credential> {
println!("Authenticate using Azure CLI credential");
let azure_cli_credential = AzureCliCredential::new(None)?;
Ok(Credential::from_token_credential(azure_cli_credential))
}
#[allow(dead_code)]
pub fn get_credential() -> Result<Credential> {
// Get authentication credential either from a PAT ("ADO_TOKEN") or via the az cli
match std::env::var("ADO_TOKEN") {
Ok(token) if !token.is_empty() => {
println!("Authenticate using PAT provided via $ADO_TOKEN");
Ok(Credential::from_pat(token))
}
_ => authenticate_with_cli_credential(),
}
}
// Define a simple policy to always set the `accept` header to `application/zip`
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct AcceptZipPolicy {}
impl AcceptZipPolicy {
#[allow(dead_code)]
pub fn new_policy() -> Arc<dyn Policy> {
Arc::new(AcceptZipPolicy {})
}
}
/// Always set the `accept` header to `application/zip`
#[async_trait]
impl Policy for AcceptZipPolicy {
async fn send(
&self,
ctx: &Context,
request: &mut Request,
next: &[Arc<dyn Policy>],
) -> PolicyResult {
request.insert_header("accept", "application/zip");
next[0].send(ctx, request, &next[1..]).await
}
}