Latest 0.1.4
Homepage https://github.com/stdevteam/RxRestClient
License MIT
Platforms ios 9.3
Dependencies RxSwift, RxCocoa, Alamofire, RxAlamofire
Authors

CI Status
Version
License
Platform

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

  • iOS 9.3+
  • Swift 4.x
  • Xcode 9+

Installation

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

pod 'RxRestClient'

Features

  • Simple way to do requests
  • Simple way to have response state in reactive way
  • Ability to customization
  • Retry on any error
  • Handle network reachability status
  • Retry on become reachable
  • Ability to use absalute and relative urls
  • Logger
  • more coming soon

How to use

First of all you need to create struct of your response state and implement ResponseState protocol.

struct RepositoriesState: ResponseState {

    var state: BaseState?
    var data: [Repository]?

    private init() {
        state = nil
    }

    init(state: BaseState) {
        self.state = state
    }

    init(response: (HTTPURLResponse, String)) {
        if response.0.statusCode == 200 {
            self.data = try? RepositoryResponse(JSONString: response.1).items
        }
    }

    static let empty = RepositoriesState()
}

Than you can do the request to get repositories:

import RxSwift
import RxRestClient

protocol RepositoriesServiceProtocol {
    func get(search: String) -> Observable<RepositoriesState>
}

class RepositoriesService: RepositoriesServiceProtocol {

    private let client = RxRestClient()

    func get(search: String) -> Observable<RepositoriesState> {
        return client.get("https://api.github.com/search/repositories", query: ["q": search])
    }
}

In order to customize client you can use RxRestClientOptions:

var options = RxRestClientOptions.default
options.addHeader(key: "x-apikey", value: "<API_KEY>")
client = RxRestClient(baseUrl: <BASE _URL>), options: options)

Relative vs absalute url

In order to use relative url you need to give <BASE_URL> when initializing client object.

let client = RxRestClient(baseURL: <BASE_URL>)

When calling any request you can provide either String endpoint or absalute URL. If you will String it will be appended to baseURL.

client.get("rest/contacts")

If baseURL is nil than it will try to convert provided String to URL.

In order to use absalute url even when your client has baseURL you can provide URL like this:

if let url = URL(string: "https://api.github.com/search/repositories") {
    client.get(url: url, query: ["q": search])
}

Logger

In order to log curl command of sent request you can use RxRestClientOptions.logger option. For just printing debug description to console you can use builtin DebugRxRestClientLogger logger.

If you prefer to have a custom logger, for example, log requests to the file, you need to implement RxRestClientLogger protocol and pass it as an option.

Author

Tigran Hambardzumyan, [email protected]

Support

Feel free to open issuses with any suggestions, bug reports, feature requests, questions.

Let us know!

We’d be really happy if you sent us links to your projects where you use our component. Just send an email to [email protected] and do let us know if you have any questions or suggestion.

License

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

Latest podspec

{
    "name": "RxRestClient",
    "version": "0.1.4",
    "summary": "Simple REST Client based on RxSwift and Alamofire.",
    "description": "Reactive way to interct with REST API. Send request and get responses easily. Handling basic response cases by default, for example: Not Found, Unauthorized, etc.",
    "homepage": "https://github.com/stdevteam/RxRestClient",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Tigran Hambardzumyan": "[email protected]"
    },
    "source": {
        "git": "https://github.com/stdevteam/RxRestClient.git",
        "tag": "0.1.4"
    },
    "platforms": {
        "ios": "9.3"
    },
    "source_files": "RxRestClient/Classes/**/*",
    "dependencies": {
        "RxSwift": [
            "~> 4.1.2"
        ],
        "RxCocoa": [
            "~> 4.1.2"
        ],
        "Alamofire": [
            "~> 4.6.0"
        ],
        "RxAlamofire": [
            "~> 4.1.0"
        ]
    }
}

Pin It on Pinterest

Share This