protected async Task <T> GetOneAsync <T>(string path, object parametros = null) { _url.AppendPathSegments(_controllerUrl, path); if (parametros != null) { _url.SetQueryParams(parametros); } var ret = await _url.GetJsonAsync <T>(); return(ret); // catch (FlurlHttpException ex) //{ // var status = ex.Call.HttpResponseMessage.StatusCode; // var message = await ex.GetResponseStringAsync(); // IsSuccessStatusCode = false; //} //catch (Exception ex) //{ // throw; //} }
/// <summary> /// Generic request for the given resource asynchronously. /// </summary> /// <param name="resource">The main resource to read.</param> /// <param name="parameters">Query string parameters in Key/Value format.</param> /// <param name="pathSegments">Path segments to be used in combination with the main resource.</param> /// <returns></returns> /// <exception cref="MetasysHttpParsingException"></exception> /// <exception cref="MetasysHttpTimeoutException"></exception> /// <exception cref="MetasysHttpException"></exception> /// <exception cref="MetasysHttpNotFoundException"></exception> protected async Task <JToken> GetRequestAsync(string resource, Dictionary <string, string> parameters = null, params object[] pathSegments) { JToken response = null; // Create URL with base resource Url url = new Url(resource); // Concatenate segments with base resource url url.AppendPathSegments(pathSegments); // Set query parameters according to the input dictionary if (parameters != null) { foreach (var p in parameters) { url.SetQueryParam(p.Key, p.Value); } } try { response = await Client.Request(url) .GetJsonAsync <JToken>() .ConfigureAwait(false); } catch (FlurlHttpException e) { ThrowHttpException(e); } return(response); }
/// <summary> /// Perform multiple requests (PUT) to the Server with a single HTTP call asynchronously. /// </summary> /// <param name="endpoint"></param> /// <param name="requests"></param> /// <param name="paths"></param> /// <returns></returns> protected async Task <JToken> PutBatchRequestAsync(string endpoint, IEnumerable <BatchRequestParam> requests, params string[] paths) { // Create URL with base resource Url url = new Url(endpoint); // Concatenate batch segment to use batch request and prepare the list of requests url.AppendPathSegments("batch"); var objectsRequests = new List <ObjectRequest>(); // Concatenate batch segment to use batch request and prepare the list of requests foreach (var r in requests) { Url relativeUrl = new Url(r.ObjectId.ToString()); relativeUrl.AppendPathSegments(paths); // e.g. "00000000-0000-0000-0000-000000000001/annotations" object body; switch (paths[0]) { case "discard": string annotationText = r.Resource; body = new { annotationText }; break; default: body = null; break; } ; // Use the object id concatenated to the resource to uniquely identify each request objectsRequests.Add(new ObjectRequest { Id = r.ObjectId.ToString() + '_' + r.Resource, RelativeUrl = relativeUrl, Body = body }); //Body = new ObjectBody {Text = r.Resource } } JToken responseToken = null; try { // Post the list of requests and return responses as JToken var response = await Client.Request(url) .PostJsonAsync(new BatchRequest { Method = "PUT", Requests = objectsRequests }) .ConfigureAwait(false); responseToken = JToken.Parse(await response.Content.ReadAsStringAsync().ConfigureAwait(false)); } catch (FlurlHttpException e) { ThrowHttpException(e); } return(responseToken); }
/// <summary> /// Bulk creates multiple ports. /// </summary> /// <param name="ports">The port definitions.</param> /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> /// <returns> /// The created subnets. /// </returns> public virtual async Task <PreparedRequest> CreatePortsAsync(IEnumerable <object> ports, CancellationToken cancellationToken = default(CancellationToken)) { Url endpoint = await Endpoint.GetEndpoint(cancellationToken).ConfigureAwait(false); return(endpoint .AppendPathSegments("ports") .Authenticate(AuthenticationProvider) .PreparePostJson(new PortDefinitionCollection(ports), cancellationToken)); }
/// <summary> /// Gets the specified network. /// </summary> /// <param name="networkId">The network identifier.</param> /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> /// <returns> /// The network associated with the specified identifier. /// </returns> public virtual async Task <PreparedRequest> GetNetworkAsync(string networkId, CancellationToken cancellationToken = default(CancellationToken)) { Url endpoint = await Endpoint.GetEndpoint(cancellationToken).ConfigureAwait(false); return(endpoint .AppendPathSegments("networks", networkId) .Authenticate(AuthenticationProvider) .PrepareGet(cancellationToken)); }
/// <summary> /// Creates a network. /// </summary> /// <param name="network">The network definition.</param> /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> /// <returns> /// The created network. /// </returns> public virtual async Task <PreparedRequest> CreateNetworkAsync(object network, CancellationToken cancellationToken = default(CancellationToken)) { Url endpoint = await Endpoint.GetEndpoint(cancellationToken).ConfigureAwait(false); return(endpoint .AppendPathSegments("networks") .Authenticate(AuthenticationProvider) .PreparePostJson(network, cancellationToken)); }
/// <summary> /// Lists all networks associated with the account. /// </summary> /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> /// <returns> /// A collection of network resources associated with the account. /// </returns> public async Task <PreparedRequest> ListNetworksAsync(CancellationToken cancellationToken = default(CancellationToken)) { Url endpoint = await UrlBuilder.GetEndpoint(cancellationToken).ConfigureAwait(false); return(endpoint .AppendPathSegments("networks") .Authenticate(AuthenticationProvider) .PrepareGet(cancellationToken)); }
/// <summary> /// Gets the specified port. /// </summary> /// <param name="portId">The port identifier.</param> /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> /// <returns> /// The port associated with the specified identifier. /// </returns> public virtual async Task <PreparedRequest> GetPortAsync(string portId, CancellationToken cancellationToken = default(CancellationToken)) { Url endpoint = await UrlBuilder.GetEndpoint(cancellationToken).ConfigureAwait(false); return(endpoint .AppendPathSegments("ports", portId) .Authenticate(AuthenticationProvider) .PrepareGet(cancellationToken)); }
/// <summary> /// Creates a port. /// </summary> /// <param name="port">The port definition.</param> /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> /// <returns> /// The created port. /// </returns> public virtual async Task <PreparedRequest> CreatePortAsync(object port, CancellationToken cancellationToken = default(CancellationToken)) { Url endpoint = await UrlBuilder.GetEndpoint(cancellationToken).ConfigureAwait(false); return(endpoint .AppendPathSegments("ports") .Authenticate(AuthenticationProvider) .PreparePostJson(port, cancellationToken)); }
/// <summary> /// Gets the specified RackConnect Cloud Network. /// </summary> /// <param name="networkId">The network identifier.</param> /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> public async Task <NetworkReference> GetNetworkAsync(Identifier networkId, CancellationToken cancellationToken = default(CancellationToken)) { Url endpoint = await _urlBuilder.GetEndpoint(cancellationToken).ConfigureAwait(false); return(await endpoint .AppendPathSegments("cloud_networks", networkId) .Authenticate(_authenticationProvider) .GetJsonAsync <NetworkReference>(cancellationToken) .ConfigureAwait(false)); }
/// <summary> /// Lists Cloud Networks associated with a RackConnect Configuration. /// </summary> /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> /// <returns> /// A collection of networks associated with the account. /// </returns> public async Task <IEnumerable <NetworkReference> > ListNetworksAsync(CancellationToken cancellationToken = default(CancellationToken)) { Url endpoint = await _urlBuilder.GetEndpoint(cancellationToken).ConfigureAwait(false); return(await endpoint .AppendPathSegments("cloud_networks") .Authenticate(_authenticationProvider) .GetJsonAsync <IEnumerable <NetworkReference> >(cancellationToken) .ConfigureAwait(false)); }
protected async Task <T> Post <T>(string path, Action <CapturedMultipartContent> buildContent) where T : class { _url = _servidor; _url.AppendPathSegments(_controllerUrl, path); var ret = await _url.PostMultipartAsync(mp => buildContent(mp)); return(await ret.GetJsonAsync <T>()); }
/// <summary> /// Deletes the specified port. /// </summary> /// <param name="portId">The port identifier.</param> /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> public virtual async Task <PreparedRequest> DeletePortAsync(string portId, CancellationToken cancellationToken = default(CancellationToken)) { Url endpoint = await Endpoint.GetEndpoint(cancellationToken).ConfigureAwait(false); return((PreparedRequest)endpoint .AppendPathSegments("ports", portId) .Authenticate(AuthenticationProvider) .PrepareDelete(cancellationToken) .AllowHttpStatus(HttpStatusCode.NotFound)); }
protected async Task <T> Post <T>(string path, object obj) { _url = _servidor; _url.AppendPathSegments(_controllerUrl, path); var ret = _url.PostJsonAsync(obj); IsSuccessStatusCode = ret.IsCompleted; return(await ret.ReceiveJson <T>()); }
protected async Task <T> Post <T>(string path, byte[] upfilebytes, string nomeparametro, string nomearquivo, object obj) where T : class { _url = _servidor; _url.AppendPathSegments(_controllerUrl, path); var ret = await _url.PostMultipartAsync(mp => mp .AddFile(nomeparametro, upfilebytes.ToStream(), fileName : nomearquivo) .AddStringParts(obj) ); return(await ret.GetJsonAsync <T>()); }
/// <summary> /// Builds a <see cref="ListSecurityGroupRulesAsync{T}"/> request. /// </summary> /// <param name="queryString">Options for filtering.</param> /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> public async Task <PreparedRequest> BuildListSecurityGroupRulesRequest(IQueryStringBuilder queryString, CancellationToken cancellationToken = default(CancellationToken)) { Url endpoint = await Endpoint.GetEndpoint(cancellationToken).ConfigureAwait(false); var request = endpoint .AppendPathSegments("security-group-rules") .Authenticate(AuthenticationProvider) .PrepareGet(cancellationToken); request.Url.SetQueryParams(queryString?.Build()); return(request); }
/// <summary> /// Gets the specified public IP address. /// </summary> /// <param name="publicIPId">The public IP address identifier.</param> /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> public async Task <PublicIP> GetPublicIPAsync(Identifier publicIPId, CancellationToken cancellationToken = default(CancellationToken)) { Url endpoint = await _urlBuilder.GetEndpoint(cancellationToken).ConfigureAwait(false); var ip = await endpoint .AppendPathSegments("public_ips", publicIPId) .Authenticate(_authenticationProvider) .GetJsonAsync <PublicIP>(cancellationToken) .ConfigureAwait(false); SetOwner(ip); return(ip); }
/// <summary> /// Removes the public IP address from the current account. /// </summary> /// <param name="publicIPId">The public IP address identifier.</param> /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> public async Task DeletePublicIPAsync(Identifier publicIPId, CancellationToken cancellationToken = default(CancellationToken)) { if (publicIPId == null) { throw new ArgumentNullException("publicIPId"); } Url endpoint = await _urlBuilder.GetEndpoint(cancellationToken).ConfigureAwait(false); await endpoint .AppendPathSegments("public_ips", publicIPId) .Authenticate(_authenticationProvider) .AllowHttpStatus(HttpStatusCode.NotFound) .DeleteAsync(cancellationToken) .ConfigureAwait(false); }
/// <summary> /// Perform multiple requests (GET) to the Server with a single HTTP call asynchronously. /// </summary> /// <param name="endpoint"></param> /// <param name="ids"></param> /// <param name="resources"></param> /// <param name="paths"></param> /// <returns></returns> protected async Task <JToken> GetBatchRequestAsync(string endpoint, IEnumerable <Guid> ids, IEnumerable <string> resources, params string[] paths) { // Create URL with base resource Url url = new Url(endpoint); // Concatenate batch segment to use batch request and prepare the list of requests url.AppendPathSegments("batch"); var objectsRequests = new List <ObjectRequest>(); // Concatenate batch segment to use batch request and prepare the list of requests foreach (var id in ids) { foreach (var r in resources) { Url relativeUrl = new Url(id.ToString()); relativeUrl.AppendPathSegments(paths); // e.g. "00000000-0000-0000-0000-000000000001/attributes" relativeUrl.AppendPathSegment(r); // e.g. "00000000-0000-0000-0000-000000000001/attributes/presentValue" // Use the object id concatenated to the resource to uniquely identify each request objectsRequests.Add(new ObjectRequest { Id = id.ToString() + '_' + r, RelativeUrl = relativeUrl }); } } JToken responseToken = null; try { // Post the list of requests and return responses as JToken var response = await Client.Request(url) .PostJsonAsync(new BatchRequest { Method = "GET", Requests = objectsRequests }) .ConfigureAwait(false); responseToken = JToken.Parse(await response.Content.ReadAsStringAsync().ConfigureAwait(false)); } catch (FlurlHttpException e) { ThrowHttpException(e); } return(responseToken); }
/// <summary> /// Lists all public IP addresses associated with the account. /// </summary> /// <param name="filter">Optional filter parameters.</param> /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> /// <returns> /// A collection of public IP addresses associated with the account. /// </returns> public async Task <IEnumerable <PublicIP> > ListPublicIPsAsync(ListPublicIPsFilter filter = null, CancellationToken cancellationToken = default(CancellationToken)) { filter = filter ?? new ListPublicIPsFilter(); Url endpoint = await _urlBuilder.GetEndpoint(cancellationToken).ConfigureAwait(false); var ips = await endpoint .AppendPathSegments("public_ips") .SetQueryParams(new { cloud_server_id = filter.ServerId, retain = filter.IsRetained }) .Authenticate(_authenticationProvider) .GetJsonAsync <IEnumerable <PublicIP> >(cancellationToken) .ConfigureAwait(false); foreach (var ip in ips) { SetOwner(ip); } return(ips); }