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
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
There are 4 main pieces in this system. Each one of them is responsable of their own area.
It is the core of this package. It provides different ways of calling an API.
public init(baseURL: String,
authManager: AuthManager? = nil,
headers: [String: String] = [:],
format: LogFormat = .full)
Network(baseURL: "https://api.coincap.io/v2/")
Required properties:
Optional properties:
Each endpoint has its own Endpoint object. This object has different properties to create the request
public init(path: String,
contentType: ContentType? = nil,
httpMethod: HTTPMethod,
body: Data? = nil,
parameters: [String: Any] = [:],
headers: [String: String] = [:],
query: [String: Any] = [:])
Endpoint(path: "https://api.coincap.io/v2/assets", httpMethod: .get)
Required properties:
Optional properties:
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
Container.swift
Network(baseURL: "https://api.coincap.io/v2/")
CryptoDTO
struct ListDTO: Codable {
let data : [CryptoDTO]
}
struct CryptoDTO: Codable {
let name : String!
let priceUsd : String!
let changePercent24Hr : String!
}
CryptoAPI
enum CryptoAPI {
case assets
var endpoint: Endpoint {
get {
switch self {
case .assets:
return Endpoint(path: "assets", httpMethod: .get)
}
}
}
}
ViewModel
Task{
do {
_ = try await network.load(endpoint: CryptoAPI.assets.endpoint, of: ListDTO.self)
} catch {
}
}
An example for OAUTH2 grant_type = password
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)
UserDTO
struct UserDTO: Codable {
let firstName,
email: String
private enum CodingKeys: String, CodingKey {
case firstName = "first_name"
case email = "email"
}
}
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)
}
}
}
}
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)
This repo includes an iOS example, which is attached to Example.xcodeproj
link |
Stars: 0 |
Last commit: 1 week ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics