-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAzureDevOpsApi.cs
48 lines (37 loc) · 1.53 KB
/
AzureDevOpsApi.cs
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
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
namespace LibraryAzureDevOps;
public class AzureDevOpsApi(Settings settings)
{
private readonly Uri _linkApi = new(
$"https://dev.azure.com/{settings.Organization}/{settings.Project}/_apis/distributedtask/variablegroups?api-version={settings.ApiVersion}&groupName=*");
private readonly HttpClient _client = new();
private readonly string _accessToken = ConvertToBase64(settings.PersonalAccessTokens);
private static string ConvertToBase64(string personalAccessToken)
{
var plainTextBytes = Encoding.UTF8.GetBytes($":{personalAccessToken}");
return Convert.ToBase64String(plainTextBytes);
}
public async Task<string> GetLibraries()
{
try
{
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", $" {_accessToken}");
HttpResponseMessage response = await _client.GetAsync(_linkApi);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
using (JsonDocument document = JsonDocument.Parse(responseBody))
{
JsonElement root = document.RootElement;
var values = root.GetProperty("value");
return values.ToString();
}
}
catch (HttpRequestException e)
{
Console.WriteLine("Ocorreu um erro na requisição: " + e.Message);
return string.Empty;
}
}
}