コード例 #1
0
        private LazopResponse ParseResponse(string jsonRsp)
        {
            IDictionary <string, object> root = FastJSON.JSON.Parse(jsonRsp) as IDictionary <string, object>;
            LazopResponse lazopRsp            = new LazopResponse();

            lazopRsp.Type      = GetStringValue(root, Constants.RSP_TYPE);
            lazopRsp.Code      = GetStringValue(root, Constants.RSP_CODE);
            lazopRsp.Message   = GetStringValue(root, Constants.RSP_MSG);
            lazopRsp.RequestId = GetStringValue(root, Constants.RSP_REQUEST_ID);
            lazopRsp.Body      = jsonRsp;
            return(lazopRsp);
        }
コード例 #2
0
        private LazopResponse DoExecute(LazopRequest request, string accessToken, DateTime timestamp)
        {
            long start = DateTime.Now.Ticks;

            // add common params
            LazopDictionary txtParams = new LazopDictionary(request.GetParameters());

            txtParams.Add(Constants.APP_KEY, appKey);
            txtParams.Add(Constants.TIMESTAMP, GetTimestamp(timestamp));
            txtParams.Add(Constants.ACCESS_TOKEN, accessToken);
            txtParams.Add(Constants.PARTNER_ID, sdkVersion);
            txtParams.AddAll(this.customrParameters);
            txtParams.Add(Constants.SIGN_METHOD, this.signMethod);
            if (IsDebugEnabled())
            {
                txtParams.Add(Constants.DEBUG, true);
            }

            // compute and add sign
            txtParams.Add(Constants.SIGN, LazopUtils.SignRequest(request.GetApiName(), txtParams, appSecret, this.signMethod));

            string realServerUrl = GetServerUrl(this.serverUrl, request.GetApiName(), accessToken);
            string reqUrl        = WebUtils.BuildRequestUrl(realServerUrl, txtParams);

            try
            {
                string body;
                if (request.GetFileParameters() != null) // if file params is set
                {
                    body = webUtils.DoPost(realServerUrl, txtParams, request.GetFileParameters(), request.GetHeaderParameters());
                }
                else
                {
                    if (request.GetHttpMethod().Equals(Constants.METHOD_POST))
                    {
                        body = webUtils.DoPost(realServerUrl, txtParams, request.GetHeaderParameters());
                    }
                    else
                    {
                        body = webUtils.DoGet(realServerUrl, txtParams, request.GetHeaderParameters());
                    }
                }

                LazopResponse response = ParseResponse(body);

                // log error response
                if (response.IsError())
                {
                    TimeSpan latency = new TimeSpan(DateTime.Now.Ticks - start);
                    LogApiError(appKey, sdkVersion, request.GetApiName(), serverUrl, txtParams, latency.TotalMilliseconds, response.Body);
                }
                else
                {
                    if (IsDebugEnabled() || IsInfoEnabled())
                    {
                        TimeSpan latency = new TimeSpan(DateTime.Now.Ticks - start);
                        LogApiError(appKey, sdkVersion, request.GetApiName(), serverUrl, txtParams, latency.TotalMilliseconds, response.Body);
                    }
                }

                return(response);
            }
            catch (Exception e)
            {
                TimeSpan latency = new TimeSpan(DateTime.Now.Ticks - start);
                LogApiError(appKey, sdkVersion, request.GetApiName(), serverUrl, txtParams, latency.TotalMilliseconds, e.GetType() + ": " + e.Message);
                throw e;
            }
        }