Skip to content

Releases: rakutentech/ios-rresultbuilders

v1.2.1

12 Oct 08:06
Compare
Choose a tag to compare
  • Added public modifier for few types
  • Fixed podspec

V1.2.0

06 Sep 04:55
818fa50
Compare
Choose a tag to compare

What's New

  • Added Request Builder
  • Added URL Builder

Request

Making API call in declarative is fairly simple

DSL

Request<Type> {
    URL("https://jsonplaceholder.typicode.com/todo")
}
.onObject { object in
    ...
}
.resume()

Data Request

To get raw data as response

Request<Type> {
    URL("https://jsonplaceholder.typicode.com/todo")
}
.onData { data in
    ...
}
.onError { err in
    ...
}
.resume()

Callback handler

You can attach all possible handlers to Request but they are completely optional execept resume

Request<[Todo]> {
    URL(string: "https://jsonplaceholder.typicode.com/todos")!
}
.onRawResponse { (data, response, error) in
    ...
}
.onData { data in
    ...
}
.onObject { todos in
    ...
}
.onError { err in
    ...
}
.resume()

Request Components

URL

URL can also be build in DSL way

URL {
    Scheme(.https)
    Host("jsonplaceholder.typicode.com")
    Path("todo")            
}
Header

Supports standard HTTP headers

Header.Accept(.json)
Header.Authorization(.basic(username: "username", password: "password"))
Header.CacheControl(.noCache)
Header.ContentLength(16)
Header.ContentType(.json)
Header.Host(jsonplaceholder.typicode.com", port: "8000")
Header.UserAgent("user-agent")
Header.Custom("custom", value: "customvVal")

It also supports building Headers in DSL

Headers {
    Header.Accept(.json)
    Header.Authorization(.basic(username: "test", password: "rest"))
    Header.CacheControl(.noCache)
}
HTTP Body

Custom Encodable Object

Request<Type> {
    RequestBody(sampleTodo)
}
.resume()

Raw Data

Request<Type> {
    RequestBody(data)
}
.resume()
HTTP Method
Method.GET
Method.POST
Method.HEAD
Method.PUT
Method.DELETE
Timeout
Timeout(30) // seconds
Decoding

You can even specify custom decoder

Request<[Todo]> {
    URL(string: "https://jsonplaceholder.typicode.com/todos")!
}
.onObject(using: JSONDecoder()) { todos in
    ...
}
.resume()

Also raw data can be decoded

DataRequest {
    URL(string: "https://jsonplaceholder.typicode.com/todos")!
}
..onData { data in
    data?.decoded()
    // Custom decoder
    // data?.decoded(using: JSONDecoder())
}
.resume()
URLRequest

It also generates raw URLRequest

DataRequest {
    URL(string: "https://jsonplaceholder.typicode.com/todos")!
    Method.GET
    CachePolicy(.reloadIgnoringLocalCacheData)
    Headers {
        Header.Accept(.json)
        Header.Authorization(.basic(username: "test", password: "rest"))
        Header.CacheControl(.noCache)
    }
}.asURLRequest()