Skip to content
This repository has been archived by the owner on Feb 28, 2023. It is now read-only.
/ MicroPinboardAPI Public archive

MicroPinboard is a Pinboard API wrapper, written in Swift using async/await

License

Notifications You must be signed in to change notification settings

otaviocc/MicroPinboardAPI

Repository files navigation

MicroPinboard

MicroPinboard is a Pinboard API wrapper, written in Swift using async/await.

public protocol PinboardAPIFactoryProtocol {
    func makePinboardAPIClient(
        userToken: @escaping () -> String?
    ) -> NetworkClientProtocol
}

The network client takes a single parameter: userToken: @escaping () -> String?, where the user token is passed to the client and is dynamically evaluated when needed (useful in those case where the network client is initialized before the user is asked to provide the token).

let factory = PinboardAPIFactory()

let client = factory.makePinboardAPIClient {
    "A_VALID_TOKEN_GOES_HERE"
}

Network requests are also built by factories, returning strongly-typed request objects:

// NetworkRequest<VoidRequest, DatesResponse>
let recentsRequest = PostsAPIFactory.makeDatesRequest()

The network client takes a single parameter, the request, returning NetworkResponse

public protocol NetworkClientProtocol {
    func run<RequestModel, ResponseModel>(
        _ networkRequest: NetworkRequest<RequestModel, ResponseModel>
    ) async throws -> NetworkResponse<ResponseModel>
}

E.g.:

let recentsRequest = PostsAPIFactory.makeRecentRequest()
let recentsResponse = try await client.run(recentsRequest)