Swift輕量級HTTP客戶端庫:Just

pjp 9年前發布 | 12K 次閱讀 Just Apple Swift開發

Just 是一個受 python-requests 啟發的Swift輕量級 HTTP 客戶端庫。

Just 可以讓你毫不費力地完成如下工作:

  • URL 查詢

  • 定制報文(headers)

  • 構建 JSON HTTP body

  • 重定向控制

  • 多文件上傳

  • Basic/Digest 認證

  • Cookies

  • 同步/異步請求

  • 用戶友好的調用結果

用法:

使用 Just 發起一個請求的例子:

//  talk to registration end point
let r = Just.post(
    "http://justiceleauge.org/member/register",
    data: ["username": "barryallen", "password":"ReverseF1ashSucks"],
    files: ["profile_photo": .URL(fileURLWithPath:"flash.jpeg", nil)]
)

if (r.ok) { /* success! */ }

下面是使用異步方法完成上面工作的例子:

//  talk to registration end point
Just.post(
    "http://justiceleauge.org/member/register",
    data: ["username": "barryallen", "password":"ReverseF1ashSucks"],
    files: ["profile_photo": .URL(fileURLWithPath:"flash.jpeg", nil)]
) { (r)
    if (r.ok) { /* success! */ }
}

你可以在這個 Playground 中了解更多 Just 庫的使用方法。

安裝:

  • 源代碼文件:只須將唯一的一個源文件拖入 playground 或者直接拖入到你的代碼中

  • Git 子模塊:將改 repo 作為 Git 子模塊添加到你的 git 項目中,然后拖動 Just.xcodeproj 到你的 Xcode 項目中以便你可以創建一個對 Just.framework 的依賴并且進行鏈接

  • 動態框架:因為 Just 是一個針對 OS X 和 iOS 的動態框架,所以 Carthage 就可以安裝它。

作者郵箱:daniel@duan.org

項目主頁:http://www.baiduhome.net/lib/view/home/1431306627966

 本文由用戶 pjp 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!