HttpSession 1.10.0

HttpSession 1.10.0

Maintained by Keisuke Yamagishi.



  • By
  • keisuke

HttpSession

Build status

build

Overview

TCP / IP based HTTP communication can be simplified and Twitter OAuth

API Server: https://sevens-api.herokuapp.com/

Recommended.

Codable SwiftyJSON and SWXMLHash is recommended.

Codable

SwiftyJSON

drmohundro/SWXMLHash

Twitter API callback fix

https://developer.twitter.com/en/docs/basics/callback_url.html

Installation

Cocoapods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate GMSDirection into your Xcode project using CocoaPods, specify it in your Podfile:

vi ./Podfile 

If you do not have the google map SDK for iOS

target 'Target Name' do
  use_frameworks!
  pod 'HttpSession'
end

Then, run the following command:

$ pod setup
$ pod install

Use it

Via SSH: For those who plan on regularly making direct commits, cloning over SSH may provide a better experience (which requires uploading SSH keys to GitHub):

$ git clone [email protected]:keisukeYamagishi/HttpSession.git

Via https: For those checking out sources as read-only, HTTPS works best:

$ git clone https://github.com/keisukeYamagishi/HttpSession.git

Sample code

GET http method

Http.request(url: "https://sevens-api.herokuapp.com/getApi.json", method: .get)
  .session(completion: { (data, responce, error) in
    self.detail(data: data!)
})

POST http method

let param = ["http_post":"Http Request POST 😄"]
            
Http.request(url: "https://sevens-api.herokuapp.com/postApi.json",method: .post)
  .session(param: param,
  completion: { (data, responce, error) in {
  self.detail(data: data!, param: param.hashString())
})

Download http method

Http.request(url: "https://shichimitoucarashi.com/mp4/file1.mp4", method: .get)
                .download(progress: { (written, total, expectedToWrite) in
                    let progress = Float(total) / Float(expectedToWrite)
                    print(String(format: "%.2f", progress * 100) + "%")                    
            }, download: { (location) in
                print ("location: \(String(describing: location))")
            }, completionHandler: { (data, responce, error) in
                self.detail(data: data!)
            })

Like Moya

enum DemoApi {
    case zen
    case post(param:Tapul)
    case download
}

extension DemoApi:ApiProtocol {
    var domain: String{
        switch self {
        case .zen, .post:
            return "https://sevens-api.herokuapp.com/"
        case .download:
            return "https://shichimitoucarashi.com"
        }
    }
    
    var endPoint: String {
        switch self {
        case .zen:
            return "getApi.json"
        case .post:
            return "postApi.json"
        case .download:
            return "mp4/Designing_For_iPad_Pro_ad_hd.mp4"
        }
    }
    
    var method: Http.method {
        switch self {
        case .zen:
            return .get
        case .post:
            return .post
        case .download:
            return .get
        }
    }
    
    var header: [String : String]? {
        return [:]
    }
    
    var params: [String : String] {
        switch self {
        case .zen:
            return [:]
        case .post(let val):
            return [val.value.0:val.value.1]
        case .download:
            return [:]
        }
    }
    
    var isCookie: Bool {
        return false
    }
    
    var basicAuth: [String : String]? {
        return nil
    }
}
let provider:ApiProvider = ApiProvider<DemoApi>()

provider.send(api: .zen) { (data, responce, error) in
    self.detail(data: data!)
}

provider.send(api: .post(param: (key:"http_post",value:"Http Request POST 😄"))) { (data, responce, error) in
    print (String(data: data!, encoding: .utf8))
}