Latest 0.1.6
Homepage https://github.com/nayanda1/iOSEatr
License Apache
Platforms ios 8.0
Dependencies HandyJSON
Authors

iOSEatr

RESTful web service consumer for iOS (Swift) with builder


Changelog

for changelog check here


Features

  • [x] Builder pattern
  • [x] Auto encode param and form url encoded body
  • [x] Support synchronous or asynchronous operation
  • [x] Support progress observer
  • [x] Support HandyJSON object https://github.com/alibaba/HandyJSON

    Requirements

  • Swift 4.2 or higher

Installation

CocoaPods

put this in your Podfile

pod 'Eatr', '~> 0.1.6'

Manually

  1. Clone this repository.
  2. Added to your project.
  3. Congratulation!

Usage Example

Synchronous

Build the object using HttpRequestBuilder and then execute

//HttpGet
let getResponse : Response? = EatrRequestBuilder.httpGet.set(url : "http://your.url.here")
    .addHeader(withKey : "SOME-HEADER", andValue : "header_value")
    .addParam(withKey : "param_key", andValue : "param_value")
    .awaitExecute()

if let response : EatrResponse = getResponse {
    let rawResponse : URLResponse? = response.rawResponse
    let isSuccess : Bool = response.isSuccess
    let isError : Bool = response.isError
    let statusCode : Int? = response.statusCode
    let body : Data? = response.rawBody
    let strBody : String? = response.bodyAsString
    let jsonBody : [String : Any?] = response.bodyAsJson
    let arrJsonBody : [Any?] = response.bodyAsJsonArray
    let parsedBody : YourHandyJSONObj = response.parsedBody()
    let parsedArray : [YourHandyJSONObj?] = response.parsedArrayBody
}

//HttpPost with Json body
let postJsonResponse : Response? = EatrRequestBuilder.httpPost.set(url : "http://your.url.here")
    .addHeader(withKey : "SOME-HEADER", andValue : "header_value")
    .addParam(withKey : "param_key", andValue : "param_value")
    .set(jsonBody : jsonDictionary).awaitExecute()

You can use raw body, form data, string, HandyJSON Object or any JSON Object for request body

Simple Asynchronous

Everything is same like synchronous, but you need to pass consumer function into the execute method
Remember, response inside closure will be null if reaching timeout

//Basic
EatrRequestBuilder.httpGet.set(url : "http://your.url.here")
    .addHeader(withKey : "SOME-HEADER", andValue : "header_value")
    .addParam(withKey : "param_key", andValue : "param_value")
    .asyncExecute(onFinished : { response : Response? in
        //YOUR CODE HERE
        // WILL BE EXECUTE AFTER REQUEST IS FINISHED
    })

Advanced Asynchronous/Synchronous

Same like any async http request, but you can set your consumer separately. you can set 5 closure consumer:

  • onProgress which will run for every progress, it will give the progress in float start from 0.0f to 1.0f
    Because this method will called periodically, its better if you’re not put object creation inside this method
  • onBeforeSending which will run before sending
  • onResponded which will ONLY run when you get response
  • onTimeout which will ONLY run when you get no response after timeout
  • onException which will ONLY run when you get unhandled exception
  • onFinished which will run after all request is complete

You don’t need to set all of the consumer. just the one you need.

Remember, response inside closure will be null if reaching timeout

//with onFinished closure
EatrRequestBuilder.httpGet.set(url : "http://your.url.here")
    .addHeader(withKey : "SOME-HEADER", andValue : "header_value")
    .addParam(withKey : "param_key", andValue : "param_value")
    .set(onTimeout : {
        //YOUR CODE HERE
    })
    .set(onBeforeSending : { (session : URLSession) -> URLSession in
        //YOUR CODE HERE
    })
    .set(onError : { error : Error in
        //YOUR CODE HERE
    })
    .set(onProgress : { progress : Float in
        //YOUR CODE HERE
    })
    .set(onResponded : { response : Response in
        //YOUR CODE HERE
    })
    .asyncExecute()

//you can even do it synchronously and all your closure will be executed synchronously
let response : Response? = EatrRequestBuilder.httpGet.set(url : "http://your.url.here")
    .addHeader(withKey : "SOME-HEADER", andValue : "header_value")
    .addParam(withKey : "param_key", andValue : "param_value")
    .set(onTimeout : {
        //YOUR CODE HERE
    })
    .set(onBeforeSending : { (session : URLSession) -> URLSession in
        //YOUR CODE HERE
    })
    .set(onError : { error : Error in
        //YOUR CODE HERE
    })
    .set(onProgress : { progress : Float in
        //YOUR CODE HERE
    })
    .set(onResponded : { response : Response in
        //YOUR CODE HERE
    })
    .awaitExecute()

Using Delegate

If you prefer using delegate, there is delegate protocol for you to use which is EatrDelegate with available method :

  • eatrOnBeforeSending(_ sessionToSend : URLSession) -> URLSession
  • eatrOnTimeout()
  • eatrOnError(_ error: Error)
  • eatrOnProgress(_ progress: Float)
  • eatrOnResponded(_ response: EatrResponse)
  • eatrOnFinished()

All method are optional, use the one you need

EatrRequestBuilder.httpGet.set(url : "http://your.url.here")
    .addHeader(withKey : "SOME-HEADER", andValue : "header_value")
    .addParam(withKey : "param_key", andValue : "param_value")
    .set(delegate : self)
    .asyncExecute()

Contribute

We would love you for the contribution to iOSEatr, just contact me to [email protected] or just pull request

Latest podspec

