Skip to content

Commit 081f0b2

Browse files
committed
release 0.0.5-beta source code for net
1 parent d5983a4 commit 081f0b2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+693
-218
lines changed

CHANGELOG.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
# 0.0.4-beta # 0.0.4-beta 2023-02-20
1+
# 0.0.5-beta 2023-04-14
2+
3+
### G42Cloud SDK Core
4+
5+
- _Features_
6+
- None
7+
- _Bug Fix_
8+
- None
9+
- _Change_
10+
- Optimize the code structure.
11+
12+
# 0.0.4-beta 2023-02-20
213

314
### G42Cloud SDK CBR
415

Core/Auth/IamService.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static string KeystoneListProjects(SdkHttpClient client, HttpRequest requ
8282
var message = client.InitHttpRequest(request, true);
8383
try
8484
{
85-
var response = client.DoHttpRequest(message).Result;
85+
var response = TaskUtils.RunSync(() => client.DoHttpRequest(message));
8686
if ((int)response.StatusCode >= 400)
8787
{
8888
throw ExceptionUtils.GetException(response);
@@ -105,7 +105,7 @@ public static string KeystoneListProjects(SdkHttpClient client, HttpRequest requ
105105
}
106106
catch (AggregateException aggregateException)
107107
{
108-
throw new ConnectionException(ExceptionUtils.GetMessageFromAggregateException(aggregateException));
108+
throw ExceptionUtils.HandleException(aggregateException);
109109
}
110110
}
111111

@@ -129,7 +129,7 @@ public static string KeystoneListAuthDomains(SdkHttpClient client, HttpRequest r
129129
var message = client.InitHttpRequest(request, true);
130130
try
131131
{
132-
var response = client.DoHttpRequest(message).Result;
132+
var response = TaskUtils.RunSync(() => client.DoHttpRequest(message));
133133
if ((int)response.StatusCode >= 400)
134134
{
135135
throw ExceptionUtils.GetException(response);
@@ -148,7 +148,7 @@ public static string KeystoneListAuthDomains(SdkHttpClient client, HttpRequest r
148148
}
149149
catch (AggregateException aggregateException)
150150
{
151-
throw new ConnectionException(ExceptionUtils.GetMessageFromAggregateException(aggregateException));
151+
throw ExceptionUtils.HandleException(aggregateException);
152152
}
153153
}
154154
}

Core/Client.cs

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
using System;
2323
using System.Collections.Generic;
2424
using System.Linq;
25+
using System.Net;
2526
using System.Net.Http;
27+
using System.Runtime.CompilerServices;
28+
using System.Threading;
2629
using System.Threading.Tasks;
2730
using G42Cloud.SDK.Core.Auth;
2831
using Microsoft.Extensions.Logging;
@@ -34,7 +37,10 @@ public class Client
3437
{
3538
public class ClientBuilder<T> where T : Client
3639
{
37-
private string[] CredentialType { get; } = {nameof(BasicCredentials)};
40+
private string[] CredentialType { get; } =
41+
{
42+
nameof(BasicCredentials)
43+
};
3844

3945
public ClientBuilder()
4046
{
@@ -48,7 +54,7 @@ public ClientBuilder(string credentialType)
4854
private Credentials _credentials;
4955
private HttpConfig _httpConfig;
5056
private Region _region;
51-
private string _endPoint;
57+
private List<string> _endpoints;
5258
private bool _enableLogging;
5359
private LogLevel _logLevel = LogLevel.Information;
5460
private HttpHandler _httpHandler;
@@ -74,10 +80,19 @@ public ClientBuilder<T> WithRegion(Region region)
7480
this._region = region;
7581
return this;
7682
}
83+
84+
[Obsolete("As of 3.1.26, because of the support of the multi-endpoint feature, use WithEndPoints instead")]
85+
public ClientBuilder<T> WithEndPoint(string endpoint)
86+
{
87+
return this.WithEndPoints(new List<string>
88+
{
89+
endpoint
90+
});
91+
}
7792

78-
public ClientBuilder<T> WithEndPoint(string endPoint)
93+
public ClientBuilder<T> WithEndPoints(List<string> endpoints)
7994
{
80-
this._endPoint = endPoint;
95+
this._endpoints = endpoints;
8196
return this;
8297
}
8398

@@ -120,24 +135,29 @@ public T Build()
120135

121136
if (this._region != null)
122137
{
123-
this._endPoint = _region.Endpoint;
138+
this._endpoints = this._region.Endpoints;
124139
this._credentials = _credentials.ProcessAuthParams(client._sdkHttpClient, _region.Id);
125140
this._credentials.ProcessDerivedAuthParams(_derivedAuthServiceName, _region.Id);
126141
}
127142

128-
if (!_endPoint.StartsWith(HttpScheme))
143+
for (var i = 0; i < _endpoints.Count; i++)
129144
{
130-
_endPoint = HttpsScheme + "://" + _endPoint;
145+
var endpoint = _endpoints[i];
146+
if (!endpoint.StartsWith(HttpScheme))
147+
{
148+
_endpoints[i] = HttpsScheme + "://" + endpoint;
149+
}
131150
}
132151

133152
client.WithCredential(this._credentials)
134-
.WithEndPoint(this._endPoint);
153+
.WithEndPoints(this._endpoints);
135154

136-
return (T) client;
155+
return (T)client;
137156
}
138157
}
139158

140-
private string _endpoint;
159+
private List<string> _endpoints;
160+
private volatile int _endpointIndex;
141161
private HttpConfig _httpConfig;
142162
private Credentials _credential;
143163

@@ -158,12 +178,13 @@ private Client WithHttpConfig(HttpConfig httpConfig)
158178
return this;
159179
}
160180

161-
private Client WithEndPoint(string endPoint)
181+
private Client WithEndPoints(List<string> endpoints)
162182
{
163-
this._endpoint = endPoint;
183+
this._endpoints = endpoints;
164184
return this;
165185
}
166186

187+
167188
private void InitSdkHttpClient(HttpHandler httpHandler, bool enableLogging, LogLevel logLevel)
168189
{
169190
this._sdkHttpClient =
@@ -194,16 +215,33 @@ private async Task<HttpResponseMessage> _async_http(string url, string method, S
194215
}
195216
catch (AggregateException aggregateException)
196217
{
197-
throw new ConnectionException(ExceptionUtils.GetMessageFromAggregateException(aggregateException));
218+
throw ExceptionUtils.HandleException(aggregateException);
198219
}
199220
}
200221

222+
[MethodImpl(MethodImplOptions.Synchronized)]
201223
protected HttpResponseMessage DoHttpRequestSync(string methodType, SdkRequest request)
202224
{
203-
var url = GetRealEndpoint(request)
204-
+ HttpUtils.AddUrlPath(request.Path, _credential.GetPathParamDictionary())
205-
+ (IsNullOrEmpty(request.QueryParams) ? "" : "?" + request.QueryParams);
206-
return _sync_http(url, methodType.ToUpper(), request);
225+
while (true)
226+
{
227+
var url = GetRealEndpoint(request) + HttpUtils.AddUrlPath(request.Path, _credential.GetPathParamDictionary())
228+
+ (IsNullOrEmpty(request.QueryParams) ? "" : "?" + request.QueryParams);
229+
try
230+
{
231+
return _sync_http(url, methodType.ToUpper(), request);
232+
}
233+
catch (HostUnreachableException hostUnreachableException)
234+
{
235+
if (this._endpointIndex < this._endpoints.Count - 1)
236+
{
237+
Interlocked.Increment(ref _endpointIndex);
238+
}
239+
else
240+
{
241+
throw hostUnreachableException;
242+
}
243+
}
244+
}
207245
}
208246

209247
private HttpResponseMessage _sync_http(string url, string method, SdkRequest sdkRequest)
@@ -222,23 +260,24 @@ private HttpResponseMessage _sync_http(string url, string method, SdkRequest sdk
222260
}
223261
catch (AggregateException aggregateException)
224262
{
225-
throw new ConnectionException(ExceptionUtils.GetMessageFromAggregateException(aggregateException));
263+
throw ExceptionUtils.HandleException(aggregateException);
226264
}
227265
}
228266

229267
private string GetRealEndpoint(SdkRequest request)
230268
{
269+
var endpoint = this._endpoints[_endpointIndex];
231270
if (String.IsNullOrEmpty(request.Cname))
232271
{
233-
return _endpoint;
272+
return endpoint;
234273
}
235274

236-
return _endpoint.Insert(8, request.Cname + ".");
237-
}
275+
return endpoint.Insert(8, request.Cname + ".");
276+
}
238277

