Ramco Ramco

Ramco Ramco

  • 441
  • 3.4k
  • 522.8k

Code to be written once

Jan 9 2023 7:11 AM

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

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<Root>(response0.Content);
        foreach (var item in root.data)
        {
            ddlCourier.Items.Add(new ListItem(item.name, item.id));
        }
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message + "</br>CreateToken");
    }
}

 


Answers (9)