Nil on data request, problem with handling requests from REST API - swift

I'm trying to inflate my layout with data, it works but on some attempts user filed or comments field just have still nil and it fails. How can I can optimise the requests? I'm still beginner with swift and I cannot wrap my had around this.
The class that fails is the findUserById which does just what it is in the name. So find the user for specific id from the users list. But sometimes users are not yet initalized and the error with force unwraping is visible
func findUserByUserId(UserId:Int) -> User{
var userPlaceholder : User!
for user in self.users {
if(user.id == UserId){
userPlaceholder = user
}
}
return userPlaceholder
}
This is the function that I'm using for setting up the structure of the table cell
private func fetchPost() {
self.posts.forEach { (post) in
DispatchQueue.global(qos: .userInteractive).async(group: dispatchGroup) {
self.dispatchGroup.enter()
self.postsCellViewModels.append(PostsCellViewModel(post: post, user: self.findUserByUserId(UserId: post.userId), comments: self.findCommensByPostId(PostId: post.id)))
self.dispatchGroup.leave()
}
}
dispatchGroup.notify(queue: .main) {
os_log("PostsViewModel -> Finished fetching posts")
self.isLoading = false
self.reloadData?()
}
And this is function for fetchingUsers from API, i have the same for posts and comments, data is taken from JSONPlaceholderAPI
func fetchUsers(){
dispatchGroup.enter()
os_log("PostsViewModel -> Starting users fetching")
if let client = client as? JSONPlaceholderClient {
self.isLoading = true
let endpoint = JsonPlaceHolderEndpoint.users
client.fetchUsers(with: endpoint) { (either) in
switch either {
case .success(let users):
self.users = users
os_log("PostsViewModel -> Ended users fetching")
self.dispatchGroup.leave()
case .error(let error):
self.showError?(error)
}
}
}
}

Related

Observable sequence called several times on retryWhen, but should call only once

I am trying to build RxSwift Auth token refresh service using following tutorial: https://www.donnywals.com/building-a-concurrency-proof-token-refresh-flow-in-combine/. However, I faced with issue, when user don't have an auth token and first refresh failed, but second refresh succeed, additional request is send, and after this (3-rd request) is completed, only then called main endpoint
So, what I see in network inspector:
request to refresh token (failed)
request to refresh token (succeed)
request to refresh token (succeed)
request to main endpoint (succeed)
But it should be:
request to refresh token (failed)
request to refresh token (succeed)
request to main endpoint (succeed)
I have following code for Authenticator
protocol AuthenticatorType {
func authenticate() -> Observable<Void>
func checkForValidAuthTokenOrRefresh(forceRefresh: Bool) -> Observable<Void>
}
extension AuthenticatorType {
func checkForValidAuthTokenOrRefresh(forceRefresh: Bool = false) -> Observable<Void> {
return checkForValidAuthTokenOrRefresh(forceRefresh: forceRefresh)
}
}
final class Authenticator<Provider: RxMoyaProviderType> where Provider.Target == AuthAPI {
private let provider: Provider
private let cookiesStorageProvider: CookiesStorageProviderType
private let queue = DispatchQueue(label: "Autenticator.\(UUID().uuidString)")
private var refreshInProgressObservable: Observable<Void>?
init(
provider: Provider,
cookiesStorageProvider: CookiesStorageProviderType
) {
self.provider = provider
self.cookiesStorageProvider = cookiesStorageProvider
}
func checkForValidAuthTokenOrRefresh(forceRefresh: Bool = false) -> Observable<Void> {
return queue.sync { [weak self] in
self?.getCurrentTokenOrRefreshIfNeeded(forceRefresh: forceRefresh) ?? .just(())
}
}
func authenticate() -> Observable<Void> {
provider.request(.authenticate(credentials: .defaultDebugAccount))
.map(LoginResponse.self)
.map { loginResponse in
guard loginResponse.login else {
throw AuthenticationError.loginRequired
}
}
.asObservable()
}
}
// MARK: - Helper methods
private extension Authenticator {
func getCurrentTokenOrRefreshIfNeeded(forceRefresh: Bool = false) -> Observable<Void> {
if let refreshInProgress = refreshInProgressObservable {
return refreshInProgress
}
if cookiesStorageProvider.isHaveValidAuthToken && !forceRefresh {
return .just(())
}
guard cookiesStorageProvider.isHaveValidRefreshToken else {
return .error(AuthenticationError.loginRequired)
}
let refreshInProgress = provider.request(.refreshToken)
.share()
.map { response in
guard response.statusCode != 401 else {
throw AuthenticationError.loginRequired
}
return response
}
.map(RefreshReponse.self)
.map { refreshResponse in
guard refreshResponse.refresh else {
throw AuthenticationError.loginRequired
}
}
.asObservable()
.do(
onNext: { [weak self] _ in self?.resetProgress() },
onError: { [weak self] _ in self?.resetProgress() }
)
refreshInProgressObservable = refreshInProgress
return refreshInProgress
}
func resetProgress() {
queue.sync { [weak self] in
self?.refreshInProgressObservable = nil
}
}
}
And thats how I refresh doing request (with logics to refresh token)
func request(_ token: Target, callbackQueue: DispatchQueue?) -> Observable<Response> {
authenticator.checkForValidAuthTokenOrRefresh()
.flatMapLatest { [weak self] res -> Observable<Response> in
self?.provider.request(token).asObservable() ?? .empty()
}
.map { response in
guard response.statusCode != 401 else {
throw AuthenticationError.loginRequired
}
return response
}
.retry { [weak self] error in
error.flatMap { error -> Observable<Void> in
guard let authError = error as? AuthenticationError, authError == .loginRequired else {
return .error(error)
}
return self?.authenticator.checkForValidAuthTokenOrRefresh(forceRefresh: true) ?? .never()
}
}
}
At first, I thought it was concurrency problem, I changed queue to NSLock, but it all was the same. Also I tried to use subscribe(on:) and observe(on:), thats also don't give any effect.
Maybe issue with do block, where I set refreshInProgressObservable to nil, because when I change onError, to afterError, I don't see third request to refresh token, but I also don't see any request to main endpoint.
I even tried to remove share(), but as you guess it don't help either.
Ah, and also I remember that 3-rd request fires instantly after second is completed, even if I add sleep in beginning of getCurrentTokenOrRefreshIfNeeded method. So that kinda strange
Edit
I tried another way to refresh token, using deferred block in Observable (inspired by Daniel tutorial).
Here is my code
final class NewProvider {
let authProvider: MoyaProvider<AuthAPI>
let apiProvider: MoyaProvider<AppAPI>
let refreshToken: Observable<Void>
init(authProvider: MoyaProvider<AuthAPI>, apiProvider: MoyaProvider<AppAPI>) {
self.authProvider = authProvider
self.apiProvider = apiProvider
refreshToken = authProvider.rx.request(.refreshToken)
.asObservable()
.share()
.map { _ in }
.catchAndReturn(())
}
func request(_ token: AppAPI) -> Observable<Response> {
Observable<Void>
.deferred {
if CookiesStorageProvider.isHaveValidAuthToken {
return .just(())
} else {
throw AuthenticationError.loginRequired
}
}
.flatMapLatest { [weak self] _ in
self?.apiProvider.rx.request(token).asObservable() ?? .never()
}
.retry { [weak self] error in
return error.flatMapLatest { [weak self] _ in
self?.refreshToken ?? .never()
}
}
}
}
It works perfectly for one request (like, "it sends request to refresh token only when auth token is missing and try to refresh token again if token refresh failed")
However, there is problem with multiple requests. If there is no auth token and multiple request are fired, it works well, requests are waiting for token to refresh. BUT, if token refresh failed, there is no attempt to try refresh token again. I don't know what can lead to this behaviour.
EDIT 2
I found out that if I place
.observe(on: SerialDispatchQueueScheduler(queue: queue, internalSerialQueueName: "test1"))
after
.share()
refreshToken = authProvider.rx.request(.refreshToken)
.asObservable()
.share()
.observe(on: SerialDispatchQueueScheduler(queue: queue, internalSerialQueueName: "test1"))
.map { _ in }
.catchAndReturn(())
All will be work as expected, but now I can't understand why its working this way
Okay, I pulled down your code and spent a good chunk of the day looking it over. A couple of review points:
This is way more complex than it needs to be for what it's doing.
Any time you have a var Observable, you are doing something wrong. Observables and Subjects should always be let.
There is no reason or need to use a DispatchQueue the way you did for Observables. This code doesn't need one at all, but even if it did, you should be passing in a Scheduler instead of using queues directly.
I could see no way for your code to actually use the new token in the retry once it has been received. Even if these tests did pass, the code still wouldn't work.
As far as this specific question is concerned. The fundamental problem is that you are calling getCurrentTokenOrRefreshIfNeeded(forceRefresh:) four times in the offending test and creating three refreshInProgress Observables. You are making three of them, because the second one has emitted a result and been disposed before the last call to the function is made. Each one emits a value so you end up with three next events in authAPIProviderMock.recordedEvents.
What is the fix? I could not find a fix without making major changes to the basic structure/architecture of the code. All I can do at this point is suggest that you check out my article on this subject RxSwift and Handling Invalid Tokens which contains working code for this use case and includes unit tests. Or revisit Donny's article which I presume works, but since there are no unit tests for his code, I can't be sure.
Edit
In answer to your question in the comments, here is how you would solve the problem using my service class:
First create a tokenAcquisitionService object. Since you don't actually need to pass a token value around, just use Void for the token type.
let service = TokenAcquisitionService(initialToken: (), getToken: { _ in URLSession.shared.rx.response(request: refreshTokenRequest) }, extractToken: { _ in })
(Use whatever you want in place of URLSession.shared.rx.response(request: refreshTokenRequest). The only requirement is that it returns an Observable<(response: HTTPURLResponse, data: Data)> and in this case the data can simply be Data() or anything else, since it is ignored. It can even present a view controller that asks the user to login.)
Now at the end of every request, include the following.
.do(onNext: { response in
guard response.response.statusCode != 401 else { throw TokenAcquisitionError.unauthorized }
})
.retry(when: { $0.renewToken(with: tokenAcquisitionService) })
Wrap the above however you want so you don't have to copy pasted it onto every request.
QED

Subsequent ordered HTTP calls

I'm building a simple iOS client for HackerNews. I'm using their APIs, according to which I'll be able to get the ordered post IDs (sorted by new, best and top) and a single post item passing the ID to the request. The problem I'm facing is the following: how can I, once I get the IDs array, make an HTTP call for every post in an ordered fashion? With the way I currently implemented it, I'm not having any luck.
E.g. say the IDs array is [3001, 3002, 3003, 3004]. I tried calling the method to get those posts inside a for loop issuing dispatch groups and dispatch semaphores, but I still get them unordered, like the call for item 3003 completes before 3002, and so on.
The methods I'm using:
#Published var posts: [Post] = []
func getPosts(feedType: FeedType){
posts = []
self.getFeedIDs(feedType: feedType).subscribe{ ids in
let firstFifteen = ids[0...15]
let dGroup = DispatchGroup()
let dQueue = DispatchQueue(label: "network-queue")
let dSemaphore = DispatchSemaphore(value: 0)
dQueue.async {
for id in firstFifteen{
dGroup.enter()
self.getPost(id: id).subscribe{ post in
self.posts.append(post)
dSemaphore.signal()
dGroup.leave()
}
dSemaphore.wait()
}
}
}
}
func getFeedIDs(feedType: FeedType) -> Observable<[Int]> {
return self.execute(url: URL(string: "https://hacker-news.firebaseio.com/v0/\(feedType)stories.json")!)
}
func getPost(id: Int) -> Observable<Post>{
return self.execute(url: URL(string: "https://hacker-news.firebaseio.com/v0/item/\(id).json")!)
}
func execute <T: Decodable>(url: URL) -> Observable<T> {
return Observable.create { observer -> Disposable in
let task = URLSession.shared.dataTask(with: url) { res, _, _ in
guard let data = res, let decoded = try? JSONDecoder().decode(T.self, from: data) else {
return
}
observer.onNext(decoded)
observer.onCompleted()
}
task.resume()
return Disposables.create {
task.cancel()
}
}
}
Any help would be greatly appreciated.
The semaphore makes no sense and is inefficient anyway.
Use the same pattern which Apple suggests in conjunction with TaskGroups: Collect the data in a dictionary and after being notified sort the data by the dictionary keys
func getPosts(feedType: FeedType){
var postData = [Int:Post]()
posts = []
self.getFeedIDs(feedType: feedType).subscribe{ ids in
let firstFifteen = ids[0...15]
let dGroup = DispatchGroup()
for (index, element) in firstFifteen.enumerated() {
dGroup.enter()
self.getPost(id: element).subscribe{ post in
postData[index] = post
dGroup.leave()
}
}
dGroup.notify(queue: .main) {
for key in postData.keys.sorted() {
posts.append(postData[key]!)
}
}
}
}

Using Combines zip with two api calls that return Result<T, Error>

I have two api calls using Combine and Result that I need to merge so that i can create a new objects from the two.
public func getPosts() -> AnyPublisher<Result<[Post], Error>, Never> {
let client = ForumClient()
return client.execute(.posts)
}
public func getUsers() -> AnyPublisher<Result<[User], Error>, Never> {
let client = ForumClient()
return client.execute(.users)
}
I have working code, but I feel like I'm missing some syntactic sugar that can avoid the two switches.
Publishers.Zip(getPosts(), getUsers())
.sink(
receiveValue: { [weak self] postsResult, usersReult in
guard let self = self else {
return
}
// Get Posts
var posts: [Post] = []
switch postsResult {
case .failure(let error):
print("Error: \(error.localizedDescription)")
case .success(let postsArray):
posts = postsArray
}
// Get Users
var users: [User] = []
switch usersReult {
case .failure(let error):
print("Error: \(error.localizedDescription)")
case .success(let usersArray):
users = usersArray
}
//Combine posts and users
var forumPosts: [ForumPost] = []
posts.forEach { post in
users.forEach { user in
if user.id == post.userId {
let forumPost = ForumPost(username: user.username, title: post.title)
forumPosts.append(forumPost)
}
}
}
print(forumPosts)
})
.store(in: &publishers)
Is there a better way to do this that avoids using two switches etc?
Answering whether another way is "better" is hard.
At some point, you're going to have to deal with the fact that one of your requests for posts or users might fail. That's going to require looking at a Result. So far as I know, that can be done in one of two ways. Either using a switch or using the get() throws function in Result. The advantage of the switch is that you can look at the error and do something.
What I might choose to do, is put the error handling a bit closer to where the error might occur. I would want to have getPosts return a publisher of posts. If an error occurs I might emit the error inside that but still return all the posts I can get. Similarly for users.
Consider the following code that I put together in a Playground:
import UIKit
import Combine
struct Post {
let userId: Int
let title : String
}
struct User {
let userId: Int
let username : String
}
struct ForumPost {
let username: String
let title: String
}
func clientGetPosts() -> AnyPublisher<Result<[Post], Error>, Never> {
let results =
[ Result<[Post], Error>.success([
Post( userId: 1, title: "The World According to Alice"),
Post( userId: 2, title: "The World Accoording to Bob")
]) ]
return results.publisher.eraseToAnyPublisher()
}
func clientGetUsers() -> AnyPublisher<Result<[User], Error>, Never> {
let results =
[ Result<[User], Error>.success([
User(userId: 1, username: "Alice"),
User(userId: 2, username: "Bob")
]) ]
return results.publisher.eraseToAnyPublisher()
}
func getPosts() -> AnyPublisher<[Post], Never> {
clientGetPosts().reduce([]) {
(allPosts : [Post], postResult: Result<[Post], Error>) -> [Post] in
var newCollection = allPosts
switch postResult {
case .success(let newPosts) :
newCollection.append(contentsOf: newPosts)
case .failure(let error) :
print("Retrieving posts errored: \(error.localizedDescription)")
}
return newCollection
}.eraseToAnyPublisher()
}
func getUsers() -> AnyPublisher<[User], Never> {
clientGetUsers().reduce([]) {
(allUsers : [User], usersResult: Result<[User], Error>) -> [User] in
var newCollection = allUsers
switch usersResult {
case .success(let newUsers) :
newCollection.append(contentsOf: newUsers)
case .failure(let error) :
print("Retrieving users errored: \(error.localizedDescription)")
}
return newCollection
}.eraseToAnyPublisher()
}
var users : [User] = []
getUsers().sink { users = $0 }
let forumPosts = getPosts().flatMap {
(posts: [Post]) -> AnyPublisher<ForumPost, Error> in
return posts.publisher.tryMap {
(post:Post) throws -> ForumPost in
if let user = users.first(where: { $0.userId == post.userId }) {
return ForumPost(username: user.username, title: post.title)
} else {
print("Post with title \"\(post.title)\" has an unknown userId")
throw NSError(domain: "posters", code: -1, userInfo: nil)
}
}.eraseToAnyPublisher()
}.eraseToAnyPublisher()
forumPosts.sink(receiveCompletion: { _ in () },
receiveValue: {
(forumPost: ForumPost) in
debugPrint(forumPost)
})
the function clientGetPosts stands in for client.execute(.posts). I've changes getPosts so that it collects all the posts and emits errors, but returns all the posts it can find. There's still a switch statement, but it's closer to where the error is discovered.
Users are handled similarly.
At the bottom, I just grab the list of users. Because of the way we've set up getUsers it will grab as many users as it can and print out errors if any of the clients requests fail.
It then grabs the sequence that returns all the posts and uses flatMap to convert that into a sequence of ForumPosts with an error emitted for any forum post where a corresponding user can't be found.
You can combine the results into a switch statement:
switch (usersResult, postsResult) {
case (.success(let users), .success(let posts)):
print(makeForumPostsFrom(users: users, posts: posts))
case (.failure(let userError), .failure(let postError)):
print("UserError: \(userError)")
print("PostError: \(postError)")
case (.failure(let userError), _):
print("UserError: \(userError)")
case (_, .failure(let postError)):
print("PostError: \(postError)")
}
I created a utility method to create a ForumPost array from the resulting User and Post arrays:
func makeForumPostsFrom(users: [User], posts: [Post]) -> [ForumPost] {
users.reduce(into: .init()) { forumPosts, user in
let usersForumPosts = posts
.filter { post in
post.userId == user.id
}
.map { post in
ForumPost(username: user.username, title: post.title)
}
forumPosts.append(contentsOf: usersForumPosts)
}
}
All I did here was create an empty array of ForumPost, then for each user filter for their posts, then map those posts to a corresponding ForumPost, then store the results in the initial array.

Swift Siesta - How to include asynchronous code into a request chain?

I try to use Siesta decorators to enable a flow where my authToken gets refreshed automatically when a logged in user gets a 401. For authentication I use Firebase.
In the Siesta documentation there is a straight forward example on how to chain Siesta requests, but I couldn't find a way how to get the asynchronous Firebase getIDTokenForcingRefresh:completion: working here. The problem is that Siesta always expects a Request or a RequestChainAction to be returned, which is not possible with the Firebase auth token refresh api.
I understand that the request chaining is primarily done for Siesta-only use cases. But is there a way to use asynchronous third party APIs like FirebaseAuth which don't perfectly fit in the picture?
Here is the code:
init() {
configure("**") {
$0.headers["jwt"] = self.authToken
$0.decorateRequests {
self.refreshTokenOnAuthFailure(request: $1)
}
}
func refreshTokenOnAuthFailure(request: Request) -> Request {
return request.chained {
guard case .failure(let error) = $0.response, // Did request fail…
error.httpStatusCode == 401 else { // …because of expired token?
return .useThisResponse // If not, use the response we got.
}
return .passTo(
self.createAuthToken().chained { // If so, first request a new token, then:
if case .failure = $0.response { // If token request failed…
return .useThisResponse // …report that error.
} else {
return .passTo(request.repeated()) // We have a new token! Repeat the original request.
}
}
)
}
}
//What to do here? This should actually return a Siesta request
func createAuthToken() -> Void {
let currentUser = Auth.auth().currentUser
currentUser?.getIDTokenForcingRefresh(true) { idToken, error in
if let error = error {
// Error
return;
}
self.authToken = idToken
self.invalidateConfiguration()
}
}
Edit:
Based on the suggested answer of Adrian I've tried the solution below. It still does not work as expected:
I use request() .post to send a request
With the solution I get a failure "Request Cancelled" in the callback
After the callback of createUser was called, the original request is sent with the updated jwt token
This new request with the correct jwt token is lost as the callback of createUser is not called for the response -> So onSuccess is never reached in that case.
How do I make sure that the callback of createUser is only called after the original request was sent with the updated jwt token?
Here is my not working solution - happy for any suggestions:
// This ends up with a requestError "Request Cancelled" before the original request is triggered a second time with the refreshed jwt token.
func createUser(user: UserModel, completion: #escaping CompletionHandler) {
do {
let userAsDict = try user.asDictionary()
Api.sharedInstance.users.request(.post, json: userAsDict)
.onSuccess {
data in
if let user: UserModel = data.content as? UserModel {
completion(user, nil)
} else {
completion(nil, "Deserialization Error")
}
}.onFailure {
requestError in
completion(nil, requestError)
}
} catch let error {
completion(nil, nil, "Serialization Error")
}
}
The Api class:
class Api: Service {
static let sharedInstance = Api()
var jsonDecoder = JSONDecoder()
var authToken: String? {
didSet {
// Rerun existing configuration closure using new value
invalidateConfiguration()
// Wipe any cached state if auth token changes
wipeResources()
}
}
init() {
configureJSONDecoder(decoder: jsonDecoder)
super.init(baseURL: Urls.baseUrl.rawValue, standardTransformers:[.text, .image])
SiestaLog.Category.enabled = SiestaLog.Category.all
configure("**") {
$0.expirationTime = 1
$0.headers["bearer-token"] = self.authToken
$0.decorateRequests {
self.refreshTokenOnAuthFailure(request: $1)
}
}
self.configureTransformer("/users") {
try self.jsonDecoder.decode(UserModel.self, from: $0.content)
}
}
var users: Resource { return resource("/users") }
func refreshTokenOnAuthFailure(request: Request) -> Request {
return request.chained {
guard case .failure(let error) = $0.response, // Did request fail…
error.httpStatusCode == 401 else { // …because of expired token?
return .useThisResponse // If not, use the response we got.
}
return .passTo(
self.refreshAuthToken(request: request).chained { // If so, first request a new token, then:
if case .failure = $0.response {
return .useThisResponse // …report that error.
} else {
return .passTo(request.repeated()) // We have a new token! Repeat the original request.
}
}
)
}
}
func refreshAuthToken(request: Request) -> Request {
return Resource.prepareRequest(using: RefreshJwtRequest())
.onSuccess {
self.authToken = $0.text // …make future requests use it
}
}
}
The RequestDelegate:
class RefreshJwtRequest: RequestDelegate {
func startUnderlyingOperation(passingResponseTo completionHandler: RequestCompletionHandler) {
if let currentUser = Auth.auth().currentUser {
currentUser.getIDTokenForcingRefresh(true) { idToken, error in
if let error = error {
let reqError = RequestError(response: nil, content: nil, cause: error, userMessage: nil)
completionHandler.broadcastResponse(ResponseInfo(response: .failure(reqError)))
return;
}
let entity = Entity<Any>(content: idToken ?? "no token", contentType: "text/plain")
completionHandler.broadcastResponse(ResponseInfo(response: .success(entity))) }
} else {
let authError = RequestError(response: nil, content: nil, cause: AuthError.NOT_LOGGED_IN_ERROR, userMessage: "You are not logged in. Please login and try again.".localized())
completionHandler.broadcastResponse(ResponseInfo(response: .failure(authError)))
}
}
func cancelUnderlyingOperation() {}
func repeated() -> RequestDelegate { RefreshJwtRequest() }
private(set) var requestDescription: String = "CustomSiestaRequest"
}
First off, you should rephrase the main thrust of your question so it's not Firebase-specific, along the lines of "How do I do request chaining with some arbitrary asynchronous code instead of a request?". It will be much more useful to the community that way. Then you can mention that Firebase auth is your specific use case. I'm going to answer your question accordingly.
(Edit: Having answered this question, I now see that Paul had already answered it here: How to decorate Siesta request with an asynchronous task)
Siesta's RequestDelegate does what you're looking for. To quote the docs: "This is useful for taking things that are not standard network requests, and wrapping them so they look to Siesta as if they are. To create a custom request, pass your delegate to Resource.prepareRequest(using:)."
You might use something like this as a rough starting point - it runs a closure (the auth call in your case) that either succeeds with no output or returns an error. Depending on use, you might adapt it to populate the entity with actual content.
// todo better name
class SiestaPseudoRequest: RequestDelegate {
private let op: (#escaping (Error?) -> Void) -> Void
init(op: #escaping (#escaping (Error?) -> Void) -> Void) {
self.op = op
}
func startUnderlyingOperation(passingResponseTo completionHandler: RequestCompletionHandler) {
op {
if let error = $0 {
// todo better
let reqError = RequestError(response: nil, content: nil, cause: error, userMessage: nil)
completionHandler.broadcastResponse(ResponseInfo(response: .failure(reqError)))
}
else {
// todo you might well produce output at this point
let ent = Entity<Any>(content: "", contentType: "text/plain")
completionHandler.broadcastResponse(ResponseInfo(response: .success(ent)))
}
}
}
func cancelUnderlyingOperation() {}
func repeated() -> RequestDelegate { SiestaPseudoRequest(op: op) }
// todo better
private(set) var requestDescription: String = "SiestaPseudoRequest"
}
One catch I found with this is that response transformers aren't run for such "requests" - the transformer pipeline is specific to Siesta's NetworkRequest. (This took me by surprise and I'm not sure that I like it, but Siesta seems to be generally full of good decisions, so I'm mostly taking it on faith that there's a good reason for it.)
It might be worth watching out for other non request-like behaviour.

Handling 401 status w/ RxSwift & URLSession

I currently have a network client that looks like the below:
class Client<R: ResourceType> {
let engine: ClientEngineType
var session: URLSession
init(engine: ClientEngineType = ClientEngine()) {
self.engine = engine
self.session = URLSession.shared
}
func request<T: Codable>(_ resource: R) -> Single<T> {
let request = URLRequest(resource: resource)
return Single<T>.create { [weak self] single in
guard let self = self else { return Disposables.create() }
let response = self.session.rx.response(request: request)
return response.subscribe(
onNext: { response, data in
if let error = self.error(from: response) {
single(.error(error))
return
}
do {
let decoder = JSONDecoder()
let value = try decoder.decode(T.self, from: data)
single(.success(value))
} catch let error {
single(.error(error))
}
},
onError: { error in
single(.error(error))
})
}
}
struct StatusCodeError: LocalizedError {
let code: Int
var errorDescription: String? {
return "An error occurred communicating with the server. Please try again."
}
}
private func error(from response: URLResponse?) -> Error? {
guard let response = response as? HTTPURLResponse else { return nil }
let statusCode = response.statusCode
if 200..<300 ~= statusCode {
return nil
} else {
return StatusCodeError(code: statusCode)
}
}
}
Which I can then invoke something like
let client = Client<MyRoutes>()
client.request(.companyProps(params: ["collections": "settings"]))
.map { props -> CompanyModel in return props }
.subscribe(onSuccess: { props in
// do something with props
}) { error in
print(error.localizedDescription)
}.disposed(by: disposeBag)
I'd like to start handling 401 responses and refreshing my token and retrying the request.
I'm struggling to find a nice way to do this.
I found this excellent gist that outlines a way to achieve this, however I am struggling to implement this in my current client.
Any tips or pointers would be very much appreciated.
That's my gist! (Thanks for calling it excellent.) Did you see the article that went with it? https://medium.com/#danielt1263/retrying-a-network-request-despite-having-an-invalid-token-b8b89340d29
There are two key elements in handling 401 retries. First is that you need a way to insert tokens into your requests and start your request pipeline with Observable.deferred { tokenAcquisitionService.token.take(1) }. In your case, that means you need a URLRequest.init that will accept a Resource and a token, not just a resource.
The second is to throw a TokenAcquisitionError.unauthorized error when you get a 401 and end your request pipeline with .retryWhen { $0.renewToken(with: tokenAcquisitionService) }
So, given what you have above, in order to handle token retries all you need to do is bring my TokenAcquisitionService into your project and use this:
func getToken(_ oldToken: Token) -> Observable<(response: HTTPURLResponse, data: Data)> {
fatalError("this function needs to be able to request a new token from the server. It has access to the old token if it needs that to request the new one.")
}
func extractToken(_ data: Data) -> Token {
fatalError("This function needs to be able to extract the new token using the data returned from the previous function.")
}
let tokenAcquisitionService = TokenAcquisitionService<Token>(initialToken: Token(), getToken: getToken, extractToken: extractToken)
final class Client<R> where R: ResourceType {
let session: URLSession
init(session: URLSession = URLSession.shared) {
self.session = session
}
func request<T>(_ resource: R) -> Single<T> where T: Decodable {
return Observable.deferred { tokenAcquisitionService.token.take(1) }
.map { token in URLRequest(resource: resource, token: token) }
.flatMapLatest { [session] request in session.rx.response(request: request) }
.do(onNext: { response, _ in
if response.statusCode == 401 {
throw TokenAcquisitionError.unauthorized
}
})
.map { (_, data) -> T in
return try JSONDecoder().decode(T.self, from: data)
}
.retryWhen { $0.renewToken(with: tokenAcquisitionService) }
.asSingle()
}
}
Note, it could be the case that the getToken function has to, for example, present a view controller that asks for the user's credentials. That means you need to present your login view controller (or a UIAlertController) to gather the data. Or maybe you get both an authorization token and a refresh token from your server when you login. In that case the TokenAcquisitionService should hold on to both of them (i.e., its T should be a (token: String, refresh: String). Either is fine.
The only problem with the service is that if acquiring the new token fails, the entire service shuts down. I haven't fixed that yet.