239278
private HttpResponseMessage GetResult(HttpResponseMessage responseMessage)
240279
{
241-
if ((int) responseMessage.StatusCode < 400)
280+
if ((int)responseMessage.StatusCode < 400)
242281
{
243282
return responseMessage;
244283
}
@@ -274,4 +313,4 @@ private void UpdateHeaders(HttpRequest request, Dictionary<string, string> heade
274313
request.Headers.Add(XRequestAgent, "g42cloud-usdk-net/3.0");
275314
}
276315
}
277-
}
316+
}

Core/Exception/ConnectionException.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,20 @@
1919
* under the License.
2020
*/
2121

22+
using System;
23+
2224
namespace G42Cloud.SDK.Core
2325
{
2426
public class ConnectionException : SdkException
2527
{
26-
public string ErrorMessage { get; set; }
27-
2828
public ConnectionException(string errorMessage)
2929
{
3030
this.ErrorMessage = errorMessage;
3131
}
32+
33+
public ConnectionException(string errorMessage, Exception innerException) : base(errorMessage, innerException)
34+
{
35+
this.ErrorMessage = errorMessage;
36+
}
3237
}
3338
}

Core/Exception/HostUnreachableException.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,20 @@
1919
* under the License.
2020
*/
2121

22+
using System;
23+
2224
namespace G42Cloud.SDK.Core
2325
{
2426
public class HostUnreachableException : ConnectionException
2527
{
26-
public HostUnreachableException(string errorMessage):base(errorMessage)
28+
public HostUnreachableException(string errorMessage) : base(errorMessage)
29+
{
30+
this.ErrorMessage = errorMessage;
31+
}
32+
33+
public HostUnreachableException(string errorMessage, Exception innerException) : base(errorMessage, innerException)
2734
{
2835
this.ErrorMessage = errorMessage;
2936
}
3037
}
31-
}
38+
}

