Exemplo n.º 1
0
        private async Task RunApiPostRequestAsync(string resource, object payload, CancellationToken cancellationToken)
        {
            // Run request
            var client  = new RestClient(_options.Value.CtfServerApiBaseUrl);
            var request = new RestRequest(resource, Method.Post);

            request.AddJsonBody(CtfApiRequest.Create(_options.Value.LabId, _cryptoService, payload));
            var response = await client.ExecuteAsync(request, cancellationToken);

            // WORKAROUND: The current implementation of RestSharp silently swallows exceptions; check and throw possible exceptions manually
            if (response.ErrorException != null)
            {
                throw response.ErrorException;
            }
            if (!response.IsSuccessful)
            {
                // Throw an exception which contains the entire response data, for easier debugging

                StringBuilder exceptionContentBuilder = new StringBuilder();
                exceptionContentBuilder.AppendLine($"Resource: {resource}");
                exceptionContentBuilder.AppendLine($"Status: {(int)response.StatusCode} {response.StatusDescription}");

                exceptionContentBuilder.AppendLine();
                exceptionContentBuilder.AppendLine("-- Response content: --");
                exceptionContentBuilder.AppendLine(response.Content ?? "(none)");

                throw new CtfApiException($"The server returned an error status code: {response.StatusCode} {response.StatusDescription}", exceptionContentBuilder.ToString());
            }
        }
Exemplo n.º 2
0
        private async Task RunApiPostRequestAsync(string resource, object payload, CancellationToken cancellationToken = default)
        {
            // Run request
            var client  = new RestClient(_options.Value.CtfServerApiBaseUrl);
            var request = new RestRequest(resource, Method.POST);

            request.AddJsonBody(CtfApiRequest.Create(_options.Value.LabId, _cryptoService, payload));
            var response = await client.ExecuteAsync(request, cancellationToken);

            // WORKAROUND: The current implementation of RestSharp silently swallows exceptions; check and throw possible exceptions manually
            if (response.ErrorException != null)
            {
                throw response.ErrorException;
            }
            if (!response.IsSuccessful)
            {
                throw new WebException("The server returned an error status code: " + response.StatusDescription);
            }
        }