Swiftpack.co - aliaslab-1984/AuthenticatedRequests as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by aliaslab-1984.
aliaslab-1984/AuthenticatedRequests 0.1.6
A lightweight API client written in Swift.
⭐️ 0
🕓 1 week ago
iOS
.package(url: "https://github.com/aliaslab-1984/AuthenticatedRequests.git", from: "0.1.6")

Authenticated Requests

This package aims to make your authenticated requests easier to perform. Each resource is described by the Resource protocol, which describes all the necessary information to retrieve a REST object from your backend.

Before requesting a resource you need to instantiate an Authenticator instance. An Authenticator is an actor which is responsible to keep track of the authentication status for each client.

Configuring an Authenticator is really simple:

let endpoint = AuthenticationEndpoint(baseEndpoint: URL(staticString: "https://api.example.com"), path: "auth/v2/token")
let authenticator = ARAuthenticator(baseEndpoint: endpoint)
let client = ARClientCredentials(clientID: "esempio", clientSecret: "esempio", scope: Set([]))
await authenticator.configure(with: client)
// We are ready to get our first Authenticated resource!

If you look close, you'll see that AuthenticationEndpoint is a Resource as well!

Now that we know what a Resource is, we can also integrate an AuthenticatedResource, which is an extension of what a Resource is. If you need some sort of authentication to retrieve a resource, you need to conform your Resource object to AuthenticatedResource as well.

struct UserFavoritesRequest: Resource, AuthenticatedResource { 

    // Resource

    var httpMethod: HttpMethod { 
        return .get
    }
    
    func urlRequest(using parameter: Input) throws -> URLRequest {
        // Decide how you compose your URLRquest...
    }
    
    // AuthenticatedResource
    
    var authenticator: AnyAuthenticator<ARClientCredentials> { 
    
    }
    
    func configure(with credentials: ARClientCredentials) async {
    
    }

}

By conforming to AuthenticatedResource, the SDK will automatically embed a bearer token to every request that you perform for this object (based on your ARClientCredentials clientID). If you explore the AuthenticatedResource definition, you'll see that it only has one requirement: Provide an Authenticator object.


We are now ready to perform our first request!

let resourceRequest = UserFavoritesRequest(authenticator: authenticator)
        
let result = try await resourceRequest.request(using: userInput)

GitHub

link
Stars: 0
Last commit: 1 week ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

Fixes FileManager errors
1 week ago

Fixes an error that occurred on devices running iOS 14 and older when finishing the download of a file.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics