Skip to content
master
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 

README.md

Bird

current version current version twitter handle Swift 5.0 compatible SPM Support license

🐀 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.

current version

License

Bird is available under the MIT license. See the LICENSE file for more info.

About

🐀 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.

Topics

Resources

License

Packages

No packages published

Languages

You can’t perform that action at this time.