配置Siesta资源的请求方法 [英] Configure request method for a Siesta resource

查看:118
本文介绍了配置Siesta资源的请求方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个api登录通过post工作,而大多数其他请求使用get。现在我正在使用siesta将登录URL定义为资源。

I have this api where the login works via post while most other requests work with get. Now I am using siesta to define the login url as a resource.

func login(username: String, password: String) -> Resource {
    return self.resource("login").withParam("username", username).withParam("password", password);
}

问题是当我在这个资源上使用.loadIfNeeded()时它会做一个获取请求,但这不起作用,因为它需要一个帖子请求。

The problem is that when I use .loadIfNeeded() on this resource it will do a get request, but that won't work because it needs to be a post request.

现在我知道.decorateRequests的存在,但我不确定如何使用它来使我的登录资源使用post作为请求方法。

Now I know of the existence of .decorateRequests, but I'm unsure how to use that to make my login resource use post as request method.

提前致谢,

Peter

推荐答案

Siesta的 load() loadIfNeeded ()仅用于GET请求。

Siesta’s load() and loadIfNeeded() are only for GET requests.

为什么?那些Siesta方法建立在假设它们没有副作用,它们的结果可以被缓存,以及它们可以安全地调用零,一次或多次的基础上。在HTTP中,这是GET的合同。但是,POST,PUT等不做任何这样的承诺;每个请求都可以有单独的效果,因此重复或可选地调用它们是危险的。

Why? Those Siesta methods are build on the assumption that they don’t have side effects, that their results can be cached, and that they are safe to call zero, one, or many times. In HTTP, this is the contract of GET. However, POST, PUT, etc do no make any such promise; each request can have a separate effect, and they are thus dangerous to call either repeatedly or optionally.

要使用POST,PUT和DELETE发出请求,请使用 Resource.request(...)

To make requests using POST, PUT, and DELETE, use Resource.request(…):

loginResource.request(.post)

(参见有关Siesta用户指南中请求的部分,了解请求(...)与<$ c>的不同之处$ c>加载(...)。)

为什么没有单独的设置例如,使资源成为POST资源?因为REST方法是 / foo 是逻辑事物的名称 - 资源 - 而 GET / foo PUT / foo 是不同的它上面有一个,它可以检索它的状态,另一个可以改变它。这不仅仅是审美纯洁的问题; GET的强烈承诺与它密切相关。

Why isn’t there a separate setting to, for example, make a resource be a "POST resource?" Because the REST approach is that /foo is the name of a logical thing — a resource — and GET /foo and PUT /foo are different actions on it, one that retrieves its state and one that changes it. This is not merely a matter of aesthetic purity; the strong promises of GET are closely tied to it.

如果您的API完全不是REST形状,Siesta可能不适合它。但是,您也可以编写一个 NetworkProvider ,将REST形状的请求转换为您自己的API结构,让Siesta将其视为REST API。

If your API is completely non-REST-shaped, Siesta may not be a good fit for it. However, you may also be able to write a NetworkProvider that translates REST-shaped requests into your API’s own structure, letting Siesta essentially treat it as a REST API.

API通常不会在查询字符串中使用密码(这是 withParam(...)确实如此),而是在帖子中。

APIs usually don’t take passwords in a query string (which is what withParam(…) does), but rather in a post body.

(旁白:如果您的API确实在查询字符串中使用了密码,您可能不想要查询字符串中的密码很容易泄漏到不安全的地方 - 例如日志文件。但是你知道你的API,我知道你经常需要使用你所拥有的东西!)

(Aside: if your API does take a password in a query string, you probably don’t want it to. Passwords in query strings are easily leak into insecure places — log files, for example. But you know your API, and I realize you often have to work with what you’ve got!)

如果你的API确实在POST正文中取代了密码而不是查询字符串,你可以这样做:

In the likely event that your API does take a password in a POST body instead of a query string, you can do this:

// If it’s a JSON request
loginResource.request(.post, json: ["user": user, "password": pass])

// If it an HTML form encoded request
loginResource.request(.post, urlEncoded: ["user": user, "password": pass])






如果您真的希望Siesta缓存您的身份验证调用的结果,就好像是GET请求一样,您可以使用 Resource.load(using :)


If you really want Siesta to cache the result of your auth call as if it had been a GET request, you can use Resource.load(using:):

authResource.load(using:
  authResource.request(
      .post, json: ["user": user, "password": pass]))

这是如果您想使用 ResourceObserver s在应用程序中发布身份验证凭据,则非常有用。更常见的方法是使用 onSuccess 挂钩获取凭据一次并更新服务配置,但 load(使用:) 可能是一个有用的选择。

This is useful if you want to publish your auth credentials within the app using ResourceObservers, for example. The more common approach is to use an onSuccess hook to grab the credentials once and update the service configuration, but load(using:) can be a helpful alternative in some situations.

这篇关于配置Siesta资源的请求方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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