Skip to content

Commit

Permalink
release 0.0.5-beta source code for net
Browse files Browse the repository at this point in the history
  • Loading branch information
unionsdk committed Apr 14, 2023
1 parent d5983a4 commit 081f0b2
Show file tree
Hide file tree
Showing 58 changed files with 693 additions and 218 deletions.
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
# 0.0.4-beta # 0.0.4-beta 2023-02-20
# 0.0.5-beta 2023-04-14

### G42Cloud SDK Core

- _Features_
- None
- _Bug Fix_
- None
- _Change_
- Optimize the code structure.

# 0.0.4-beta 2023-02-20

### G42Cloud SDK CBR

Expand Down
8 changes: 4 additions & 4 deletions Core/Auth/IamService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static string KeystoneListProjects(SdkHttpClient client, HttpRequest requ
var message = client.InitHttpRequest(request, true);
try
{
var response = client.DoHttpRequest(message).Result;
var response = TaskUtils.RunSync(() => client.DoHttpRequest(message));
if ((int)response.StatusCode >= 400)
{
throw ExceptionUtils.GetException(response);
Expand All @@ -105,7 +105,7 @@ public static string KeystoneListProjects(SdkHttpClient client, HttpRequest requ
}
catch (AggregateException aggregateException)
{
throw new ConnectionException(ExceptionUtils.GetMessageFromAggregateException(aggregateException));
throw ExceptionUtils.HandleException(aggregateException);
}
}

Expand All @@ -129,7 +129,7 @@ public static string KeystoneListAuthDomains(SdkHttpClient client, HttpRequest r
var message = client.InitHttpRequest(request, true);
try
{
var response = client.DoHttpRequest(message).Result;
var response = TaskUtils.RunSync(() => client.DoHttpRequest(message));
if ((int)response.StatusCode >= 400)
{
throw ExceptionUtils.GetException(response);
Expand All @@ -148,7 +148,7 @@ public static string KeystoneListAuthDomains(SdkHttpClient client, HttpRequest r
}
catch (AggregateException aggregateException)
{
throw new ConnectionException(ExceptionUtils.GetMessageFromAggregateException(aggregateException));
throw ExceptionUtils.HandleException(aggregateException);
}
}
}
Expand Down
85 changes: 62 additions & 23 deletions Core/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using G42Cloud.SDK.Core.Auth;
using Microsoft.Extensions.Logging;
Expand All @@ -34,7 +37,10 @@ public class Client
{
public class ClientBuilder<T> where T : Client
{
private string[] CredentialType { get; } = {nameof(BasicCredentials)};
private string[] CredentialType { get; } =
{
nameof(BasicCredentials)
};

public ClientBuilder()
{
Expand All @@ -48,7 +54,7 @@ public ClientBuilder(string credentialType)
private Credentials _credentials;
private HttpConfig _httpConfig;
private Region _region;
private string _endPoint;
private List<string> _endpoints;
private bool _enableLogging;
private LogLevel _logLevel = LogLevel.Information;
private HttpHandler _httpHandler;
Expand All @@ -74,10 +80,19 @@ public ClientBuilder<T> WithRegion(Region region)
this._region = region;
return this;
}

[Obsolete("As of 3.1.26, because of the support of the multi-endpoint feature, use WithEndPoints instead")]
public ClientBuilder<T> WithEndPoint(string endpoint)
{
return this.WithEndPoints(new List<string>
{
endpoint
});
}

public ClientBuilder<T> WithEndPoint(string endPoint)
public ClientBuilder<T> WithEndPoints(List<string> endpoints)
{
this._endPoint = endPoint;
this._endpoints = endpoints;
return this;
}

Expand Down Expand Up @@ -120,24 +135,29 @@ public T Build()

if (this._region != null)
{
this._endPoint = _region.Endpoint;
this._endpoints = this._region.Endpoints;
this._credentials = _credentials.ProcessAuthParams(client._sdkHttpClient, _region.Id);
this._credentials.ProcessDerivedAuthParams(_derivedAuthServiceName, _region.Id);
}

if (!_endPoint.StartsWith(HttpScheme))
for (var i = 0; i < _endpoints.Count; i++)
{
_endPoint = HttpsScheme + "://" + _endPoint;
var endpoint = _endpoints[i];
if (!endpoint.StartsWith(HttpScheme))
{
_endpoints[i] = HttpsScheme + "://" + endpoint;
}
}

client.WithCredential(this._credentials)
.WithEndPoint(this._endPoint);
.WithEndPoints(this._endpoints);

return (T) client;
return (T)client;
}
}

private string _endpoint;
private List<string> _endpoints;
private volatile int _endpointIndex;
private HttpConfig _httpConfig;
private Credentials _credential;

Expand All @@ -158,12 +178,13 @@ private Client WithHttpConfig(HttpConfig httpConfig)
return this;
}

private Client WithEndPoint(string endPoint)
private Client WithEndPoints(List<string> endpoints)
{
this._endpoint = endPoint;
this._endpoints = endpoints;
return this;
}


private void InitSdkHttpClient(HttpHandler httpHandler, bool enableLogging, LogLevel logLevel)
{
this._sdkHttpClient =
Expand Down Expand Up @@ -194,16 +215,33 @@ private async Task<HttpResponseMessage> _async_http(string url, string method, S
}
catch (AggregateException aggregateException)
{
throw new ConnectionException(ExceptionUtils.GetMessageFromAggregateException(aggregateException));
throw ExceptionUtils.HandleException(aggregateException);
}
}

[MethodImpl(MethodImplOptions.Synchronized)]
protected HttpResponseMessage DoHttpRequestSync(string methodType, SdkRequest request)
{
var url = GetRealEndpoint(request)
+ HttpUtils.AddUrlPath(request.Path, _credential.GetPathParamDictionary())
+ (IsNullOrEmpty(request.QueryParams) ? "" : "?" + request.QueryParams);
return _sync_http(url, methodType.ToUpper(), request);
while (true)
{
var url = GetRealEndpoint(request) + HttpUtils.AddUrlPath(request.Path, _credential.GetPathParamDictionary())
+ (IsNullOrEmpty(request.QueryParams) ? "" : "?" + request.QueryParams);
try
{
return _sync_http(url, methodType.ToUpper(), request);
}
catch (HostUnreachableException hostUnreachableException)
{
if (this._endpointIndex < this._endpoints.Count - 1)
{
Interlocked.Increment(ref _endpointIndex);
}
else
{
throw hostUnreachableException;
}
}
}
}

private HttpResponseMessage _sync_http(string url, string method, SdkRequest sdkRequest)
Expand All @@ -222,23 +260,24 @@ private HttpResponseMessage _sync_http(string url, string method, SdkRequest sdk
}
catch (AggregateException aggregateException)
{
throw new ConnectionException(ExceptionUtils.GetMessageFromAggregateException(aggregateException));
throw ExceptionUtils.HandleException(aggregateException);
}
}

private string GetRealEndpoint(SdkRequest request)
{
var endpoint = this._endpoints[_endpointIndex];
if (String.IsNullOrEmpty(request.Cname))
{
return _endpoint;
return endpoint;
}

return _endpoint.Insert(8, request.Cname + ".");
}
return endpoint.Insert(8, request.Cname + ".");
}

