A simple network stack using Combine or Async/Await to perform network requests and OAuth2Token refreshing.
-
For unprotected network requests (requests that do not require a token), initialize a
Network
with an object that conforms toAPIEnvironment
. Then callrequest
on theNetwork
you created. -
To perform a protected network request (requests that require an auth token of some kind), create an object that conforms to
ProtectedAPIEnvironment
and then initialize aNetwork
with that environment. Then callrequest
on theNetwork
you created. -
You can use the
HTTPMethod
enum when setting the httpMethod on theURLRequest
-
Subscribe to
request<T: Codable>(_ urlRequest: URLRequest, responseAs: T.Type)
onNetworkManager
to perform a request. Pass in the url request and the Type you want to be returned that conforms toCodable
. The publisher will publish<T, APIError>
-
You can also set the
retryCount
andJSONDecoder
when making the request. The network manager will retry the request if it fails when trying to make the request. TheJSONDecoder
can be set for custom decoding of the json that will be returned from the request. -
You can subscribe to
requestIgnoringError<T: Codable>(_ urlRequest: URLRequest, responseAs: T.Type) -> AnyPublisher<T?, Never>
if you wish to make a request and want to ignore any possible errors. If an error does occur then the publisher will publishnil
-
In rare cases where you need to access to the
URLSession.DataTaskPublisher.Output
directly, you can subscribe toperform(_ urlRequest: URLRequest) -> AnyPublisher<URLSession.DataTaskPublisher.Output, URLError>
on theNetworkManager
-
guard let request = urlRequest else { return }
- First create aURLRequest
-
let networkManger = Network(environment: ProtectedEnvironment())
- Initialize aNetwork
with an object that comforms to anAPIEnvironment
or aProtectedAPIEnvironment
-
self.subscriber = networkManager.request(request, responseAs: CodableObject.self) .sink(receiveCompletion: { completion in switch completion { case .failure(let error): print(error) case .finished: break } }, receiveValue: { codableObject in self.handle(codableObject) })
- Subscribe to therequest
and then handle the publishederror
or decoded object
-
Call
func request<T: Decodable>(_ urlRequest: URLRequest, responseAs: T.Type) async throws
to perform a request. Pass in the url request and the Type you want to be returned that conforms toCodable
. The request will decode the response for you. -
In the case where you need access to the
(Data, URLResponse)
then callperform(_ urlRequest: URLRequest) async throws
-
guard let request = urlRequest else { return }
- First create aURLRequest
-
let networkManger = Network(environment: ProtectedEnvironment())
- Initialize aNetwork
with an object that comforms to anAPIEnvironment
or aProtectedAPIEnvironment
-
do { let object = try await networkManager.request(request, responseAs: CodableObject.self) } catch { handle(error) }
- await for the decoded object in a do/catch block to handle any possible errors