public List <LookupDropDownsProvince> restDropDownProvince(IUimDataContext datacontext, string lookupIDquery) { List <LookupDropDownsProvince> items = new List <LookupDropDownsProvince>(); string URL = selectCustomValue(datacontext.CustomValue, "RestURL="); //System.Windows.MessageBox.Show("URL REST = " + URL); string urlParameter = string.Empty; //Request Here try { LookupDataID lookup = new LookupDataID() { lookupID = lookupIDquery }; string json = JsonConvert.SerializeObject(lookup); // System.Windows.MessageBox.Show(json); HttpClient client = new HttpClient(); client.BaseAddress = new Uri(URL); // Add an Accept header for JSON format. client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // List data response. HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, URL); request.Content = new StringContent(json); request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); HttpResponseMessage response = client.PostAsync(string.Empty, request.Content).Result; if (response.IsSuccessStatusCode) { // Parse the response body. IEnumerable <LookupDropDownsProvince> lookupBankName = response.Content.ReadAsAsync <IEnumerable <LookupDropDownsProvince> >().Result; //Make sure to add a reference to System.Net.Http.Formatting.dll foreach (var d in lookupBankName) { // Edit Value or Text here items.Add(new LookupDropDownsProvince { provinceName = d.provinceName, provinceCode = d.provinceCode }); } } else { System.Windows.MessageBox.Show("API Dropdownlist :" + lookupIDquery + " Fail !"); } client.Dispose(); return(items); } catch (Exception e) { System.Windows.MessageBox.Show(e.ToString()); } return(items); }
//--------------------------------------------------------------Call REST API For DATA. -------------------------------------// public void restFundName(IUimDataContext datacontext) { DataTable table = new DataTable(); List <DataObjectFundName> list = new List <DataObjectFundName>(); DataSet dts = new DataSet(); string URL = selectCustomValue(datacontext.CustomValue, "RestURL="); //string URL = "https://api.fwd.co.th/dev-mock/dataentryFacadeService/fetchFundMasterData"; // string urlParameters = "?api_key=123"; try { LookupDataID lookup = new LookupDataID() { lookupID = "FUNDMASTER" }; string json = JsonConvert.SerializeObject(lookup); HttpClient client = new HttpClient(); client.BaseAddress = new Uri(URL); // Add an Accept header for JSON format. client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // List data response. //HttpResponseMessage response = client.GetAsync(string.Empty).Result; //GET REST API HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, URL); request.Content = new StringContent(json); request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); HttpResponseMessage response = client.PostAsync(string.Empty, request.Content).Result; //System.Windows.MessageBox.Show("REST API"); if (response.IsSuccessStatusCode) { //System.Windows.MessageBox.Show(response.IsSuccessStatusCode.ToString()); // Parse the response body. IEnumerable <DataObjectFundName> dataObjects = response.Content.ReadAsAsync <IEnumerable <DataObjectFundName> >().Result; //Make sure to add a reference to System.Net.Http.Formatting.dll // System.Windows.MessageBox.Show("START LOOP"); foreach (var d in dataObjects) { //System.Windows.MessageBox.Show("LOOP 1 "); string sdt = d.ShortDesc + "-" + d.DescriptionTh; list.Add(new DataObjectFundName { ShortDesc = d.ShortDesc, DescriptionTh = d.DescriptionTh + "[" + d.ShortDesc + "]" }); } // table = ToDataTable(list); dt = ToDataTable(list); } else { System.Windows.MessageBox.Show("API FAIL"); } //MessageBox.Show("START-5"); client.Dispose(); //return dt; } catch (Exception e) { System.Windows.MessageBox.Show("Rest API Fund Name Fail !" + e.ToString()); } //return table; }