Skip to content
Next generation of HTTP client for Swift
Swift Python
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Sources
Tests
utils
.gitignore
.travis.yml
Package.swift
README.md

README.md

Alice

travis

Next generation of HTTP client for Swift.

Still under development...

Modules

In the current plan, Alice consists of the following four parts:

  • Async: A high performance future & promise library.
  • HTTP: An extensible HTTP client library.
  • JSON: An easy to use JSON model library.
  • Layer: A type-safe HTTP abstraction layer library.

These libraries will be split into their own repositories in the official release in the future. Each library can be used separately in your app.

Usage

HTTP(ing)

let client = HTTPClient()
client.use { (req, next) in
    return try next.respond(to: req)
        .catch { e in
            LOG.error("[HTTP]: \(req.method) \(req.url) \(e)")
        }
}

client.use { (req, next) in
    let tag = Date()
    return try next.respond(to: req).map {
        $0.headers.set("\(Date().timeIntervalSince(tag))", for: "Time-Spent")
    }
}

client.use(AuthMiddleware(), when: .path("/users"))

let task = client.get("https://api.alice.com/user")

// task.text // -> Future<String, HTTPError>
// task.json // -> Future<JSON, HTTPError>

task.response
    .map { 
        User.init
    }
    .then {
        updateUI()
    }

Async

func request(_ url: URL) -> Future<HTTPResponse, HTTPError> {
    let p = Promise<HTTPResponse, HTTPError>()
    URLSession.shared.dataTask(with: url) { (data, response, error) in
        if let e = error {
            p.fail(HTTPError.session(e))
            return
        }
        p.succeed(HTTPResponse(response, data))
    }
    return p.future
}

request(imageURL)
    .validate {
        $0.isValid()
    }
    .yield(on: workQ)
    .tryMap {
        try ImageDecoder().decode($0.data)
    }
    .main {
        self.imageView = $0
    }
    .background {
        cache.add($0, for: img)
    }
    .catch {
        Log.error($0)
    }

更新

更多

Alice 还在开发中,我将用连载的方式记录她的开发过程——

内容会有技术方面的:

  • futures & promise 的实现
  • 多线程和网络的应用
  • 性能的取舍
  • 怎么写测试
  • 我理想的网络抽象层
  • 我对接口设计的思考
  • ……

也会有项目相关的:

  • 创建与发布一个 Swift 框架
  • 依赖管理
  • CI 的工作
  • 我怎么看代码风格
  • ……

欢迎关注!😉

注意,这不是教程,而是记录,记录当天所做,分享当天所学。

我非常期待交流,你可以通过评论或 issue 反馈,发表你的评论和想法,让 Alice 能集百家之长,成一家之言。我期望 Alice 能在交流中的写就,在反馈中完成。

如果你是一个经验丰富的大佬,欢迎指教!这是一个尝试,过程中一定会遇到很多问题,我非常需要你的帮助!

如果是你一个开源世界的新手,那么来吧!和我一起学习,这是一个开始参与开源的绝好机会。我们可以一起成长为——巨人!

You can’t perform that action at this time.