All Projects → futuredapp → FTAPIKit

futuredapp / FTAPIKit

Licence: MIT license
Declarative and generic REST API framework using Codable.

Programming Languages

swift
15916 projects
ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to FTAPIKit

ObservableComputations
Cross-platform .NET library for computations whose arguments and results are objects that implement INotifyPropertyChanged and INotifyCollectionChanged (ObservableCollection) interfaces.
Stars: ✭ 94 (+422.22%)
Mutual labels:  functional, declarative
swift-declarative-configuration
Declarative configuration for your objects
Stars: ✭ 46 (+155.56%)
Mutual labels:  functional, declarative
kdecole-api
Unofficial Node.js API client of Kdecole (Skolengo EMS)
Stars: ✭ 31 (+72.22%)
Mutual labels:  api-client
specs
Kontalk specifications and documentation
Stars: ✭ 20 (+11.11%)
Mutual labels:  protocol
net-protocol
golang模拟内核协议栈 实现链路层、网络层、传输层、应用层 用户态协议栈 ,基于虚拟网卡TUN/TAP
Stars: ✭ 129 (+616.67%)
Mutual labels:  protocol
latex-protocol
LaTeX template for documents and laboratory documentation 📚
Stars: ✭ 14 (-22.22%)
Mutual labels:  protocol
Motoro
Smart contracts for decentralized rentals of vehicles.
Stars: ✭ 96 (+433.33%)
Mutual labels:  protocol
hacker-rank
Functional Path do hacker Rank
Stars: ✭ 48 (+166.67%)
Mutual labels:  functional
diepindepth
Collection of protocol, memory, and other information for the browser game diepio
Stars: ✭ 39 (+116.67%)
Mutual labels:  protocol
redash-api-client
Redash API Client written in Python
Stars: ✭ 36 (+100%)
Mutual labels:  api-client
go-typetalk
go-typetalk is a GO client library for accessing the Typetalk API.
Stars: ✭ 19 (+5.56%)
Mutual labels:  api-client
tenjin
📝 A template engine.
Stars: ✭ 15 (-16.67%)
Mutual labels:  declarative
onebot
OneBot:统一的聊天机器人应用接口标准
Stars: ✭ 1,113 (+6083.33%)
Mutual labels:  protocol
echovr api docs
Unofficial documentation for Echo VR's HTTP API
Stars: ✭ 19 (+5.56%)
Mutual labels:  api-client
jellyfin-apiclient-python
Python API Client for Jellyfin
Stars: ✭ 30 (+66.67%)
Mutual labels:  api-client
airtable
Airtable API Client for Go
Stars: ✭ 25 (+38.89%)
Mutual labels:  api-client
prune
A tree library for Java 8 with functional sensibilities.
Stars: ✭ 22 (+22.22%)
Mutual labels:  functional
morphmorph
😱 Isomorphic transformations. Map, transform, filter, and morph your objects
Stars: ✭ 26 (+44.44%)
Mutual labels:  functional
tiktok-scraper-php
Tiktok (Musically) PHP scraper
Stars: ✭ 65 (+261.11%)
Mutual labels:  api-client
WXKDarkSky
A pure-Swift Codable layer over the Dark Sky API.
Stars: ✭ 21 (+16.67%)
Mutual labels:  codable

FTAPIKit logo

FTAPIKit

Cocoapods Cocoapods platforms License

macOS 11 macOS 10.15 Ubuntu

Declarative and generic REST API framework using Codable. With standard implementation using URLSesssion and JSON encoder/decoder. Easily extensible for your asynchronous framework or networking stack.

Installation

When using Swift package manager install using Xcode 11+ or add following line to your dependencies:

.package(url: "https://github.com/futuredapp/FTAPIKit.git", from: "1.5.0")

When using CocoaPods add following line to your Podfile:

pod 'FTAPIKit', '~> 1.5'

Features

The main feature of this library is to provide documentation-like API for defining web services. This is achieved using declarative and protocol-oriented programming in Swift.

The framework provides two core protocols reflecting the physical infrastructure:

  • Server protocol defining single web service.
  • Endpoint protocol defining access points for resources.

Combining instances of type conforming to Server and Endpoint we can build request. URLServer has convenience method for calling endpoints using URLSession. If some advanced features are required then we recommend implementing API client. This client should encapsulate logic which is not provided by this framework (like signing authorized endpoints or conforming to URLSessionDelegate).

Architecture

This package contains predefined Endpoint protocols. Use cases like multipart upload, automatic encoding/decoding are separated in various protocols for convenience.

  • Endpoint protocol has empty body. Typically used in GET endpoints.
  • DataEndpoint sends provided data in body.
  • UploadEndpoint uploads file using InputStream.
  • MultipartEndpoint combines body parts into InputStream and sends them to server. Body parts are represented by MultipartBodyPart struct and provided to the endpoint in an array.
  • RequestEndpoint has encodable request which is encoded using encoding of the Server instance.

Endpoint types

Usage

Defining web service (server)

Firstly we need to define our server. Structs are preferred but not required:

struct HTTPBinServer: URLServer {
    let baseUri = URL(string: "http://httpbin.org/")!
    let urlSession = URLSession(configuration: .default)
}

If we want to use custom formatting we just need to add our encoding/decoding configuration:

struct HTTPBinServer: URLServer {
    ...

    let decoding: Decoding = JSONDecoding { decoder in
        decoder.keyDecodingStrategy = .convertFromSnakeCase
    }
    let encoding: Encoding = JSONEncoding { encoder in
        encoder.keyEncodingStrategy = .convertToSnakeCase
    }
}

If we need to create specific request, add some headers, usually to provide authorization we can override default request building mechanism.

struct HTTPBinServer: URLServer {
    ...
    func buildRequest(endpoint: Endpoint) throws -> URLRequest {
        var request = try buildStandardRequest(endpoint: endpoint)
        request.addValue("MyApp/1.0.0", forHTTPHeaderField: "User-Agent")
        return request
    }
}

Defining endpoints

Most basic GET endpoint can be implemented using Endpoint protocol, all default propertires are inferred.

struct GetEndpoint: Endpoint {
    let path = "get"
}

Let's take more complicated example like updating some model. We need to supply encodable request and decodable response.

struct UpdateUserEndpoint: RequestResponseEndpoint {
    typealias Response = User

    let request: User
    let path = "user"
}

Executing the request

When we have server and enpoint defined we can call the web service:

let server = HTTPBinServer()
let endpoint = UpdateUserEndpoint(request: user)
server.call(response: endpoint) { result in
    switch result {
    case .success(let updatedUser):
        ...
    case .failure(let error):
        ...
    }
}

Contributors

Current maintainer and main contributor is Matěj Kašpar Jirásek, [email protected].

We want to thank other contributors, namely:

License

FTAPIKit is available under the MIT license. See the LICENSE file for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].