{
    "name": "Eatr",
    "version": "0.1.6",
    "summary": "RESTful web service consumer for iOS (Swift) with builder.",
    "description": "Featuresn- Builder patternn- Auto encode param and form url encoded bodyn- Support synchronous or asynchronous operationn- Support progress observern- Support HandyJSON objectnnRequirementsnn- Swift 4.2 or highernnUsage Examplenn- SynchronousnBuild the object using HttpRequestBuilder and then executennCode :n//HttpGetnlet getResponse : Response? = EatrRequestBuilder.httpGet.set(url : "http://your.url.here")n.addHeader(withKey : "SOME-HEADER", andValue : "header_value")n.addParam(withKey : "param_key", andValue : "param_value")n.awaitExecute()nnif let response : EatrResponse = getResponse {n    let rawResponse : URLResponse? = response.rawResponsen    let isSuccess : Bool = response.isSuccessn    let isError : Bool = response.isErrorn    let statusCode : Int? = response.statusCoden    let body : Data? = response.rawBodyn    let strBody : String? = response.bodyAsStringn    let jsonBody : [String : Any?] = response.bodyAsJsonn    let arrJsonBody : [Any?] = response.bodyAsJsonArrayn    let parsedBody : YourHandyJSONObj = response.parsedBody()n    let parsedArray : [YourHandyJSONObj?] = response.parsedArrayBodyn}nn//HttpPost with Json bodynlet postJsonResponse : Response? = EatrRequestBuilder.httpPost.set(url : "http://your.url.here")n.addHeader(withKey : "SOME-HEADER", andValue : "header_value")n.addParam(withKey : "param_key", andValue : "param_value")n.set(jsonBody : jsonDictionary).awaitExecute()nnnnYou can use raw body, form data, string, HandyJSON Object or any JSON Object for request bodynn- Simple AsynchronousnEverything is same like synchronous, but you need to pass consumer function into the execute methodnRemember, response inside closure will be null if reaching timeoutnnCode:n//BasicnEatrRequestBuilder.httpGet.set(url : "http://your.url.here")n   .addHeader(withKey : "SOME-HEADER", andValue : "header_value")n   .addParam(withKey : "param_key", andValue : "param_value")n   .asyncExecute(onFinished : { response : Response? inn      //YOUR CODE HEREn      // WILL BE EXECUTE AFTER REQUEST IS FINISHEDn  })nnn- Advanced Asynchronous/Synchronousn  Same like any async http request, but you can set your consumer separately. you can set 5 closure consumer:n  - onProgress which will run for every progress, it will give the progress in float start from 0.0f to 1.0fn    Because this method will called periodically, its better if you're not put object creation inside this methodn  - onBeforeSending which will run before sendingn  - onResponded which will **ONLY** run when you get responsen  - onTimeout which will **ONLY** run when you get no response after timeoutn  - onException which will **ONLY** run when you get unhandled exceptionn  - onFinished which will run after all request is completen              n  You don't need to set all of the consumer. just the one you need.n              n  Remember, response inside closure will be null if reaching timeoutn              n  Code:n  //with onFinished closuren  EatrRequestBuilder.httpGet.set(url : "http://your.url.here")n      .addHeader(withKey : "SOME-HEADER", andValue : "header_value")n      .addParam(withKey : "param_key", andValue : "param_value")n      .set(onTimeout : {n          //YOUR CODE HEREn      })n      .set(onBeforeSending : { (session : URLSession) -> URLSession inn          //YOUR CODE HEREn      })n      .set(onError : { error : Error inn          //YOUR CODE HEREn      })n      .set(onProgress : { progress : Float inn          //YOUR CODE HEREn      })n      .set(onResponded : { response : Response inn          //YOUR CODE HEREn      })n      .asyncExecute()n                                       n  //you can even do it synchronously and all your closure will be executed synchronouslyn  let response : Response? = EatrRequestBuilder.httpGet.set(url : "http://your.url.here")n      .addHeader(withKey : "SOME-HEADER", andValue : "header_value")n      .addParam(withKey : "param_key", andValue : "param_value")n      .set(onTimeout : {n          //YOUR CODE HEREn      })n      .set(onBeforeSending : { (session : URLSession) -> URLSession inn          //YOUR CODE HEREn      })n      .set(onError : { error : Error inn          //YOUR CODE HEREn      })n      .set(onProgress : { progress : Float inn          //YOUR CODE HEREn      })n      .set(onResponded : { response : Response inn          //YOUR CODE HEREn      })n      .awaitExecute()n                                                                    n- Using Delegaten  If you prefer using delegate, there is delegate protocol for you to use which is EatrDelegate with available method :n  - eatrOnBeforeSending(_ sessionToSend : URLSession) -> URLSessionn  - eatrOnTimeout()n  - eatrOnError(_ error: Error)n  - eatrOnProgress(_ progress: Float)n  - eatrOnResponded(_ response: EatrResponse)n  - eatrOnFinished()n                                                                    n  All method are optional, use the one you needn                                                                    n  Code:n  EatrRequestBuilder.httpGet.set(url : "http://your.url.here")n      .addHeader(withKey : "SOME-HEADER", andValue : "header_value")n      .addParam(withKey : "param_key", andValue : "param_value")n      .set(delegate : self)n      .asyncExecute()",
    "homepage": "https://github.com/nayanda1/iOSEatr",
    "license": {
        "type": "Apache",
        "file": "LICENSE"
    },
    "authors": {
        "nayanda1": "[email protected]"
    },
    "source": {
        "git": "https://github.com/nayanda1/iOSEatr.git",
        "tag": "0.1.6"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "Eatr/Classes/**/*",
    "swift_version": "4.2",
    "dependencies": {
        "HandyJSON": [
            "~> 4.2.0"
        ]
    }
}

Pin It on Pinterest

Share This