Swiftpack.co - fsalom/TripleA as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by fsalom.
fsalom/TripleA 1.0.0
swift package to handle api calls easy way
⭐️ 0
🕓 2 weeks ago
iOS
.package(url: "https://github.com/fsalom/TripleA.git", from: "1.0.0")

Swift Swift Package Manager

TripleA: Async Await API

✍️ About

TripleA is an acronym for Async Await API in swift. It is a package created to simplify task of using REST API. It is main purpose is to be used with Rudo APIs. At the core of the system is URLSession and the URLSessionTask subclasses. TripleAAA wraps these APIs and provide a way to simplify and reduce code to make it easier to use calls to APIs

📦 Installation

Swift Package Manager

To install TripleA using Swift Package Manager you can follow the tutorial published by Apple using the URL for the TipleAAA repo with the current version:

In Xcode, select “File” → “Swift Packages” → “Add Package Dependency”

Enter https://github.com/fsalom/TripleA

🦾 Core elements

There are 4 main pieces in this system. Each one of them is responsable of their own area.

  • Endpoint: Its the way endpoint request are built for TripleA.
  • AuthManager: responsable of managing tokens and refresh them.
  • Network: responsable of making calls and parse objects.
  • TokenStore: responsable of storing token information (by default UserDefaults)

🚀 Usage non authorized API

As an example we will use https://coincap.io and their crypto list. This API is free to use and do not have any kind of authentication system

Defining dependency injection

Container.swift

Network(baseURL: "https://api.coincap.io/v2/")

DTO declaration

CryptoDTO

struct ListDTO: Codable {
    let data : [CryptoDTO]
}

struct CryptoDTO: Codable {
    let name : String!
    let priceUsd : String!
    let changePercent24Hr : String!
}

Endpoint

CryptoAPI

enum CryptoAPI {
    case assets
    var endpoint: Endpoint {
        get {
            switch self {
            case .assets:
                return Endpoint(path: "assets", httpMethod: .get)
            }
        }
    }
}

Making request

ViewModel

Task{
    do {
        _ = try await network.load(endpoint: CryptoAPI.assets.endpoint, of: ListDTO.self)
    } catch {

    }
}

🔒 Usage authorized API

An example for OAUTH2 grant_type = password

Defining dependency injection

Container.swift

let storage = AuthTokenStoreDefault()
let remoteDataSource = OAuthGrantTypePasswordManager(storage: storage, startController: getLoginController(), refreshTokenEndpoint: OAuthAPI.refresh(parametersRefresh).endpoint, tokensEndPoint: OAuthAPI.login(parametersLogin).endpoint)
let authManager = AuthManager(storage: storage,
                              remoteDataSource: remoteDataSource,
                              parameters: [:])
let network = Network(baseURL: "https://dashboard.rudo.es/", authManager: authManager)

DTO declaration

UserDTO

struct UserDTO: Codable {
    let firstName,
        email: String

    private enum CodingKeys: String, CodingKey {
        case firstName = "first_name"
        case email = "email"
    }
}

Endpoint

OauthAPI

enum OAuthAPI {
    case login([String: Any])
    case refresh([String: Any])
    case me
    var endpoint: Endpoint {
        get {
            switch self {
            case .login(let parameters):
                return Endpoint(baseURL: "https://dashboard.rudo.es/", path: "auth/token/", httpMethod: .post, parameters: parameters)
            case .refresh(let parameters):
                return Endpoint(baseURL: "https://dashboard.rudo.es/", path: "auth/token/", httpMethod: .post, parameters: parameters)
            case .me:
                return Endpoint(path: "users/me/", httpMethod: .get)
            }
        }
    }
}

Usage

Login

let parameters = ["grant_type": "password",
                  "username": "XXXX",
                  "password": "XXXX",
                  "client_id": "XXXX",
                  "client_secret": "XXXX"]
try await Container.network.getNewToken(with: parameters)

Calls

try await Container.network.loadAuthorized(endpoint: OAuthAPI.me.endpoint, of: UserDTO.self)

📚 Examples

This repo includes an iOS example, which is attached to Example.xcodeproj

👨‍💻 Author

Fernando Salom

GitHub

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

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