Hi
I have below code & i don't want to write in every Program. It should be written once . How it can be done
Parameters should be passed to the calling program and respose returned by Api should be displayed
C#
private void GetCourier(string token)
{
try
{
var client = new RestClient("htts://api.nimbuspost.com/v1/courier");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", "Bearer " + token);
request.AddParameter("origin", txtWeight.Text, ParameterType.RequestBody);
request.AddParameter("destination", txtLength.Text, ParameterType.RequestBody);
request.AddHeader("content-type", "application/json");
IRestResponse response0 = client.Execute(request);
Root root = JsonConvert.DeserializeObject(response0.Content);
foreach (var item in root.data)
{
ddlCourier.Items.Add(new ListItem(item.name, item.id));
}
}
catch (Exception ex)
{
Response.Write(ex.Message + "CreateToken");
}
}
Sachin SinghRank #66 · 26.2K reputationPosted Jan 9, 2023, 2:22 AM EST
make the method public and return JsonResult instead
public JsonResult GetCourier(string token) { return Json(new {status=status, response=JsonConvert.Deserialize(yourObject)}); }Ramco RamcoRank #462 · 5.4K reputationPosted Jan 9, 2023, 2:36 AM EST
Hi
Sorry Sachin. I want for this below part . Parameters (Origin & Destination) should be passed & then Json result should be returned
try { var client = new RestClient("htts://api.nimbuspost.com/v1/courier"); client.Timeout = -1; var request = new RestRequest(Method.GET); request.AddHeader("Content-Type", "application/json"); request.AddHeader("Authorization", "Bearer " + token); request.AddParameter("origin", txtWeight.Text, ParameterType.RequestBody); request.AddParameter("destination", txtLength.Text, ParameterType.RequestBody); request.AddHeader("content-type", "application/json"); IRestResponse response0 = client.Execute(request); Root root = JsonConvert.DeserializeObject(response0.Content); foreach (var item in root.data) { ddlCourier.Items.Add(new ListItem(item.name, item.id)); } }Thanks
Amit MohantyRank #112 · 20K reputationPosted Jan 9, 2023, 2:40 AM EST
try this
private static List GetCourier(string token, string origin, string destination) { try { var client = new RestClient("htts://api.nimbuspost.com/v1/courier"); client.Timeout = -1; var request = new RestRequest(Method.GET); request.AddHeader("Content-Type", "application/json"); request.AddHeader("Authorization", "Bearer " + token); request.AddParameter("origin", origin, ParameterType.RequestBody); request.AddParameter("destination", destination, ParameterType.RequestBody); request.AddHeader("content-type", "application/json"); IRestResponse response0 = client.Execute(request); if (response0.IsSuccessful) { var dropdownLists = new List(); Root root = JsonConvert.DeserializeObject(response0.Content); foreach (var item in root.data) { var dropdownList = new DropdownList() { id = item.id, name = item.name }; dropdownLists.Add(dropdownList); } return dropdownLists; } else { Console.WriteLine(string.Format("Failed. Code : {0}", response0.StatusCode)); return new List(); } } catch (Exception ex) { Console.Write(ex.Message + "CreateToken"); return new List(); } }Dropdownlist class
public class DropdownList { public string id { get; set; } public string name { get; set; } }uses
var result = GetCourier("your token", txtWeight.Text, txtLength.Text); ddlCourier.DataSource = result; ddlCourier.DataBind();Sachin SinghRank #66 · 26.2K reputationPosted Jan 9, 2023, 3:07 AM EST
You shouldn't return/add ddlcourier from a common method, rather do this
public JsonResult GetCourier(string token) { var client = new RestClient("htts://api.nimbuspost.com/v1/courier"); client.Timeout = -1; var request = new RestRequest(Method.GET); request.AddHeader("Content-Type", "application/json"); request.AddHeader("Authorization", "Bearer " + token); request.AddParameter("origin", txtWeight.Text, ParameterType.RequestBody); request.AddParameter("destination", txtLength.Text, ParameterType.RequestBody); request.AddHeader("content-type", "application/json"); IRestResponse response0 = client.Execute(request); Root root = JsonConvert.DeserializeObject(response0.Content); return Json(new {status=root.status, response=root}); }Pasang TamangRank #449 · 5.6K reputationPosted Jan 9, 2023, 3:41 AM EST
Hi,
You can create a function to re-use same code. Please check below code example.
public RestResponse GetResponse(string token, string endPointUrl, Dictionary parameters) { var client = new RestClient(endPointUrl); var request = new RestRequest { Method = Method.Get, Timeout = -1 }; request.AddHeader("Content-Type", "application/json"); request.AddHeader("Authorization", "Bearer " + token); foreach (var parameter in parameters) { request.AddParameter(parameter.Key, parameter.Value, ParameterType.RequestBody); } request.AddHeader("content-type", "application/json"); return client.Execute(request); }And this function you can call like in below code
var parameters = new Dictionary(); parameters.Add("origin", txtWeight.Text); parameters.Add("destination", txtLength.Text); Root root = JsonConvert.DeserializeObject(GetResponse("your-token","your-endpoint-url",parameters).Content); foreach (var item in root.data) { ddlCourier.Items.Add(new ListItem(item.name, item.id)); }Regards,
Pasang
Aravind GovindarajRank #430 · 5.8K reputationPosted Jan 9, 2023, 4:02 AM EST
Typically you need to follow a few things here, such as
ExternalInterface Library => ( This is the gateway to connect the external system)
Under the API Folder, you have to create Response and Request. The request is the one which will till this part
var client = new RestClient(input parameter); client.Timeout = -1; var request = new RestRequest(Method.GET); request.AddHeader("Content-Type", "application/json"); // input parameter may be json or xml request.AddHeader("Authorization", "Bearer " + token); // input parameter request.AddParameter("origin", txtWeight.Text, ParameterType.RequestBody); // input param request.AddParameter("destination", txtLength.Text, ParameterType.RequestBody); // inmput param request.AddHeader("content-type", "application/json");once done Get the Response
public GetResponse(request){ IRestResponse response0 = client.Execute(request); Format Required format response }one more function to format as per the required format response
Finally call this library and method from source , you can reuse this anywhere even for calling other api / wcf/ web service
Ramco RamcoRank #462 · 5.4K reputationPosted Jan 9, 2023, 4:46 AM EST
Hi Amit
What is Root. How it is to be used.
Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted Jan 9, 2023, 4:58 AM EST
Hi Sachin
ON below line i am getting error - Using the generic type JsonResult requires 1 type arguments
public JsonResult GetCourier(string token, Int32 origin, Int32 destination,string PaymentType,Int32 OrderAmount)
Thanks
Amit MohantyRank #112 · 20K reputationPosted Jan 9, 2023, 5:00 AM EST
Hey Ramco,
you have already mentioned Root class in your question. Use the same Root class.