/// <summary> /// This method initializes a new RESTClient. The host URI for Amazon is set to virtual hosted style /// if usePathStyle is false. Otherwise path style URL is constructed. /// </summary> internal void InitClient() { if (string.IsNullOrEmpty(this.BaseUrl)) { throw new InvalidEndpointException("Endpoint cannot be empty."); } string host = this.BaseUrl; var scheme = this.Secure ? utils.UrlEncode("https") : utils.UrlEncode("http"); // This is the actual url pointed to for all HTTP requests this.Endpoint = string.Format("{0}://{1}", scheme, host); this.uri = RequestUtil.GetEndpointURL(this.BaseUrl, this.Secure); RequestUtil.ValidateEndpoint(this.uri, this.Endpoint); // Initialize a new REST client. This uri will be modified if region specific endpoint/virtual style request // is decided upon while constructing a request for Amazon. restClient = new RestSharp.RestClient(this.uri) { UserAgent = this.FullUserAgent }; authenticator = new V4Authenticator(this.Secure, this.AccessKey, this.SecretKey, this.Region, this.SessionToken); restClient.Authenticator = authenticator; restClient.UseUrlEncoder(s => HttpUtility.UrlEncode(s)); }
private async Task <ResponseResult> ExecuteTaskCoreAsync( IEnumerable <ApiResponseErrorHandlingDelegate> errorHandlers, HttpRequestMessageBuilder requestMessageBuilder, CancellationToken cancellationToken = default(CancellationToken)) { var startTime = DateTime.Now; // Logs full url when HTTPtracing is enabled. if (this.trace) { var fullUrl = requestMessageBuilder.RequestUri; } var v4Authenticator = new V4Authenticator(this.Secure, this.AccessKey, this.SecretKey, this.Region, this.SessionToken); requestMessageBuilder.AddOrUpdateHeaderParameter("Authorization", v4Authenticator.Authenticate(requestMessageBuilder)); HttpRequestMessage request = requestMessageBuilder.Request; ResponseResult responseResult; try { if (requestTimeout > 0) { this.HttpClient.Timeout = new TimeSpan(0, 0, 0, 0, requestTimeout); } var response = await this.HttpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken) .ConfigureAwait(false); responseResult = new ResponseResult(request, response); if (requestMessageBuilder.ResponseWriter != null) { requestMessageBuilder.ResponseWriter(responseResult.ContentStream); } } catch (OperationCanceledException) { throw; } catch (Exception e) { responseResult = new ResponseResult(request, e); } this.HandleIfErrorResponse(responseResult, errorHandlers, startTime); return(responseResult); }
/// <summary> /// This method initializes a new RESTClient. It is called by other Inits /// </summary> internal void Init() { this.uri = RequestUtil.GetEndpointURL(this.BaseUrl, this.Secure); RequestUtil.ValidateEndpoint(this.uri, this.Endpoint); // Initialize a new REST client. This uri will be modified if region specific endpoint/virtual style request // is decided upon while constructing a request for Amazon. restClient = new RestSharp.RestClient(this.uri) { UserAgent = this.FullUserAgent }; authenticator = new V4Authenticator(this.Secure, this.AccessKey, this.SecretKey, this.Region, this.SessionToken); restClient.Authenticator = authenticator; restClient.UseUrlEncoder(s => HttpUtility.UrlEncode(s)); }
/// <summary> /// Creates and returns an Cloud Storage client /// </summary> /// <param name="uri">Location of the server, supports HTTP and HTTPS</param> /// <param name="accessKey">Access Key for authenticated requests</param> /// <param name="secretKey">Secret Key for authenticated requests</param> /// <returns>Client with the uri set as the server location and authentication parameters set.</returns> public MinioClient(Uri uri, string accessKey, string secretKey) { if (uri == null) { throw new NullReferenceException(); } if (!(uri.Scheme == "http" || uri.Scheme == "https")) { throw new UriFormatException("Expecting http or https"); } if (uri.Query.Length != 0) { throw new UriFormatException("Expecting no query"); } if (!(uri.AbsolutePath.Length == 0 || (uri.AbsolutePath.Length == 1 && uri.AbsolutePath[0] == '/'))) { throw new UriFormatException("Expecting AbsolutePath to be empty"); } String path = uri.Scheme + "://" + uri.Host + ":" + uri.Port + "/"; uri = new Uri(path); this.client = new RestClient(uri); this.region = Regions.GetRegion(uri.Host); this.client.UserAgent = this.FullUserAgent; if (accessKey != null && secretKey != null) { this.authenticator = new V4Authenticator(accessKey, secretKey); this.client.Authenticator = new V4Authenticator(accessKey, secretKey); } }