REST 客户端中的单个请求对象实例好吗? [英] Is single Request object instance in REST client okay?

查看:23
本文介绍了REST 客户端中的单个请求对象实例好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 C# WinForm 应用程序编写一个简单的 REST 客户端.我使用 RestSharp 使发送请求和获得响应变得更容易.我有几个关于我应该如何设计我的客户端的问题.

I'm writing a simple REST client for a C# WinForm application. I use RestSharp to make sending requests and getting responses easier. I have a few questions regarding how I should design my client.

用户只与客户端交互一次.他按下一个Button,客户端被实例化并传递给private 方法在后台做一些逻辑.它从服务器访问对象并将它们与用户内部数据库中的对象同步.

The user interacts with the client only once. He pushes a Button and the client is instantiated and passed to private methods to do some logic in the background. It accesses objects from the server and synchronizes them with objects in the user's internal database.

关键是客户端的方法是通过private 方法访问的,该方法遵循用户在 GUI 中的单个操作.他无法控制调用客户端的哪些方法以及调用顺序.

The point is that the client's methods are accessed by private methods called following the user's single action in the GUI. He does not have any control over which of the client's methods are called, and in which order.

所以我的问题是:

  • 我可以在实例化客户端时只向服务器请求一次令牌,然后将其存储在客户端实例中以供将来在客户端的后续请求中参考吗?令牌是用户名和密码的散列,因此它不应随时间变化.当然,一旦我创建了客户端的新实例,它会再次向服务器请求令牌.

  • Can I ask the server for a token only once when I instantiate my client, and then store it in the client instance for future reference in the client's following requests? The token is a hash of the username and password, so it should not change over time. Of course, once I create a new instance of the client, it will again ask the server for a token.

是否可以在我的客户端中保留一个 Request 对象实例?然后我可以只设置一次请求头,所有访问 API 的方法只需要更改请求的资源 URL 和 HTTP 方法.它会减少我的代码中的重复性.

Is it okay to keep a single Request object instance in my client? I can then set request header only once and all the methods that access the API will only need to change the request's resource URL and HTTP method. It would reduce repetitiveness in my code.

例如:

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;
}

我有很多使用不同资源 URL 的方法,但都具有相同的标头.如果我在我的客户端中只保留一个 Request 实例,我只能设置一次标头.这个方法好吗?我想避免任何代表和活动.

I have quite a few methods for utilizing different resource URLs, but all have the same header. If I keep only one Request instance in my client I can set the header only once. Is this approach okay? I would like to avoid any delegates and events.

推荐答案

  1. 在客户端上保存身份验证令牌是完全正常的,只要它加密并且上面有过期时间.您可以通过在 REST API 上实现会话来改进它,因此您只需要检查身份验证令牌是否仍然有效,如果无效则进行身份验证.

  1. It's perfectly normal to save auth token on client, as long it's encrypted and have expired time on it. You can improve it with implement session on your REST API, so you just need check if the auth token is still valid or not, and do the authentication if it's not valid.

很明显,您需要管理向 REST API 请求的方式,我建议您使用 IDisposable 模式来实现这种方式,您可以使用一些惰性实现或 Singelton.

Clearly you need to manage the way you request to the REST API, I Recommend you to use IDisposable Pattern for this manner, you can utilize some lazy implementation or Singelton.

这篇关于REST 客户端中的单个请求对象实例好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