Latest 0.0.1
Homepage https://github.com/starmel/RxNetworkApiClient
License MIT
Platforms ios 8.0
Dependencies RxSwift, SwiftyJSON
Authors

Requirements

  • RxSwift
  • SwiftyJson
  • Codable mark is required for all Entities.
  • Need to set default ApiEndpoint.baseEndpoint before sending requests or init new ApiClient with with defaultInstance(host:)

Example


class TodoEntity: Codable{
    var title: String? = nil
}

let apiClient = ApiClientImp.defaultInstance("https://jsonplaceholder.typicode.com")
let request: ApiRequest<TodoEntity> = .request(path: "/todos", method: .GET)

apiClient.execute(request: request)
    .subscribe(onSuccess: { (items: [TodoEntity]) in
        // use result
    })

Features

Interceptors

For request modification before sending and additional response handling.

Builtin Logging interceptor result example:

>>> https://jsonplaceholder.typicode.com/todos [GET]

<<< https://jsonplaceholder.typicode.com/todos
<<< Status code: (200) 👌
<<< Body: Body: [
  {
    "userId": 1,
    "id": 1,
    "title": "delectus aut autem",
    "completed": false
  },
  ...

Example for custom OAuth2 interceptor:

public class AuthInterceptor: Interceptor {

    private let settings: Settings

    public init(_ settings: Settings) {
        self.settings = settings
    }

    public func prepare(request: inout URLRequest) {
        let authHeader = request.allHTTPHeaderFields?["Authorization"]
        if authHeader == nil, let auth = settings.authToken {
            request.addValue("Bearer (auth.token)", forHTTPHeaderField: "Authorization")
        }
    }

    public func handle(response: NetworkResponse) {
        // empty
    }
}

Custom Http Body converters

Mark Entity for sending with JsonBodyConvertible protocol

struct NoteEntity: JsonBodyConvertible {

    var date: Date
    var text: String
    var warning_level: Int
}

let note = NoteEntity(date: Date(), text: "note text", warning_level: Int.max)
let request: ApiRequest<JSON> = .request(path: "todo/add",
                                         method: .POST,
                                         body: note)

apiClient.execute(request: request).subscribe()

Show in output

>>> https://jsonplaceholder.typicode.comtodo/add [POST] Parameters: {"date":558022549.47259104,"text":"note text","warning_level":9223372036854775807}

File uploading


let imgs = ["https://pp.userapi.com/c834302/v834302737/278d7/DoAiIaCb5hY.jpg?ava=1",
                "https://2static1.fjcdn.com/comments/When+i+was+a+small+kid+and+tried+smoking+i+_57ab9cf4d3afaa9daba047a508dc4f2f.jpg"]
            .compactMap { URL(string: $0)! }
            .compactMap { try! Data(contentsOf: $0) }

let files = [UploadFile(name: "file 1.jpg", data: imgs[0], mimeType: "jpg"),
                  UploadFile(name: "file 2.jpg", data: imgs[1], mimeType: "jpg")]

let request: ApiRequest<JSON> = .request("/file/add", method: .POST, files: files)

apiClient.execute(request: request).subscribe()

Full list of available operations for request

static public func request<ResponseType: Codable>(
            path: String? = nil,
            method: HttpMethod,
            endpoint: ApiEndpoint = ApiEndpoint.baseEndpoint,
            headers: [Header]? = nil,
            formData: FormDataFields? = nil,
            files: [UploadFile]? = nil,
            body: BodyConvertible? = nil,
            query: QueryField...) -> ApiRequest<ResponseType> {

Installation

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

pod 'RxNetworkApiClient'

Author

starmel, [email protected]

License

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

Latest podspec

{
    "name": "RxNetworkApiClient",
    "version": "0.0.1",
    "summary": "Simple Rx network library.",
    "description": "See description on github.",
    "homepage": "https://github.com/starmel/RxNetworkApiClient",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "starmel": "[email protected]"
    },
    "source": {
        "git": "https://github.com/starmel/RxNetworkApiClient.git",
        "tag": "0.0.1"
    },
    "platforms": {
        "ios": "8.0"
    },
    "swift_version": "4.0",
    "source_files": "RxNetworkApiClient/Classes/**/*",
    "dependencies": {
        "RxSwift": [],
        "SwiftyJSON": []
    }
}

Pin It on Pinterest

Share This