This return is sending back a single string, the accessToken
from the ContentResponse
:
return body.encodeResponse(for: request)
.map { $0.body.buffer }
.flatMap { buffer in
return request.client.post(url, headers: self.callbackHeaders) { $0.body = buffer }
}.flatMapThrowing { response in
return try response.content.get(ContentResponse.self)
}.map { $0.accessToken }
which is being received in the completion handler here:
return try self.fetchToken(from: request).flatMap { accessToken in
How can I send back two strings - both the accessToken
and the refreshToken
from the ContentResponse
?
Thanks so much for any help in how to structure this!
CodePudding user response:
You could define and use a struct
to return, but for one-off cases like this, I usually just go with a tuple with named members:
return body.encodeResponse(for: request)
.map { $0.body.buffer }
.flatMap { buffer in
return request.client.post(url, headers: self.callbackHeaders) { $0.body = buffer }
}.flatMapThrowing { response in
return try response.content.get(ContentResponse.self)
}.map { (accessToken: $0.accessToken, refreshToken: $0.refreshToken) } // <<- This is changed line
That will result in a return type of [(accessToken: String, refreshToken: String)]
instead of [String]
, so the return type of fetchToken(from:)
will have to be changed to match.
Then the code where you call it looks like this:
return try self.fetchToken(from: request).flatMap { tokens in // <<- renamed to indicate it contains more than one value
/*
refer to tokens.accessToken and tokens.refreshToken in whatever
code goes here
*/
...
}