I'm pretty sure this should be possible and I'm just searching for the wrong thing, but what I'm going to do is perform a 2 network requests in the same publisher chain.
Basically I'm trying to fetch a JSON Web token (from the web or locally) then perform the requested fetch using that JWT.
I've created a custom Subscription and Publisher for getting my JWT, and it stores the token locally and checks if it's valid so it can be reused, renewed or requested for the first time. However I'm having trouble working out how to then use this with the fetch.
This is where I am (please ignore the force unwrap for now):
JWTState.publisher(JWTState.urlRequest!)
.tryMap { (jwtState) -> String in
guard jwtState.isValid == true else {
throw JWTError.invalidJWT
}
return jwtState.jwt
}
.map { (jwt) -> URLRequest in
URLRequest.jsonRequest(url: url, jwt: jwt)
}
At that point I have a valid URLRequst
for the fetch using a valid JWT, but I just can't work out how I call the next URLSession.shared.dataTaskPublisher
or URLSession.shared.dataTask
in the chain.
I'm hoping I've missed some publisher or function, or someone can steer me in the right direction. I'm also guessing I might have to create another custom Subscription and Publisher pair, but at the moment I can't see how doing that would help.
Thanks in advance.