-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathJfrogAqlApiCommunication.cs
205 lines (172 loc) · 8.36 KB
/
JfrogAqlApiCommunication.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// --------------------------------------------------------------------------------------------------------------------
// SPDX-FileCopyrightText: 2024 Siemens AG
//
// SPDX-License-Identifier: MIT
// --------------------------------------------------------------------------------------------------------------------
using LCT.APICommunications.Interfaces;
using LCT.APICommunications.Model;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace LCT.APICommunications
{
/// <summary>
/// The JfrogAqlApiCommunication class
/// </summary>
public class JfrogAqlApiCommunication : IJfrogAqlApiCommunication
{
protected string DomainName { get; set; }
private static int TimeoutInSec { get; set; }
protected ArtifactoryCredentials ArtifactoryCredentials { get; set; }
/// <summary>
/// The JfrogAqlApiCommunication constructor
/// </summary>
/// <param name="repoDomainName">repoDomainName</param>
/// <param name="artifactoryCredentials">artifactoryCredentials</param>
/// <param name="timeout">timeout</param>
public JfrogAqlApiCommunication(string repoDomainName, ArtifactoryCredentials artifactoryCredentials, int timeout)
{
DomainName = repoDomainName;
ArtifactoryCredentials = artifactoryCredentials;
TimeoutInSec = timeout;
}
public async Task<HttpResponseMessage> CheckConnection()
{
HttpClient httpClient = GetHttpClient(ArtifactoryCredentials);
string url = $"{DomainName}/api/security/apiKey";
return await httpClient.GetAsync(url);
}
/// <summary>
/// Gets the Internal Component Data By Repo name
/// </summary>
/// <param name="repoName"></param>
/// <returns></returns>
public async Task<HttpResponseMessage> GetInternalComponentDataByRepo(string repoName)
{
HttpClient httpClient = GetHttpClient(ArtifactoryCredentials);
TimeSpan timeOutInSec = TimeSpan.FromSeconds(TimeoutInSec);
httpClient.Timeout = timeOutInSec;
StringBuilder query = new();
query.Append("items.find({\"repo\":\"");
query.Append($"{repoName}");
query.Append("\"}).include(\"repo\", \"path\", \"name\", \"actual_sha1\",\"actual_md5\",\"sha256\")");
string aqlQueryToBody = query.ToString();
string uri = $"{DomainName}{ApiConstant.JfrogArtifactoryApiSearchAql}";
HttpContent httpContent = new StringContent(aqlQueryToBody);
return await httpClient.PostAsync(uri, httpContent);
}
public async Task<HttpResponseMessage> GetNpmComponentDataByRepo(string repoName)
{
HttpClient httpClient = GetHttpClient(ArtifactoryCredentials);
TimeSpan timeOutInSec = TimeSpan.FromSeconds(TimeoutInSec);
httpClient.Timeout = timeOutInSec;
StringBuilder query = new();
query.Append("items.find({\"repo\":\"");
query.Append($"{repoName}");
query.Append("\"}).include(\"repo\", \"path\", \"name\",\"@npm.name\",\"@npm.version\", \"actual_sha1\",\"actual_md5\",\"sha256\")");
string aqlQueryToBody = query.ToString();
string uri = $"{DomainName}{ApiConstant.JfrogArtifactoryApiSearchAql}";
HttpContent httpContent = new StringContent(aqlQueryToBody);
return await httpClient.PostAsync(uri, httpContent);
}
public async Task<HttpResponseMessage> GetPypiComponentDataByRepo(string repoName)
{
HttpClient httpClient = GetHttpClient(ArtifactoryCredentials);
TimeSpan timeOutInSec = TimeSpan.FromSeconds(TimeoutInSec);
httpClient.Timeout = timeOutInSec;
StringBuilder query = new();
query.Append("items.find({\"repo\":\"");
query.Append($"{repoName}");
query.Append("\"}).include(\"repo\", \"path\", \"name\",\"@pypi.normalized.name\",\"@pypi.version\", \"actual_sha1\",\"actual_md5\",\"sha256\")");
string aqlQueryToBody = query.ToString();
string uri = $"{DomainName}{ApiConstant.JfrogArtifactoryApiSearchAql}";
HttpContent httpContent = new StringContent(aqlQueryToBody);
return await httpClient.PostAsync(uri, httpContent);
}
/// <summary>
/// Gets the package information in the repo, via the name or path
/// </summary>
public async Task<HttpResponseMessage> GetPackageInfo(ComponentsToArtifactory component = null)
{
ValidateParameters(component.JfrogPackageName, component.Path);
var aqlQueryToBody = BuildAqlQuery(component);
string uri = $"{DomainName}{ApiConstant.JfrogArtifactoryApiSearchAql}";
HttpContent httpContent = new StringContent(aqlQueryToBody);
return await ExecuteSearchAqlAsync(uri, httpContent);
}
private static HttpClient GetHttpClient(ArtifactoryCredentials credentials)
{
var handler = new RetryHttpClientHandler()
{
InnerHandler = new HttpClientHandler()
};
var httpClient = new HttpClient(handler);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", credentials.Token);
return httpClient;
}
private static void ValidateParameters(string packageName, string path)
{
if (string.IsNullOrEmpty(packageName) && string.IsNullOrEmpty(path))
{
throw new ArgumentException("Either packageName or path, or both must be provided.");
}
}
private static string BuildAqlQuery( ComponentsToArtifactory component)
{
if (component.ComponentType.Equals("NPM", StringComparison.InvariantCultureIgnoreCase))
{
var queryList = new List<string>
{
$"\"repo\":{{\"$eq\":\"{component.SrcRepoName}\"}}",
$"\"@npm.name\":{{\"$eq\":\"{component.Name}\"}}",
$"\"@npm.version\":{{\"$eq\":\"{component.Version}\"}}"
};
// Build the AQL query string
StringBuilder query = new();
query.Append($"items.find({{{string.Join(", ", queryList)}}}).include(\"repo\", \"path\", \"name\")");
return query.ToString();
}
else if (component.ComponentType.Equals("Python", StringComparison.InvariantCultureIgnoreCase))
{
var queryList = new List<string>
{
$"\"repo\":{{\"$eq\":\"{component.SrcRepoName}\"}}",
$"\"@pypi.normalized.name\":{{\"$eq\":\"{component.Name}\"}}",
$"\"@pypi.version\":{{\"$eq\":\"{component.Version}\"}}"
};
// Build the AQL query string
StringBuilder query = new();
query.Append($"items.find({{{string.Join(", ", queryList)}}}).include(\"repo\", \"path\", \"name\")");
return query.ToString();
}
else
{
var queryList = new List<string>()
{
$"\"repo\":{{\"$eq\":\"{component.SrcRepoName}\"}}"
};
if (!string.IsNullOrEmpty(component.Path))
{
queryList.Add($"\"path\":{{\"$match\":\"{component.Path}\"}}");
}
if (!string.IsNullOrEmpty(component.JfrogPackageName))
{
queryList.Add($"\"name\":{{\"$match\":\"{component.JfrogPackageName}\"}}");
}
StringBuilder query = new();
query.Append($"items.find({{{string.Join(", ", queryList)}}}).include(\"repo\", \"path\", \"name\").limit(1)");
return query.ToString();
}
}
private async Task<HttpResponseMessage> ExecuteSearchAqlAsync(string uri, HttpContent httpContent)
{
HttpClient httpClient = GetHttpClient(ArtifactoryCredentials);
TimeSpan timeOutInSec = TimeSpan.FromSeconds(TimeoutInSec);
httpClient.Timeout = timeOutInSec;
return await httpClient.PostAsync(uri, httpContent);
}
}
}