๐ค Bird ฮฑ
Bird is currently in alpha stage and should therefore not yet be used in a production environment.
Bird is a lightweight HTTP networking library written in Swift.
It is based on Apples new Reactive Framework Combine and focused on maintain- and extendability.
How to
๐ค
Step 0: Install If the first version is ready,
Bird will be available via SPM.
dependencies: [
.package(url: "https://github.com/SebastianBoldt/Bird", from: "0.0.1"),
]
โ๏ธ
Step 1: Create a Request The first thing you need to create is an Object that conforms to the RequestDefinition
-Protocol.
This can be a struct, enum or class. It will provide all relevant values for making an actual HTTP-request.
In this Example we create a RequestDefinition
for an Endpoint of the PokeAPI
that returns a Pokemon by its Pokdex-Number:
struct GetPokemon: RequestDefinition {
let pokedexNumber: Int
init(pokedexNumber: Int) {
self.pokedexNumber = pokedexNumber
}
}
extension GetPokemon {
var method: HTTPMethod {
return .get
}
var url: String {
return "pokeapi.co"
}
var path: String {
return "/api/v2/pokemon/\(pokedexNumber)"
}
}
๐ฉ
Step 2: Create a Model Create the expected Model that will be returned by the Server.
It has to be Codable
.
struct Pokemon: Codable {
var name: String?
}
๐ฆ
Step 3: Create a RequestService After the Model was declared you need to create an Instance of Type: RequestService
You can do that by calling a static function on the class Bird
.
let requestService = Bird.makeRequestService()
๐ฆ
Step 4: Make a Request Because Bird is using Combine
you will be familiar with the semantics.
Just sink
& go to receive the response you requested.
let defition = GetPokemon(pokedexNumber: 3)
let request = try! requestService.request(defintion, responseType: Pokemon.self)
subscription = request.receive(on: RunLoop.main).sink(receiveCompletion: { completion in
switch completion {
case .finished:
// Handle Finished Subscription
case .failure(let error):
// Handle Failure
}
}, receiveValue: { pokemon in
// Handle Success
})
โก๏ธ
Plugins Plugins can be used e.g. to prepare requests or log responses.
A Plugin
will be notified by 3 different function calls.
- prepare
- willSend
- didReceive
struct ExamplePlugin: Plugin {
func prepare(request: URLRequest, definition: RequestDefinition) -> URLRequest {
// Manipulate the request with Authorization Headers etc.
return request
}
func willSend(request: URLRequest, definition: RequestDefinition) {
// Request will be send in the next Step
}
func didReceive(result: URLSession.DataTaskPublisher.Output, definition: RequestDefinition) {
// Request was successfull and will be published to the subscriber
}
}
๐
Planned Features - Stubbing
- PublisherPlugins
- nested URL Parameters
- Plugin-Suite
Author
Sebastian Boldt, https://www.sebastianboldt.com
I am a mobile software architect and developer specializing in iOS. Passionate about creating awesome user experiences, designing beautiful user interfaces, and writing maintainable, structured, and best-practice orientated code. Continuously trying to improve skills and learn new technologies.
License
Bird is available under the MIT license. See the LICENSE file for more info.