有没有办法通过 Moya 使用 Alamofire 的 ResponseSerializers?

问题描述

我浏览了 Moya 的文档和源代码,但没有找到任何方法可以在通过 Moya 运行请求时利用 Alamofire 的 ResponseSerializers

有没有办法做到这一点?

我想做什么

将 Alamofire 的 AuthenticationInterceptor 与 REST API 结合使用,该 API 在响应的 JSON 正文而不是 HTTP 标头中发送 401 错误

为什么这不起作用

使用 Alamofire 的 AuthenticationInterceptor 时,retry 行为仅在 Alamofire 的 Request Pipeline 内发生错误时触发。

因为 401 错误位于 200 OK 响应的 JSON 正文中,所以它永远不会被 Alamofire 捕获。

我们围绕 MoyaProvider 制作的任何包装器都能够处理序列化,但此时我们已经在 Alamofire 的请求管道之外,因此,我们抛出的任何错误都不会触发 {{1} } 的 AuthenticationInterceptor 行为。

我必须做的

直接修改Moya的源码,在retry中注入一个responseSerializer,如下图:

Moya+Alamofire.swift

TLDR

Alamofire 的 ResponseSerializers 可以通过 Moya 使用吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)