示例#1
0
        public async Task <ActionResponse> ExecuteAsync(string baseUrl, IRestRequest request, string userAgent = "HandlerAutomation", int timeout = 2000)
        {
            var res = new ActionResponse();

            try
            {
                request.AddHeader("Accept", "application/json");
                request.AddHeader("Content-Type", "application/json");
                request.RequestFormat = DataFormat.Json;

                var client = new RestClient {
                    BaseUrl = new Uri(baseUrl), FollowRedirects = false, Timeout = timeout > 0 ? timeout : 2000
                };
                client.UserAgent = userAgent;

                _logHandler.Info($"Send {request.Method} to endpoint {request.Resource}");

                var response = await client.ExecuteAsync(request);

                var responseContent = JsonConvert.DeserializeObject <HandlerResponse>(response.Content);

                if (response.ErrorException == null && (int)response.StatusCode < 400)
                {
                    res = new ActionResponse()
                    {
                        Content    = responseContent,
                        StatusCode = response.StatusCode
                    };

                    _logHandler.Info("Response", res.Content);
                }
                else
                {
                    res = new ActionResponse()
                    {
                        StatusCode = response != null ? response.StatusCode : HttpStatusCode.InternalServerError,
                        Error      = responseContent != null
                        ? responseContent?.ErrorReason
                        : (response != null ? response.ErrorException.Message?.ToString() : "Uknown Erorr"),
                        Content = responseContent,
                    };

                    _logHandler.Error("Bad response", res);
                }
            }
            catch (Exception ex)
            {
                res = new ActionResponse()
                {
                    StatusCode = HttpStatusCode.InternalServerError,
                    Error      = $"Unknown Exception: {((RestException)ex)?.InnerException}, Message: {((RestException)ex)?.Message}",
                    Content    = null
                };
                _logHandler.Error("Exception", res);
            }
            return(res);
        }
示例#2
0
        public async Task <ActionResponse> SetHandlerPosition(int X, int Y, int Z)
        {
            _logHandler.Info($"Set Handler Position cordinates to: {X},{Y},{Z}");

            var actionRequest = new ActionRequest()
            {
                position = new Position()
                {
                    X = X, Y = Y, Z = Z
                }
            };
            var request = new RestRequest($"/Move", Method.POST);
            var data    = JsonConvert.SerializeObject(actionRequest);

            request.AddParameter("application/json", data, ParameterType.RequestBody);
            var response = await _restProvider.ExecuteAsync(HandlerApiUrl, request);

            //ResponseHandler(response, "SetHandlerPosition");
            return(response);
        }
示例#3
0
 internal static void Info(object message) => handler.Info(message);
示例#4
0
 public void Setup()
 {
     logger.Info("\n\n");
     logger.Info($"Test {TestContext.CurrentContext.Test.Name} Start");
 }