Exemplo n.º 1
0
        public async Task <object> Post(HttpRequestMessage requestMessage, [FromBody] JObject request)
        {
            var messageId = requestMessage.GetCorrelationId().ToString();

            TraceSources.AddAdditionalLogItem("RequestMessageId", messageId);
            try
            {
                CheckRequest(request);
                var requestXml     = JsonConvert.DeserializeXmlNode(request.ToString(), "contact");
                var xmlDeclaration = requestXml.CreateXmlDeclaration("1.0", "UTF-8", null);
                using (var stringwriter = new StringWriter())
                    using (var xmltextWriter = new XmlTextWriter(stringwriter))
                    {
                        xmlDeclaration.WriteTo(xmltextWriter);
                        requestXml.WriteTo(xmltextWriter);
                        var requestBodyXml = stringwriter.ToString();
                        var url            = new Uri(Config.Get <string>("Toyota.Gbook.WebSite.Contact.MemberUtilityApi.Url"));
                        using (var client = new HttpClient())
                            using (var m = new HttpRequestMessage
                            {
                                Method = HttpMethod.Post,
                                RequestUri = url,
                                Content = new StringContent(requestBodyXml)
                            })
                            {
                                m.Headers.Add("MessageId", messageId);

                                var xid   = Config.Get <string>("Toyota.Gbook.WebSite.TConnect.XId");
                                var xpass = Config.Get <string>("Toyota.Gbook.WebSite.TConnect.XPassword");
                                m.Headers.Add("X-ID", xid);
                                m.Headers.Add("X-Password", xpass);
                                PCSiteTraceSource.ApiExecute_Api("問合せAPI(オンプレ)", url.ToString(), requestBodyXml);
                                var response = await client.SendAsync(m);

                                var responseXml = await response.Content.ReadAsStringAsync();

                                PCSiteTraceSource.ApiResponsed_Api("問合せAPI(オンプレ)", response.StatusCode, responseXml);

                                if (response.StatusCode == HttpStatusCode.OK)
                                {
                                    var x = new XmlDocument();
                                    x.LoadXml(responseXml);
                                    var code           = x.SelectSingleNode("//contact/result_code").InnerText;
                                    var result_message = x.SelectSingleNode("//contact/result_code_message").InnerText;
                                    if (code == "000000")
                                    {
                                        if (request["registered"]["user_id"] == null)
                                        {
                                            return(new SuccessResponse
                                            {
                                                UserId = "",
                                                Datetime = request["inquiry"]["input_date"].ToString() + request["inquiry"]["input_time"]
                                            });
                                        }
                                        else
                                        {
                                            return(new SuccessResponse
                                            {
                                                UserId = request["registered"]["user_id"].ToString(),
                                                Datetime = request["inquiry"]["input_date"].ToString() + request["inquiry"]["input_time"]
                                            });
                                        }
                                    }
                                    if (code == "100002")
                                    {
                                        //入力チェックエラー
                                        var trace     = PCSiteTraceSource.UnexpectedResultCode_Api(url.ToString(), code);
                                        var errorCode = 0;
                                        trace.TryGetId(out errorCode);
                                        return(requestMessage.CreateResponse(HttpStatusCode.BadRequest));
                                    }
                                    if (code == "300000")
                                    {
                                        //DBアクセスエラー
                                        var trace     = PCSiteTraceSource.UnexpectedResultCode_Api(url.ToString(), code);
                                        var errorCode = 0;
                                        trace.TryGetId(out errorCode);
                                        return(requestMessage.CreateResponse(HttpStatusCode.InternalServerError,
                                                                             new ErrorResponse {
                                            Errors = new Dictionary <string, string> {
                                                { "code", errorCode.ToString() }
                                            }
                                        }));
                                    }

                                    if (code == "500000")
                                    {
                                        var trace     = PCSiteTraceSource.InvalidRequest_Api(url.ToString(), requestBodyXml, null);
                                        var errorCode = 0;
                                        trace.TryGetId(out errorCode);
                                        return(requestMessage.CreateResponse(HttpStatusCode.InternalServerError,
                                                                             new ErrorResponse {
                                            Errors = new Dictionary <string, string> {
                                                { "code", errorCode.ToString() }
                                            }
                                        }));
                                    }

                                    if (code == "600000")
                                    {
                                        var trace     = PCSiteTraceSource.ValueMissing_Api(url.ToString(), "処理結果コード");
                                        var errorCode = 0;
                                        trace.TryGetId(out errorCode);
                                        return(requestMessage.CreateResponse(HttpStatusCode.InternalServerError,
                                                                             new ErrorResponse {
                                            Errors = new Dictionary <string, string> {
                                                { "code", errorCode.ToString() }
                                            }
                                        }));
                                    }

                                    if (code == "900000")
                                    {
                                        var trace     = PCSiteTraceSource.SystemError_Api(string.Format("問合せAPI(オンプレ)で想定外のエラーが発生。メッセージ:", result_message), null);
                                        var errorCode = 0;
                                        trace.TryGetId(out errorCode);
                                        return(requestMessage.CreateResponse(HttpStatusCode.InternalServerError,
                                                                             new ErrorResponse {
                                            Errors = new Dictionary <string, string> {
                                                { "code", errorCode.ToString() }
                                            }
                                        }));
                                    }

                                    var errorTrace        = PCSiteTraceSource.UnexpectedResultCode(url.ToString(), code);
                                    var unexpcetErrorCode = 0;
                                    errorTrace.TryGetId(out unexpcetErrorCode);
                                    return(requestMessage.CreateResponse(HttpStatusCode.InternalServerError,
                                                                         new ErrorResponse {
                                        Errors = new Dictionary <string, string> {
                                            { "code", unexpcetErrorCode.ToString() }
                                        }
                                    }));
                                }
                                else
                                {
                                    var trace     = PCSiteTraceSource.UnexpectedHttpStatus_Api(url.ToString(), response.StatusCode, "");
                                    var errorCode = 0;
                                    trace.TryGetId(out errorCode);
                                    return(requestMessage.CreateResponse(HttpStatusCode.InternalServerError,
                                                                         new ErrorResponse {
                                        Errors = new Dictionary <string, string> {
                                            { "code", errorCode.ToString() }
                                        }
                                    }));
                                }
                            }
                    }
            }
            catch (InvalidRequestForContactException ie)
            {
                PCSiteTraceSource.InvalidRequest_Api("tcmypage/api/contact", request.ToString(), ie);
                return(requestMessage.CreateResponse(HttpStatusCode.BadRequest));
            }
            catch (Exception e)
            {
                var trace = PCSiteTraceSource.SystemError_Api("問合せAPIで想定外のエラーが発生", e);
                var code  = 0;
                trace.TryGetId(out code);
                return(requestMessage.CreateResponse(HttpStatusCode.InternalServerError,
                                                     new ErrorResponse {
                    Errors = new Dictionary <string, string> {
                        { "code", code.ToString() }
                    }
                }));
            }
        }