Git Product home page Git Product logo

ezhttp's Introduction

EzHTTP

Build Status Version Status Platform

EzHTTP is easy-to-use library for HTTP access for your iOS application.

  • simplest API
  • auto make request (query,form,json)
  • useful response (use as string,json,data)
  • auto indicator show/hide
  • log,stub handler
  • escape App-Transport-Security
  • short code

Requirements

  • iOS 9.0+
  • Xcode 11+

Installation

CocoaPods

To install EzHTTP with CocoaPods, add EzHTTP to the devendencies in your Podfile.

pod 'EzHTTP'

Then, run pod install command in your project.

Swift Package Manager

You can also install EzHTTP using Swift Package Xcode11 later

File->Swift Packages->Add Package Dependency...

https://github.com/asaday/EzHTTP.git

Usage

Begin by importing the EzHTTP.

import EzHTTP

Request

Simple GET request

HTTP.get("https://httpbin.org/get") { print($0.string) }

POST with form

HTTP.request(.POST, "https://httpbin.org/post", params: ["form1": "TEST"]) {}

// auto appended header "Content-Type: application/x-www-form-urlencoded; charset=utf-8"
// or if included HTTP.MultipartFile() , header is "multipart/form-data; boundary=..."

POST with JSON

HTTP.request(.POST, "https://httpbin.org/post", json: ["foo": "bar"]) {}

// auto appended header "Content-Type: application/json"

POST with custom Headers

HTTP.request(.POST, "https://httpbin.org/post",headers: ["Custom-Content":"HAHAHA"])

POST with raw data

// data is String or Data

HTTP.request(.POST, "https://httpbin.org/post", body: data, headers:["Content-Type": "application/atom+xml"]) {}

Other methods

HTTP.request(.DELETE, "https://httpbin.org/delete") {}
  • OPTIONS, GET, HEAD, POST, PUT, PATCH, DELETE, TRACE, CONNECT

Create a request

let req:NSMutableURLRequest = HTTP.createRequest(.GET, "https://httpbin.org/get", params: [:], headers: [:])
HTTP.request(req!){}

sync

let r = HTTP.getSync("https://httpbin.org/get")
print(r.string)

Response body

  • data
  • error
  • response
Retrieve as String
  • $0.string String?
  • $0.stringValue String
JSON
  • $0.jsonObject NSObject? (json decoded)
  • $0.jsonObjectValue NSObject (json decoded)
Data
  • $0.data Data?
  • $0.dataValue Data
Error
  • if let error = $0.error {...}
misc
  • status Int Response status code
  • headers [String: String] Response headers
  • duration
  • request
  • description
  • retriedCount

Request make

let req:NSMutableURLRequest = HTTP.create(.GET,"https://httpbin.org/", params: 	["foo":"bar"],headers: ["Custom-Content":"HAHAHA"])

params

normal
method in param
GET query
POST,PUT application/x-www-form-urlencoded or application/json
POST with file multipart/form-data

change to JSON POST HTTP.shard.postASJSON = true

to use file, add in params HTTP.MultipartFile, auto changed to multipart/form-data

in json mode, data is auto convered to base64

on demand change request params mode

use HTTP.makeParams() or

params = [HTTP.ParamMode.query.rawValue: ["foo":bar"],
		HTTP.ParamMode.form.rawValue: ["aaa":"bbb"]]
  • ParamMode
key description
query in query ?aaa=bbb
form as application/x-www-form-urlencoded
json as application/json
multipartForm as multipart/form-data; boundary=...
path in path https://example.com/{user}
header in header

make URL as

"https://example.com/123"

Log,Stub,Retry

var errorHandler: ((result: Response) -> Void)?
var successHandler: ((result: Response) -> Void)?
var logHandler: ((result: Response) -> Void)?
var stubHandler: ((request: NSURLRequest) -> Response?)?
var retryHandler: ((_ result: Response) -> Bool)?

to use

HTTP.shared.logHandler = { print($0.stringValue) }

or you can use preset handler

HTTP.shared.logHandler = HTTP.defaultLogHandler
HTTP.shared.retryHandler = HTTP.defaultRetryHandler

retry hanlder example

HTTP.shared.retryHandler = { return $0.retriedCount < 3 }
kind desc
error called at error
success called at success
log every called
stub call and get response. if response is nil, do as normal http access
retry judge retry

self signed SSL

If you want to use self signed SSL authentication, make the following settings.

set Allow Arbitrary Loads = YES

and

HTTP.shared.allowSelfSignedSSL = true

ATS escape

ATS(AppTransportSecurity) is enabled(default) and call as http://, request to server as socket connection (not use NSURLSession,NSURLConnection :-)

HTTP.shared.escapeATS = true // default is false

HTTP.get("http://httpbin.org/get") {
	print($0.string)
}

auto checked NSAllowsArbitraryLoads and NSExceptionAllowsInsecureHTTPLoads/NSExceptionDomains

  • protocol HTTP/1.1
  • cookie managed (NSHTTPCookieStorage.sharedHTTPCookieStorage)
  • auto redirect
  • redirect to HTTPS, auto changed to use NSURLSession
  • chunked mode (stream)

ezhttp's People

Contributors

53ningen avatar asaday avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.