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

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

问题描述

我正在为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.

可以保留一个<$客户中的c $ c> 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上实施实现会话来改进它,因此您只需要检查auth令牌是否仍然有效,并在无效时进行身份验证。

  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 Pattern,您可以利用一些惰性实现或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客户端中的单个Request对象实例可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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