//protected async Task<string> InternalRequest(AbstractModel request, string actionName) //{ // if ((this.Profile.HttpProfile.ReqMethod != HttpProfile.REQ_GET) && (this.Profile.HttpProfile.ReqMethod != HttpProfile.REQ_POST)) // { // throw new Exception("Method only support (GET, POST)"); // } // IResponse response = null; // response = await RequestV1(request, actionName); // if ((int)response.Status != HTTP_RSP_OK) // { // throw new Exception(response.Status + await response.Message.Content.ReadAsStringAsync()); // } // string strResp = null; // try // { // strResp = await response.AsString(); // } // catch (ApiException ex) // { // string responseText = await ex.Response.AsString(); // throw new Exception($"The API responded with HTTP {ex.Response.Status}: {responseText}"); // } // return strResp; //} protected string InternalRequestSync(AbstractModel request, string actionName) { if ((this.Profile.HttpProfile.ReqMethod != HttpProfile.REQ_GET) && (this.Profile.HttpProfile.ReqMethod != HttpProfile.REQ_POST)) { throw new Exception("Method only support (GET, POST)"); } HttpWebResponse response = null; response = RequestV1Sync(request, actionName); HttpStatusCode statusCode = response.StatusCode; if (statusCode != HttpStatusCode.OK) { Encoding encoding = Encoding.UTF8; using (Stream stream = response.GetResponseStream()) { using (StreamReader sr = new StreamReader(response.GetResponseStream(), encoding)) { string content = sr.ReadToEnd().ToString(); throw new Exception(statusCode.ToString() + content); } } } string strResp = null; try { Encoding encoding = Encoding.UTF8; using (Stream stream = response.GetResponseStream()) { using (StreamReader sr = new StreamReader(response.GetResponseStream(), encoding)) { strResp = sr.ReadToEnd().ToString(); } } } catch (Exception ex) { string responseText = ex.Message; throw new Exception($"The API responded with HTTP {responseText}"); } //try //{ // errResp = JsonConvert.DeserializeObject<JsonResponseModel<JsonResponseErrModel>>(strResp); //} //catch (JsonSerializationException e) //{ // throw new Exception(e.Message); //} //if (errResp.Response.Error != null) //{ // throw new Exception($"code:{errResp.Response.Error.Code} message:{errResp.Response.Error.Message} ", // errResp.Response.RequestId); //} return(strResp); }
private Dictionary <string, string> BuildParam(AbstractModel request, string actionName) { Dictionary <string, string> param = new Dictionary <string, string>(); string data = AbstractModel.ToJsonString(request); Console.WriteLine(data); // inplace change param = this.FormatRequestData(actionName, data); return(param); }
private string BuildRequestPayload(AbstractModel request) { string httpRequestMethod = this.Profile.HttpProfile.ReqMethod; if (HttpProfile.REQ_GET.Equals(httpRequestMethod)) { return(""); } return(JsonConvert.SerializeObject(request, Newtonsoft.Json.Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore })); }
private string BuildCanonicalQueryString(AbstractModel request) { string httpRequestMethod = this.Profile.HttpProfile.ReqMethod; if (!HttpProfile.REQ_GET.Equals(httpRequestMethod)) { return(""); } Dictionary <string, string> param = new Dictionary <string, string>(); StringBuilder urlBuilder = new StringBuilder(); foreach (KeyValuePair <string, string> kvp in param) { urlBuilder.Append($"{WebUtility.UrlEncode(kvp.Key)}={WebUtility.UrlEncode(kvp.Value)}&"); } return(urlBuilder.ToString().TrimEnd('&')); }
private HttpWebResponse RequestV1Sync(AbstractModel request, string actionName) { HttpWebResponse response = null; Dictionary <string, string> param = BuildParam(request, actionName); HttpConnection conn = this.BuildConnection(); try { if (this.Profile.HttpProfile.ReqMethod == HttpProfile.REQ_GET) { response = conn.GetRequestSync(this.Path, param); } else if (this.Profile.HttpProfile.ReqMethod == HttpProfile.REQ_POST) { response = conn.PostRequestSync(this.Path, param); } } catch (Exception ex) { throw new Exception($"The request with exception: {ex.Message }"); } return(response); }