Git Product home page Git Product logo

xhttp's Introduction

XHttp

Swift License Platform

XHttp is an elegant and tiny network framework base on URLSession and Promises.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

PromisesSwift

Installation

XHttp is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'XHttp'

Feature

  • Set base url

  • Set headers

  • Set timeout

  • Set query by String or Dictionary

  • Set http body

  • Chainable request

  • Convenience http method

  • Validate url

  • Retry requset

  • Cancel request

  • Download

Usage

You can create your network manager by inherit XHttp, config your base url, headers and timeout, etc.

import Foundation
import XHttp

let http = MyHttp.shared

class MyHttp: XHttp {

    static let shared: MyHttp = {
        let myConfig = XHttpConfig()
        myConfig.baseURL = "https://httpbin.org"
        myConfig.headers = ["token": "xxx"]
        let instance = MyHttp()
        instance.config = myConfig
        return instance
    }()
    
    private override init() {
        super.init()
    }
}

And you can make requuset like this:

import Foundation
import Promises
import XHttp

func testGet(query: [String: Any]? = nil) -> Promise<XHttpRespone> {
    return http.request("/get", query: query)
}

func testPost(query: [String: Any]? = nil, data: [String: Any]?) -> Promise<XHttpRespone> {
    return http.request("/post", method: .post, data: data, query: query)
}

func testPut() -> Promise<XHttpRespone> {
    return http.request("/put", method: .put)
}

Finally, you can use like this:

import UIKit
import XHttp

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
         post()
    }

    func get() {
        testGet(query: ["nickname": "Bob"]).then { res in
            print("jsonData:\n", res.jsonString)
        }.catch { err in
            print(err)
        }
    }
    
    func post() {
        testPost(query: ["age": 3, "hair": "black"], data: ["nickname": "Bob"]).then { res in
            print("jsonData:\n", res.jsonString)
        }.catch { err in
            print(err)
        }
    }

    func put() {
        testPut().then { res in
            print("jsonData:\n", res.jsonString)
        }.catch { err in
            print(err)
        }
    }
}

With Promises, you can combine request with all:

import UIKit
import XHttp
import Promises

class ViewController: UIViewController {    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        let get = testGet()
        let post = testPost(data: [:])
        all(get, post).then { res1, res2 in
            print(res1.jsonString)
            print(res2.jsonString)
        }
    }
}

Author

xurong, [email protected]

License

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

xhttp's People

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.