private HttpResponseMessage GetResult(HttpResponseMessage responseMessage)
{
if ((int) responseMessage.StatusCode < 400)
if ((int)responseMessage.StatusCode < 400)
{
return responseMessage;
}
Expand Down Expand Up @@ -274,4 +313,4 @@ private void UpdateHeaders(HttpRequest request, Dictionary<string, string> heade
request.Headers.Add(XRequestAgent, "g42cloud-usdk-net/3.0");
}
}
}
}
9 changes: 7 additions & 2 deletions Core/Exception/ConnectionException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@
* under the License.
*/

using System;

namespace G42Cloud.SDK.Core
{
public class ConnectionException : SdkException
{
public string ErrorMessage { get; set; }

public ConnectionException(string errorMessage)
{
this.ErrorMessage = errorMessage;
}

public ConnectionException(string errorMessage, Exception innerException) : base(errorMessage, innerException)
{
this.ErrorMessage = errorMessage;
}
}
}
11 changes: 9 additions & 2 deletions Core/Exception/HostUnreachableException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,20 @@
* under the License.
*/

using System;

namespace G42Cloud.SDK.Core
{
public class HostUnreachableException : ConnectionException
{
public HostUnreachableException(string errorMessage):base(errorMessage)
public HostUnreachableException(string errorMessage) : base(errorMessage)
{
this.ErrorMessage = errorMessage;
}

public HostUnreachableException(string errorMessage, Exception innerException) : base(errorMessage, innerException)
{
this.ErrorMessage = errorMessage;
}
}
}
}
9 changes: 7 additions & 2 deletions Core/Exception/RequestTimeoutException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@
* under the License.
*/

