Exemplo n.º 1
0
        /// <summary>
        /// Default method handler for any method calls which are not implemented
        /// </summary>
        /// <param name="methodRequest"></param>
        /// <param name="userContext"></param>
        /// <returns></returns>
        private static Task <MethodResponse> DefaultMethodHandler(MethodRequest methodRequest, object userContext)
        {
            Log.Information($"Received method invocation for non-existing method {methodRequest.Name}. Returning 404.");
            var result = new RestMethodResponsePayload()
            {
                Error = $"Method {methodRequest.Name} not implemented"
            };
            var outResult = JsonConvert.SerializeObject(result, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            });

            return(Task.FromResult(new MethodResponse(Encoding.UTF8.GetBytes(outResult), 404)));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Function to call external REST endpoints
        /// </summary>
        /// <param name="request"></param>
        /// <param name="httpMethod"></param>
        /// <returns></returns>
        private static async Task <MethodResponse> ExecuteRestCall(RestMethodRequestPayload request, HttpMethod httpMethod)
        {
            Log.Information($"Received REST call request for method {httpMethod.ToString()}");
            var result     = new RestMethodResponsePayload();
            int returnCode = 200;

            try
            {
                if (request == null || string.IsNullOrEmpty(request.Url))
                {
                    result.Error = "No valid method payload received. At least Url parameter required in request payload.";
                    returnCode   = 400;

                    Log.Error("No valid method payload received");
                }
                else
                {
                    result.ClientUrl = request.Url;
                    Log.Information($"Executing HTTP {httpMethod} method against endpoint {request.Url} with timeout of {_httpClient.Timeout}");

                    HttpResponseMessage response;
                    if (httpMethod == HttpMethod.Get)
                    {
                        response = await _httpClient.GetAsync(request.Url).ConfigureAwait(false);
                    }
                    else if (httpMethod == HttpMethod.Post)
                    {
                        // For now we exepct - and only support - JSON payload
                        var content = new StringContent(request.RequestPayload, Encoding.UTF8, "application/json");
                        response = await _httpClient.PostAsync(request.Url, content).ConfigureAwait(false);
                    }
                    else
                    {
                        throw new NotImplementedException($"HTTP method {httpMethod.ToString()} is currently not supported by the module.");
                    }

                    response.EnsureSuccessStatusCode();
                    Log.Information($"HTTP operation successfully completed StatusCode={response.StatusCode}.");

                    result.ClientResponse = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                    Log.Debug($"Endpoint response:");
                    Log.Debug(result.ClientResponse);
                }
            }
            catch (HttpRequestException hre)
            {
                Log.Error($"Response code: {hre.ToString()}");
                result.Error = hre.Message;
                returnCode   = 500;
            }
            catch (Exception ex)
            {
                Log.Error($"Exception: {ex.Message}");
                result.Error = ex.Message;
                returnCode   = 500;
            }
            var outResult = JsonConvert.SerializeObject(result, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            });

            return(new MethodResponse(Encoding.UTF8.GetBytes(outResult), returnCode));
        }