I am writing a simple REST client for a C # WinForm application. I use RestSharp to make sending requests and receiving responses easier. I have a few questions regarding how I should design my client.
The user interacts with the client only once. He clicks Button, and the client is created and passed to the methods privateto do some logic in the background. It accesses objects from the server and synchronizes them with objects in the internal user database.
The fact is that client methods are accessed by methods privatethat are called after a single user action in the graphical interface. It has no control over which of the client methods are called, and in what order.
So my questions are:
Is it possible to request a server for a token only once when I create an instance of my client and then save it in a client instance for subsequent reference in the client for the following requests? A token is a hash of a username and password, so it should not change over time. Of course, as soon as I create a new client instance, it will again ask the server for a token.
Is it possible to save a single instance of an object Requestin my client? Then I can set the request header only once, and all methods that access the API will only need to change the request resource URL and the HTTP method. This will reduce code repeatability.
For instance:
public PriceListItem[] GetPriceListItems()
{
string requestUrl = Resources.PriceListItemsUrl;
var request = new RestRequest(requestUrl, Method.GET);
request.AddHeader("SecureToken", _token);
var response = Client.Execute(request) as RestResponse;
JObject jObject = JObject.Parse(response.Content);
var priceListItems = jObject["Data"].ToObject<PriceListItem[]>();
return priceListItems;
}
I have several ways to use different resource URLs, but they all have the same header. If I save only one instance Requestin my client, I can set the header only once. Is this approach okay? I would like to avoid any delegates and events.
source
share