/** * Request http api * @param method string Method of requesting http * @param path string Path of requesting http * @param query array Query of requesting http * @param body array Body of requesting http * @return mixed * @throws VctcException */ public async Task <string> callAPI(Method method, string path, SortedList <string, string> query, string body) { // if (is_array(body)) { // this.fliterParams(body); // } // if (is_array(query)) { // this.fliterParams(query); // } var signatures = this.getSignature(method.ToString(), path, query, body); var req = BuildRequest(path + "?" + HtmlUtils.BuildQueryString(signatures.fullQueries), method, body); var cts = new CancellationTokenSource(); cts.CancelAfter(TimeSpan.FromSeconds(3)); var res = await _client.ExecuteTaskAsync(req, cts.Token); if (res.Content != "") { var ret = JObject.Parse(res.Content); if (ret.TryGetValue("error", out var err)) { var ex = new VctcException((string)ret["msg"], (string)ret["code"]); ex.setRaw(res.Content, (string)ret["code"]); throw ex; } } return(res.Content); }
/** * Calculate the signature of a request. * @param method string Method of requesting http * @param path string Path of requesting http * @param query array Query of requesting http * @param body array Body of requesting http * @return mixed * @throws VctcException */ public (SortedList <string, string> fullQueries, string signature, string timestamp) getSignature(string method, string path, SortedList <string, string> query, string body) { if (string.Empty == path) { throw new Exception("invalid path"); } if (method != "GET" && method != "POST" && method != "DELETE" && method != "PUT") { throw new Exception("invalid method, only GET, POST, DELETE and PUT is supported"); } if (query == null) { query = new SortedList <string, string>(); } string textForSigning = method + " " + path + "\n"; query["_appid"] = this.appId; query["_t"] = (DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0)).ToString(); string queryStr = HtmlUtils.BuildQueryString(query); textForSigning += queryStr; if (body != "") { textForSigning += "\n" + body; } query["_s"] = HashUtils.HMAC_SHA256(textForSigning, this.appSecret); return(fullQueries : query, signature : query["_s"], timestamp : query["_t"]); }