Core/Exception/RequestTimeoutException.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,20 @@
1919
* under the License.
2020
*/
2121

22+
using System;
23+
2224
namespace G42Cloud.SDK.Core
2325
{
2426
public class RequestTimeoutException : SdkException
2527
{
26-
public string ErrorMessage { get; set; }
27-
2828
public RequestTimeoutException(string errorMessage)
2929
{
3030
this.ErrorMessage = errorMessage;
3131
}
32+
33+
public RequestTimeoutException(string errorMessage, Exception innerException) : base(errorMessage, innerException)
34+
{
35+
this.ErrorMessage = errorMessage;
36+
}
3237
}
3338
}

Core/Exception/SdkError.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,15 @@ public class SdkError : SdkResponse
3131
[XmlElement("Message")]
3232
public string ErrorMsg { get; set; }
3333

34-
[JsonProperty("error_code", NullValueHandling = NullValueHandling.Ignore)]
35-
[XmlElement("Code")]
34+
[JsonProperty("error_code", NullValueHandling = NullValueHandling.Ignore)] [XmlElement("Code")]
3635
public string ErrorCode;
3736

38-
[JsonProperty("request_id", NullValueHandling = NullValueHandling.Ignore)]
39-
[XmlElement("RequestId")]
37+
[JsonProperty("request_id", NullValueHandling = NullValueHandling.Ignore)] [XmlElement("RequestId")]
4038
public string RequestId;
4139

40+
[JsonProperty("encoded_authorization_message", NullValueHandling = NullValueHandling.Ignore)] [XmlElement("EncodedAuthorizationMessage")]
41+
public string EncodedAuthorizationMessage;
42+
4243
public SdkError()
4344
{
4445
}
@@ -49,16 +50,16 @@ public SdkError(string errorCode, string errorMsg, string requestId)
4950
this.ErrorMsg = errorMsg;
5051
this.RequestId = requestId;
5152
}
52-
53+
5354
public SdkError(string errorCode, string errorMsg)
5455
{
5556
this.ErrorCode = errorCode;
5657
this.ErrorMsg = errorMsg;
5758
}
58-
59+
5960
public SdkError(string errorMsg)
6061
{
6162
this.ErrorMsg = errorMsg;
6263
}
6364
}
64-
}
65+
}

Core/Exception/SdkException.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,20 @@ namespace G42Cloud.SDK.Core
2525
{
2626
public class SdkException : Exception
2727
{
28+
protected SdkException()
29+
{
30+
31+
}
32+
public string ErrorMessage { get; set; }
33+
34+
public SdkException(string errorMessage) : base(errorMessage)
35+
{
36+
this.ErrorMessage = errorMessage;
37+
}
38+
39+
public SdkException(string errorMessage, Exception innerException) : base(errorMessage, innerException)
40+
{
41+
this.ErrorMessage = errorMessage;
42+
}
2843
}
2944
}

Core/Exception/ServiceResponseException.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,15 @@ public class ServiceResponseException : SdkException
3131

3232
public string RequestId { get; set; }
3333

34+
public string EncodedAuthorizationMessage { get; set; }
35+
3436
public ServiceResponseException(int? httpStatusCode, SdkError sdkError)
3537
{
3638
this.HttpStatusCode = httpStatusCode;
3739
this.ErrorCode = sdkError.ErrorCode;
3840
this.ErrorMsg = sdkError.ErrorMsg;
3941
this.RequestId = sdkError.RequestId;
42+
this.EncodedAuthorizationMessage = sdkError.EncodedAuthorizationMessage;
4043
}
4144

4245
public static ServiceResponseException MapException(int? httpStatusCode, SdkError sdkError)
@@ -54,4 +57,4 @@ public static ServiceResponseException MapException(int? httpStatusCode, SdkErro
5457
return new ServiceResponseException(httpStatusCode, sdkError);
5558
}
5659
}
57-
}
60+
}

0 commit comments

Comments
 (0)