using System;

namespace G42Cloud.SDK.Core
{
public class RequestTimeoutException : SdkException
{
public string ErrorMessage { get; set; }

public RequestTimeoutException(string errorMessage)
{
this.ErrorMessage = errorMessage;
}

public RequestTimeoutException(string errorMessage, Exception innerException) : base(errorMessage, innerException)
{
this.ErrorMessage = errorMessage;
}
}
}
15 changes: 8 additions & 7 deletions Core/Exception/SdkError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ public class SdkError : SdkResponse
[XmlElement("Message")]
public string ErrorMsg { get; set; }

[JsonProperty("error_code", NullValueHandling = NullValueHandling.Ignore)]
[XmlElement("Code")]
[JsonProperty("error_code", NullValueHandling = NullValueHandling.Ignore)] [XmlElement("Code")]
public string ErrorCode;

[JsonProperty("request_id", NullValueHandling = NullValueHandling.Ignore)]
[XmlElement("RequestId")]
[JsonProperty("request_id", NullValueHandling = NullValueHandling.Ignore)] [XmlElement("RequestId")]
public string RequestId;

[JsonProperty("encoded_authorization_message", NullValueHandling = NullValueHandling.Ignore)] [XmlElement("EncodedAuthorizationMessage")]
public string EncodedAuthorizationMessage;

public SdkError()
{
}
Expand All @@ -49,16 +50,16 @@ public SdkError(string errorCode, string errorMsg, string requestId)
this.ErrorMsg = errorMsg;
this.RequestId = requestId;
}

public SdkError(string errorCode, string errorMsg)
{
this.ErrorCode = errorCode;
this.ErrorMsg = errorMsg;
}

public SdkError(string errorMsg)
{
this.ErrorMsg = errorMsg;
}
}
}
}
15 changes: 15 additions & 0 deletions Core/Exception/SdkException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,20 @@ namespace G42Cloud.SDK.Core
{
public class SdkException : Exception
{
protected SdkException()
{

}
public string ErrorMessage { get; set; }

public SdkException(string errorMessage) : base(errorMessage)
{
this.ErrorMessage = errorMessage;
}

public SdkException(string errorMessage, Exception innerException) : base(errorMessage, innerException)
{
this.ErrorMessage = errorMessage;
}
}
}
5 changes: 4 additions & 1 deletion Core/Exception/ServiceResponseException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ public class ServiceResponseException : SdkException

public string RequestId { get; set; }

public string EncodedAuthorizationMessage { get; set; }

public ServiceResponseException(int? httpStatusCode, SdkError sdkError)
{
this.HttpStatusCode = httpStatusCode;
this.ErrorCode = sdkError.ErrorCode;
this.ErrorMsg = sdkError.ErrorMsg;
this.RequestId = sdkError.RequestId;
this.EncodedAuthorizationMessage = sdkError.EncodedAuthorizationMessage;
}

public static ServiceResponseException MapException(int? httpStatusCode, SdkError sdkError)
Expand All @@ -54,4 +57,4 @@ public static ServiceResponseException MapException(int? httpStatusCode, SdkErro
return new ServiceResponseException(httpStatusCode, sdkError);
}
}
}
}
Loading

0 comments on commit 081f0b2

Please sign in to comment.