Beispiel #1
0
        /// <summary>
        /// This method calls third-party service and sends request
        /// Client will process request within few days. This is why we are
        /// just posting data and only check is to make sure that request submitted successfuly
        /// </summary>
        /// <param name="request">client details, request sent from front-end, request id</param>
        /// <returns>status of request</returns>
        public string StartPostingProcess(IMyClient myClient,
                                          MyRequest preDefinedRequest,
                                          string requestId)
        {
            string result = string.Empty;

            var request = new MyClientRequest()
            {
                Body        = preDefinedRequest,
                CallBackUrl = $"api/MyTask/RequestReceived/{requestId}"                 //Since this is an exercise it is ok to have hard coded values
            };

            //calling third party service
            //This method just posts data to the third party service
            //We want to make sure that post was successful.
            //"PostData" method has a simple logic to check transaction status
            //the purpose of "response" class is to return status of http rerequest and based on status we can have logic to populate apiResponse which is json
            var response = myClient.PostData(request);

            //process response
            if (response.ResponseType == MyClientResponseTypes.BadRequest || response.ResponseType == MyClientResponseTypes.Error)
            {
                //this is the part where we can have handle error class and take care of errors; we can log error and other details
                //for this exercise we are just returning string
                result = "HRTTP REQUEST ERROR";
            }
            else if (response.ResponseType == MyClientResponseTypes.Success)
            {
                //if we would have some additional logic this is the part where we can have handle success class
                result = "HRTTP REQUEST SUCCESS";
            }

            return(result);
        }
Beispiel #2
0
        public MyClientResponse PostData(MyClientRequest request)
        {
            //in real scenario this will be retrieved from db
            var url      = $"http://example.com";
            var response = HandleMyClientRequest(url, request, Method.POST);

            //since the reference url is not live - i hardcoded response to OK status
            response.StatusCode = HttpStatusCode.OK;

            switch (response.StatusCode)
            {
            case HttpStatusCode.OK:
                return(new MyClientResponse(MyClientResponseTypes.Success)
                {
                    StatusCode = HttpStatusCode.OK
                });

            case HttpStatusCode.BadRequest:
                return(new MyClientResponse(MyClientResponseTypes.BadRequest)
                {
                    StatusCode = HttpStatusCode.BadRequest
                });

            default:
                return(new MyClientResponse(MyClientResponseTypes.Error)
                {
                    StatusCode = HttpStatusCode.InternalServerError
                });
            }
        }
Beispiel #3
0
        public IRestResponse HandleMyClientRequest(string url, MyClientRequest clRequest, Method method)
        {
            var MyRestClient = new RestClient();

            MyRestClient.BaseUrl = new Uri(url);
            var request = new RestRequest(method)
            {
                ReadWriteTimeout = 3600
            };

            //in real scenario we could add more parameters to Header (e.g. token authentication, accept)
            request.AddHeader("Content-Type", "application/json");

            if (method == Method.POST || method == Method.PUT)
            {
                request.AddParameter("application/json", clRequest.Body, ParameterType.RequestBody);
            }

            return(MyRestClient.Execute(request));
        }
Beispiel #4
0
        public async Task <MyClientAPIResponse> CheckRequestStatus(IMyClient myClient, string requestId)
        {
            //this is json response object
            MyClientAPIResponse model = null;

            var request = new MyClientRequest()
            {
                RequestID = requestId
            };


            //we can have a separate table for tracking purpose (Request, Response, RequestUrl, InsertedBy, UpdatedBy, InsertDate, UpdatedDate)
            //before calling service we can log API end point details in to db
            //(since this is exercise table is not created)

            //calling client
            var response = await myClient.CheckRequestStatus(request);

            //after response is received we can update certain columns in the table
            //(since this is exercise table is not created)

            //process response
            if (response.ResponseType == MyClientResponseTypes.BadRequest || response.ResponseType == MyClientResponseTypes.Error)
            {
                //this is the part where we can create error handler class and take care of errors
                //returning null back to business object would indicate that http request failed
                return(null);
            }
            else if (response.ResponseType == MyClientResponseTypes.Success)
            {
                //since client's end point does not exist - i hardcoded json
                response.RawResponse = "{'Status': 'COMPLETED', 'Detail': 'This is my details', 'Body': 'This is responsebody', 'LastActionDate': '05/05/03' }";

                //getting data from response
                model = JsonConvert.DeserializeObject <MyClientAPIResponse>(response.RawResponse);
            }

            return(model);
        }
Beispiel #5
0
        public async Task <MyClientResponse> CheckRequestStatus(MyClientRequest request)
        {
            var url    = $"http://example.com/{request.RequestID}";
            var client = new HttpClient();

            var response = await client.GetAsync(url);

            //since the reference url is not live - i hardcoded response to OK status
            response.StatusCode = HttpStatusCode.OK;

            return(response.IsSuccessStatusCode ?
                   new MyClientResponse(MyClientResponseTypes.Success)
            {
                RawResponse = await response.Content.ReadAsStringAsync(),
                StatusCode = HttpStatusCode.OK,
            } :
                   new MyClientResponse(MyClientResponseTypes.Error)
            {
                RawResponse = string.Empty,
                StatusCode = response.StatusCode
            });
        }