A lightweight Web API for Apple devices, written in Swift 5.x and using Structured Concurrency. It builds on top of HTTPTypes library by Apple.
Add the following dependency clause to your Package.swift:
dependencies: [
.package(url: "https://github.com/wmalloc/HTTpRequestable.git", from: "0.6.0")
]
Features | |
---|---|
HTTPRequstable |
Define your request |
URLRequestable |
Build your URLRequest to make a call |
URLTransferable |
To create your API client |
URLAsyncTransferable |
Concurrency version of the API. |
To defineing a request:
struct StoryList: HTTPRequstable {
typealias ResultType = [Int]
let authority: String = "hacker-news.firebaseio.com"
let method: HTTPMethod = .get
let path: String
let headerFields: HTTPFields = .init([.accept(.json)])
let queryItems: Array<URLQueryItem>? = [URLQueryItem(name: "print", value: "pretty")]
init(storyType: String) throws {
guard !storyType.isEmpty else {
throw URLError(.badURL)
}
self.path = "/v0/" + storyType
}
}
class HackerNewsAPI: HTTPTransferable {
let session: URLSession
required init(session: URLSession = .shared) {
self.session = session
}
func storyList(type: String) async throws -> StoryList.ResultType {
let request = try StoryList(storyType: type)
return try await data(for: request, delegate: nil)
}
}
Then you can create an instantiate your API object to make calls
var api = HackerNewsAPI()
let topStories = try await api.storyList(type: "topstories.json")