public async Task <T> Get <T>(string url, Dictionary <string, object> formdata = null) { try { var client = new RestSharp.RestClient(url); var request = new RestSharp.RestRequest(RestSharp.Method.GET); foreach (var item in formdata) { request.AddParameter(item.Key, item.Value); } System.Diagnostics.Debug.WriteLine(url); var response = await client.ExecuteTaskAsync(request); if (response != null) { if (response.StatusCode == System.Net.HttpStatusCode.OK) { return(Newtonsoft.Json.JsonConvert.DeserializeObject <T>(response.Content)); } } } catch { } return(default(T)); }
internal async void DisplayAssetInfo(string asset) { const string url = "https://s2.coinmarketcap.com/static/img/coins/32x32/"; if (cmc_listing == null) { cmc_listing = await CoinMarketCapApiClient.GetListingsAsync(); } var listing = cmc_listing.FirstOrDefault(x => asset.Equals(x.symbol, StringComparison.CurrentCultureIgnoreCase)); if (listing != null) { label5.Text = listing.name; var client = new RestSharp.RestClient(url); var req = new RestSharp.RestRequest($"{listing.id}.png", RestSharp.Method.GET); var resp = await client.ExecuteTaskAsync(req); image1.Pixbuf = new Gdk.Pixbuf(resp.RawBytes); } else { label5.Text = asset; image1.Pixbuf = null; } }
public async void SendResponseAsync(int month) { var restClient = new RestSharp.RestClient("http://ringo-tabetter-api.herokuapp.com/"); var request = new RestSharp.RestRequest("api/v1/month", RestSharp.Method.GET); var response = await restClient.ExecuteTaskAsync(request); RestSharp.Deserializers.JsonDeserializer deserial = new RestSharp.Deserializers.JsonDeserializer(); var results = deserial.Deserialize <List <RingoByMonths> >(response); var topRingo = results.OrderByDescending(r => r.quantities[month]).First(); this.RunOnUiThread(() => { var status = FindViewById <TextView>(Resource.Id.HandheldStatus); status.Text = topRingo.name; } ); var msg = (month + 1).ToString() + ":" + topRingo.name + ":" + topRingo.color; var nodeIds = await Task.Run(() => NodeIds); foreach (var nodeId in nodeIds) { WearableClass.MessageApi.SendMessage(client, nodeId, MessageTag, System.Text.Encoding.UTF8.GetBytes(msg)); } }
public async Task <T> Get <T>(string url, Dictionary <string, object> formdata = null) { try { Debugger.WriteLine("RestSharp: " + url); var client = new RestSharp.RestClient(url); var request = new RestSharp.RestRequest(RestSharp.Method.GET); foreach (var item in formdata) { request.AddParameter(item.Key, item.Value); } var response = await client.ExecuteTaskAsync(request); if (response != null) { if (response.StatusCode == System.Net.HttpStatusCode.OK) { Debugger.WriteLine("RestSharp: " + response.Content); return(response.Content.DeserializeObject <T>()); } } } catch (Exception ex) { Debugger.WriteLine("RestSharp: " + ex.StackTrace); } return(default(T)); }
public async Task <T> Post <T, K>(string url, K objecttosend) { try { Debugger.WriteLine("RestSharp: " + url); var client = new RestSharp.RestClient(url); var request = new RestSharp.RestRequest(RestSharp.Method.POST); var jsonstring = objecttosend.SerializeObject(); request.AddJsonBody(jsonstring); request.RequestFormat = RestSharp.DataFormat.Json; var response = await client.ExecuteTaskAsync(request); if (response != null) { if (response.StatusCode == System.Net.HttpStatusCode.OK) { Debugger.WriteLine("RestSharp: " + response.Content); return(response.Content.DeserializeObject <T>()); } } } catch (Exception ex) { Debugger.WriteLine("RestSharp: " + ex.StackTrace); } return(default(T)); }
/// <summary> /// QRCode 생성을 위한 URL 요청 /// </summary> /// <param name="aRfid"></param> /// <param name="aPurchaseHistoryOnetimeURL"></param> /// <returns></returns> public static DTOPurchaseHistoryOnetimeURLResponse API_PostPurchaseHistoryOnetimeURL(string aRfid, long aBeforeTimestamp, long aAfterTimestamp) { //---------------------------------------- DTOPurchaseHistoryOnetimeURLRequest req = new DTOPurchaseHistoryOnetimeURLRequest(); { req.rfid = aRfid; req.purchase_before = aBeforeTimestamp; req.purchase_after = aAfterTimestamp; } //---------------------------------------- RestSharp.RestClient client = new RestSharp.RestClient(URL_DCCAFFE); RestSharp.RestRequest request = new RestSharp.RestRequest(); request.AddHeader("Content-Type", "application/json;charset=UTF-8"); request.Method = RestSharp.Method.POST; request.RequestFormat = RestSharp.DataFormat.Json; request.Resource = URI_POST_PURCHASE_HISTORY_TEMPORARY_URL; request.AddJsonBody(req); //---------------------------------------- var t1 = client.ExecuteTaskAsync(request); t1.Wait(); //---------------------------------------- // error handling if (t1.Result.ErrorException != null) { System.Diagnostics.Debug.WriteLine("[RESPONSE] " + t1.Result.Content); return(null); } string json = t1.Result.Content; //---------------------------------------- // debug output json = JsonFormatting(json); System.Diagnostics.Debug.WriteLine("[RESPONSE] " + json); //---------------------------------------- // desirialized json data DTOPurchaseHistoryOnetimeURLResponse dto = new DTOPurchaseHistoryOnetimeURLResponse(); try { dto = JsonConvert.DeserializeObject <DTOPurchaseHistoryOnetimeURLResponse>(json); dto.code = (int)t1.Result.StatusCode; } catch (Exception ex) { dto = null; System.Diagnostics.Debug.WriteLine("[ERROR] " + ex.Message); } return(dto); }
/// <summary> /// 구매 취소 /// </summary> /// <param name="aReceiptId"></param> /// <returns></returns> public static DTOPurchaseCancelResponse API_PatchPurchaseCancel(string aRfid, string aReceiptId) { //---------------------------------------- DTOPurchaseCancelRequest req = new DTOPurchaseCancelRequest(); { req.rfid = aRfid; } //---------------------------------------- RestSharp.RestClient client = new RestSharp.RestClient(URL_DCCAFFE); RestSharp.RestRequest request = new RestSharp.RestRequest(); request.AddHeader("Content-Type", "application/json;charset=UTF-8"); request.Method = RestSharp.Method.PATCH; request.RequestFormat = RestSharp.DataFormat.Json; request.Resource = URI_PATCH_PURCHASE_CANCEL; request.AddParameter("receipt_id", aReceiptId, RestSharp.ParameterType.UrlSegment); request.AddJsonBody(req); //---------------------------------------- var t1 = client.ExecuteTaskAsync(request); t1.Wait(); //---------------------------------------- // error handling if (t1.Result.ErrorException != null) { System.Diagnostics.Debug.WriteLine("[RESPONSE] " + t1.Result.Content); return(null); } string json = t1.Result.Content; //---------------------------------------- // debug output json = JsonFormatting(json); System.Diagnostics.Debug.WriteLine("[RESPONSE] " + json); //---------------------------------------- // desirialized json data DTOPurchaseCancelResponse dto = new DTOPurchaseCancelResponse(); try { dto = JsonConvert.DeserializeObject <DTOPurchaseCancelResponse>(json); dto.code = (int)t1.Result.StatusCode; } catch (Exception ex) { dto = null; System.Diagnostics.Debug.WriteLine("[ERROR] " + ex.Message); } return(dto); }
/// <summary> /// 메뉴 목록 가져오기 /// </summary> /// <returns></returns> public static DTOGetMenusResponse API_GetMenus() { //============================================ // GET http://10.1.203.12:8080/api/caffe/menus //============================================ RestSharp.RestClient client = new RestSharp.RestClient(URL_DCCAFFE); RestSharp.RestRequest request = new RestSharp.RestRequest(); request.AddHeader("Content-Type", "application/json"); request.Method = RestSharp.Method.GET; request.RequestFormat = RestSharp.DataFormat.Json; request.Resource = URI_GET_MENUS; request.Timeout = 3000; //---------------------------------------- var t1 = client.ExecuteTaskAsync(request); t1.Wait(); //---------------- // error handling if (t1.Result.ErrorException != null) { return(null); } string json = t1.Result.Content; //-------------- // debug output json = JsonFormatting(json); System.Diagnostics.Debug.WriteLine("[RESPONSE] " + json); //----------------------- // desirialized json data DTOGetMenusResponse dto = new DTOGetMenusResponse(); try { if (t1.Result.StatusCode == System.Net.HttpStatusCode.OK) { dto.dicCategoryMenus = JsonConvert.DeserializeObject <Dictionary <string, List <VOCategoryMenuList> > >(json); dto.code = (int)t1.Result.StatusCode; } else { dto = JsonConvert.DeserializeObject <DTOGetMenusResponse>(json); } } catch (Exception ex) { dto = null; System.Diagnostics.Debug.WriteLine("[ERROR] " + ex.Message); } return(dto); }
private static async Task SendExpense(decimal amount, string reason) { try { var client = new RestSharp.RestClient(); var content = new RestSharp.RestRequest(new Uri(SERVICE_URL), RestSharp.Method.POST) { RequestFormat = RestSharp.DataFormat.Json }; content.AddJsonBody(new { Amount = amount, Reason = reason, LicenseKey = PASample.Wpf.App.LicenseKey, Id = Guid.NewGuid(), Department = PASample.Wpf.App.Department }); content.Resource = "api/Expense/Approve"; client.BaseUrl = new Uri(SERVICE_URL); var uri = client.BuildUri(content); var response = await client.ExecuteTaskAsync(content); if (response.ResponseStatus != RestSharp.ResponseStatus.Completed) { MessageBox.Show("Unknown Error Processing Request", "Error"); return; } var respStream = response.Content; var js = new Newtonsoft.Json.JsonSerializer(); var expResponse = (ExpenseApprovalResponse)js.Deserialize(new Newtonsoft.Json.JsonTextReader(new StringReader(respStream)), typeof(ExpenseApprovalResponse)); var msg = string.Empty; if (expResponse.Exception == null || string.IsNullOrEmpty(expResponse.Exception.Message)) { msg = "Your request was processed."; } else { msg = "Your request was rejected because - " + expResponse.Exception.Message; } MessageBox.Show(msg, "Result"); } catch (Exception ex) { MessageBox.Show("Error", "Error"); System.Diagnostics.Debug.WriteLine(ex.ToString()); } }
public async Task <T> Post <T>(string url, Dictionary <string, object> simpleparams = null, List <Param> complexparams = null) { try { Debugger.WriteLine("RestSharp: " + url); var client = new RestSharp.RestClient(url); var request = new RestSharp.RestRequest(RestSharp.Method.POST); if (simpleparams != null) { foreach (var item in simpleparams) { request.AddParameter(item.Key, item.Value); } } if (complexparams != null) { foreach (var item in complexparams) { switch (item.Type) { case ParamType.File: request.AddFileBytes(item.Name, item.Bytes, item.FileName, item.ContentType); break; case ParamType.String: request.AddParameter(item.Name, item.Element); break; } } } var response = await client.ExecuteTaskAsync(request); if (response != null) { if (response.StatusCode == System.Net.HttpStatusCode.OK) { Debugger.WriteLine("RestSharp: " + response.Content); return(response.Content.DeserializeObject <T>()); } } } catch (Exception ex) { Debugger.WriteLine("RestSharp: " + ex.StackTrace); } return(default(T)); }
public async Task <T> Post <T, K>(string url, K objecttosend) { try { var client = new RestSharp.RestClient(url); var request = new RestSharp.RestRequest(RestSharp.Method.POST); request.AddJsonBody(objecttosend); System.Diagnostics.Debug.WriteLine(url); var response = await client.ExecuteTaskAsync(request); if (response != null) { if (response.StatusCode == System.Net.HttpStatusCode.OK) { return(Newtonsoft.Json.JsonConvert.DeserializeObject <T>(response.Content)); } } } catch { } return(default(T)); }
public async Task <T> Delete <T>(string url, int id) { try { Debugger.WriteLine("RestSharp: " + url); var client = new RestSharp.RestClient(url); var request = new RestSharp.RestRequest(RestSharp.Method.DELETE); var response = await client.ExecuteTaskAsync(request); if (response != null) { if (response.StatusCode == System.Net.HttpStatusCode.OK) { Debugger.WriteLine("RestSharp: " + response.Content); return(response.Content.DeserializeObject <T>()); } } } catch (Exception ex) { Debugger.WriteLine("RestSharp: " + ex.StackTrace); } return(default(T)); }
/// <summary> /// 구매 확정을 위한 주문 내역 전송 /// </summary> /// <param name="aReceiptId"></param> /// <param name="aPurchaseList"></param> /// <returns></returns> public static DTOPurchasesResponse API_PostPurchaseSuccess(string aReceiptId, DTOPurchasesRequest aPurchasesRequest) { //================================================================================ // POST http://10.1.203.12:8080/api/caffe/purchases/purchase/receipt/{receipt_id} //================================================================================ //VOMenu menu = new VOMenu //{ // category = 100, // code = 1, // price = 1000, // type = "HOT", // size = "REGULAR", // count = 5 //}; //DTOPurchasesRequest obj = new DTOPurchasesRequest //{ // purchases = new List<VOMenu> // { // new VOMenu // { // category = 100, // code = 1, // price = 1000, // type = "HOT", // size = "REGULAR", // count = 5 // }, // new VOMenu // { // category = 200, // code = 8, // price = 1500, // type = "HOT", // size = "REGULAR", // count = 1 // }, // } //}; // //string json = JsonConvert.SerializeObject(aPurchasesRequest); RestSharp.RestClient client = new RestSharp.RestClient(URL_DCCAFFE); RestSharp.RestRequest request = new RestSharp.RestRequest(); request.AddHeader("Content-Type", "application/json;charset=UTF-8"); request.Method = RestSharp.Method.POST; request.RequestFormat = RestSharp.DataFormat.Json; request.Resource = URI_POST_PURCHASE; request.AddParameter("receipt_id", aReceiptId, RestSharp.ParameterType.UrlSegment); request.AddJsonBody(aPurchasesRequest); //---------------------------------------- var t1 = client.ExecuteTaskAsync(request); t1.Wait(); //---------------- // error handling if (t1.Result.ErrorException != null) { System.Diagnostics.Debug.WriteLine("[RESPONSE] " + t1.Result.Content); return(null); } string json = t1.Result.Content; //-------------- // debug output json = JsonFormatting(json); System.Diagnostics.Debug.WriteLine("[RESPONSE] " + json); //----------------------- // desirialized json data DTOPurchasesResponse dto = new DTOPurchasesResponse(); try { dto = JsonConvert.DeserializeObject <DTOPurchasesResponse>(json); dto.code = (int)t1.Result.StatusCode; } catch (Exception ex) { dto = null; System.Diagnostics.Debug.WriteLine("[ERROR] " + ex.Message); } return(dto); }
/// <summary> /// 영수증 번호 요청 /// sha256(rfid)를 인증하고, 구매 영수증 번호를 응답 받는다. /// </summary> /// <param name="aRfid"></param> /// <returns></returns> public static DTOGetPurchaseIdResponse API_PostPurchaseId(string aRfid) { //===================================================================== // POST http://10.1.203.12:8080/api/caffe/purchases/purchase/receipt/id //===================================================================== RestSharp.RestClient client = new RestSharp.RestClient(URL_DCCAFFE); RestSharp.RestRequest request = new RestSharp.RestRequest(); request.AddHeader("Content-Type", "application/json;charset=UTF-8"); request.Method = RestSharp.Method.POST; request.RequestFormat = RestSharp.DataFormat.Json; request.Resource = URI_GET_PURCHASE_ID; //------------------------------------------------ // make to request json /* * StringBuilder sb = new StringBuilder(); * StringWriter sw = new StringWriter(sb); * using (JsonWriter writer = new JsonTextWriter(sw)) * { * writer.Formatting = Formatting.None; * * writer.WriteStartObject(); * writer.WritePropertyName("rfid"); * writer.WriteValue(aRfid); * writer.WriteEndObject(); * } * * //------------------------------------------------ * request.AddParameter("application/json", sb.ToString(), RestSharp.ParameterType.RequestBody); */ DTOGetPurchaseIdRequest reqJson = new DTOGetPurchaseIdRequest { rfid = aRfid }; request.AddJsonBody(reqJson); //---------------------------------------- var t1 = client.ExecuteTaskAsync(request); t1.Wait(); //---------------- // error handling if (t1.Result.ErrorException != null) { System.Diagnostics.Debug.WriteLine("[RESPONSE] " + t1.Result.Content); return(null); } string json = t1.Result.Content; //-------------- // debug output json = JsonFormatting(json); System.Diagnostics.Debug.WriteLine("[RESPONSE] " + json); //----------------------- // desirialized json data DTOGetPurchaseIdResponse dto = new DTOGetPurchaseIdResponse(); try { dto = JsonConvert.DeserializeObject <DTOGetPurchaseIdResponse>(json); dto.code = (int)t1.Result.StatusCode; } catch (Exception ex) { dto = null; System.Diagnostics.Debug.WriteLine("[ERROR] " + ex.Message); } return(dto); }