예제 #1
0
        private T DoExecute <T>(IDingTalkRequest <T> request, string session, DateTime timestamp) where T : DingTalkResponse
        {
            long start = DateTime.Now.Ticks;

            // 提前检查业务参数
            try
            {
                request.Validate();
            }
            catch (TopException e)
            {
                return(CreateErrorResponse <T>(e.ErrorCode, e.ErrorMsg));
            }

            // 添加协议级请求参数
            TopDictionary txtParams = new TopDictionary(request.GetParameters());

            txtParams.Add(Constants.METHOD, request.GetApiName());
            txtParams.Add(Constants.VERSION, "2.0");
            txtParams.Add(Constants.FORMAT, format);
            txtParams.Add(Constants.PARTNER_ID, GetSdkVersion());
            txtParams.Add(Constants.TIMESTAMP, timestamp);
            txtParams.Add(Constants.SESSION, session);
            txtParams.AddAll(this.systemParameters);

            if (this.useSimplifyJson)
            {
                txtParams.Add(Constants.SIMPLIFY, "true");
            }
            // 添加头部参数
            if (this.useGzipEncoding)
            {
                request.GetHeaderParameters()[Constants.ACCEPT_ENCODING] = Constants.CONTENT_ENCODING_GZIP;
            }

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

            try
            {
                string body;

                body = webUtils.DoPost(realServerUrl, txtParams, request.GetHeaderParameters());

                // 解释响应结果
                T rsp;
                if (disableParser)
                {
                    rsp      = Activator.CreateInstance <T>();
                    rsp.Body = body;
                }
                else
                {
                    if (Constants.FORMAT_XML.Equals(format))
                    {
                        ITopParser <T> tp = new TopXmlParser <T>();
                        rsp = tp.Parse(body);
                    }
                    else
                    {
                        ITopParser <T> tp;
                        if (useSimplifyJson)
                        {
                            tp = new TopSimplifyJsonParser <T>();
                        }
                        else
                        {
                            tp = new TopJsonParser <T>();
                        }
                        rsp = tp.Parse(body);
                    }
                }

                // 追踪错误的请求
                if (rsp.IsError)
                {
                    TimeSpan latency = new TimeSpan(DateTime.Now.Ticks - start);
                    TraceApiError(request.GetApiName(), serverUrl, txtParams, latency.TotalMilliseconds, rsp.Body);
                }
                return(rsp);
            }
            catch (Exception e)
            {
                TimeSpan latency = new TimeSpan(DateTime.Now.Ticks - start);
                TraceApiError(request.GetApiName(), serverUrl, txtParams, latency.TotalMilliseconds, e.GetType() + ": " + e.Message);
                throw e;
            }
        }
예제 #2
0
        private T DoExecuteOApi <T>(IDingTalkRequest <T> request, string session, string accessKey, string accessSecret, string suiteTicket, string corpId, DateTime timestamp) where T : DingTalkResponse
        {
            long start = DateTime.Now.Ticks;

            // 提前检查业务参数
            try
            {
                request.Validate();
            }
            catch (TopException e)
            {
                return(CreateErrorResponse <T>(e.ErrorCode, e.ErrorMsg));
            }

            this.format = Constants.FORMAT_JSON;

            // 添加协议级请求参数
            TopDictionary txtParams = new TopDictionary(request.GetParameters());

            txtParams.Add(DingTalkConstants.ACCESS_TOKEN, session);

            // 添加头部参数
            if (this.useGzipEncoding)
            {
                request.GetHeaderParameters()[Constants.ACCEPT_ENCODING] = Constants.CONTENT_ENCODING_GZIP;
            }

            string realServerUrl = null;

            // 签名优先
            if (accessKey != null)
            {
                long dingTimestamp = GetTimestamp(DateTime.UtcNow);
                // 验证签名有效性
                String canonicalString          = DingTalkSignatureUtil.GetCanonicalStringForIsv(dingTimestamp, suiteTicket);
                String signature                = DingTalkSignatureUtil.ComputeSignature(accessSecret, canonicalString);
                IDictionary <String, String> ps = new Dictionary <String, String>();
                ps.Add("accessKey", accessKey);
                ps.Add("signature", signature);
                ps.Add("timestamp", dingTimestamp + "");
                if (suiteTicket != null)
                {
                    ps.Add("suiteTicket", suiteTicket);
                }
                if (corpId != null)
                {
                    ps.Add("corpId", corpId);
                }

                String queryStr = DingTalkSignatureUtil.ParamToQueryString(ps, "utf-8");
                if (this.serverUrl.IndexOf("?") > 0)
                {
                    realServerUrl = this.serverUrl + "&" + queryStr;
                }
                else
                {
                    realServerUrl = this.serverUrl + "?" + queryStr;
                }
            }
            else
            {
                if (this.serverUrl.IndexOf("?") > 0)
                {
                    realServerUrl = this.serverUrl + (session != null && session != "" ? ("&access_token=" + session) : "");
                }
                else
                {
                    realServerUrl = this.serverUrl + (session != null && session != "" ? ("?access_token=" + session) : "");
                }
            }

            try
            {
                string body;

                if (request.GetHttpMethod() == "POST")
                {
                    if (request is IDingTalkUploadRequest <T> ) // 是否需要上传文件
                    {
                        IDingTalkUploadRequest <T>     uRequest   = (IDingTalkUploadRequest <T>)request;
                        IDictionary <string, FileItem> fileParams = TopUtils.CleanupDictionary(uRequest.GetFileParameters());
                        body = webUtils.DoPost(realServerUrl, null, fileParams, request.GetHeaderParameters());
                    }
                    else
                    {
                        IDictionary <String, Object> jsonParams = new Dictionary <String, Object>();
                        foreach (string key in request.GetParameters().Keys)
                        {
                            string value = request.GetParameters()[key];
                            if (value.StartsWith("[") && value.EndsWith("]"))
                            {
                                IList childMap = (IList)TopUtils.JsonToObject(value);
                                jsonParams.Add(key, childMap);
                            }
                            else if (value.StartsWith("{") && value.EndsWith("}"))
                            {
                                IDictionary <string, Object> childMap = (IDictionary <string, Object>)TopUtils.JsonToObject(value);
                                jsonParams.Add(key, childMap);
                            }
                            else
                            {
                                jsonParams.Add(key, value);
                            }
                        }


                        body = webUtils.DoPostWithJson(realServerUrl, jsonParams, request.GetHeaderParameters());
                    }
                }
                else
                {
                    body = webUtils.DoGet(realServerUrl, request.GetParameters());
                }

                // 解释响应结果
                T rsp;
                if (disableParser)
                {
                    rsp      = Activator.CreateInstance <T>();
                    rsp.Body = body;
                }
                else
                {
                    ITopParser <T> tp = new DingTalkJsonParser <T>();
                    rsp = tp.Parse(body);
                }

                // 追踪错误的请求
                if (rsp.IsError)
                {
                    TimeSpan latency = new TimeSpan(DateTime.Now.Ticks - start);
                    TraceApiError(request.GetApiName(), serverUrl, txtParams, latency.TotalMilliseconds, rsp.Body);
                }
                return(rsp);
            }
            catch (Exception e)
            {
                TimeSpan latency = new TimeSpan(DateTime.Now.Ticks - start);
                TraceApiError(request.GetApiName(), serverUrl, txtParams, latency.TotalMilliseconds, e.GetType() + ": " + e.Message);
                throw e;
            }
        }