Skip to content

Commit 8e15346

Browse files
authored
Merge pull request #26 from hamtiko/master
Adding validation to response
2 parents 2a2fd96 + 192c104 commit 8e15346

File tree

6 files changed

+34
-15
lines changed

6 files changed

+34
-15
lines changed

Example/Podfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
platform :ios, '9.3'
1+
platform :ios, '10.0'
22

33
use_frameworks!
44

Example/Podfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ PODS:
1818
- RxSwift (~> 5)
1919
- RxRelay (5.0.0):
2020
- RxSwift (~> 5)
21-
- RxRestClient (1.2.0):
21+
- RxRestClient (1.2.1):
2222
- Alamofire (~> 4)
2323
- RxAlamofire (~> 5)
2424
- RxCocoa (~> 5)
@@ -65,10 +65,10 @@ SPEC CHECKSUMS:
6565
RxCocoa: fcf32050ac00d801f34a7f71d5e8e7f23026dcd8
6666
RxOptional: 9904e2219d59260c3c171273d475b2126de187e8
6767
RxRelay: 4f7409406a51a55cd88483f21ed898c234d60f18
68-
RxRestClient: 8bffa40d201a99a25e6537921bed2c596a7cc908
68+
RxRestClient: 4bf25b1d2355c6f6815d0d71f2f6e32e3d34f0d9
6969
RxSwift: 8b0671caa829a763bbce7271095859121cbd895f
7070
SDWebImage: 3f3f0c02f09798048c47a5ed0a13f17b063572d8
7171

72-
PODFILE CHECKSUM: e36ea5b45e7317f482ff7b8fb57fc3a886f1a890
72+
PODFILE CHECKSUM: c8e4a5269ca34cd9b67088527c787471660ff793
7373

7474
COCOAPODS: 1.7.0

Example/RxRestClient.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@
562562
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
563563
GCC_WARN_UNUSED_FUNCTION = YES;
564564
GCC_WARN_UNUSED_VARIABLE = YES;
565-
IPHONEOS_DEPLOYMENT_TARGET = 9.3;
565+
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
566566
MTL_ENABLE_DEBUG_INFO = YES;
567567
ONLY_ACTIVE_ARCH = YES;
568568
SDKROOT = iphoneos;
@@ -611,7 +611,7 @@
611611
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
612612
GCC_WARN_UNUSED_FUNCTION = YES;
613613
GCC_WARN_UNUSED_VARIABLE = YES;
614-
IPHONEOS_DEPLOYMENT_TARGET = 9.3;
614+
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
615615
MTL_ENABLE_DEBUG_INFO = NO;
616616
SDKROOT = iphoneos;
617617
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";

RxRestClient.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'RxRestClient'
3-
s.version = '1.2.0'
3+
s.version = '1.2.1'
44
s.summary = 'Simple REST Client based on RxSwift and Alamofire.'
55
s.swift_version = '5.0'
66

@@ -14,7 +14,7 @@ Pod::Spec.new do |s|
1414
s.author = { 'Tigran Hambardzumyan' => 'tigran@stdevmail.com' }
1515
s.source = { :git => 'https://github.com/stdevteam/RxRestClient.git', :tag => s.version.to_s }
1616

17-
s.ios.deployment_target = '9.3'
17+
s.ios.deployment_target = '10.0'
1818

1919
s.source_files = 'RxRestClient/Classes/**/*'
2020

RxRestClient/Classes/Models/BaseResponse.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import Foundation
1616
/// - notFound: When recieve **404** status code from server.
1717
/// - validationProblem: When recieve **422** status code from server.
1818
/// - unexpectedError: When recieve `Internal Sever Error`.
19-
public enum BaseResponse {
19+
public enum BaseResponse: Error {
2020
case serviceOffline
2121

2222
case badRequest(body: Any?)

RxRestClient/Classes/RxRestClient.swift

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,9 @@ open class RxRestClient {
466466
/// - Returns: An observable of a the response state
467467
public func run<T: ResponseState>(_ request: Observable<DataRequest>) -> Observable<T> {
468468
return request
469+
.validate { [unowned self] request, response, data in
470+
return self.validate(request, response, data)
471+
}
469472
.flatMap { [options] request -> Observable<(HTTPURLResponse, T.Body?)> in
470473
options.logger?.log(request)
471474

@@ -476,16 +479,17 @@ open class RxRestClient {
476479
return request.rx.responseData().map { ($0.0, $0.1 as? T.Body) }
477480
}
478481
}
479-
.retry(options.retryCount)
480482
.observeOn(backgroundWorkScheduler)
481483
.map { (httpResponse, body) -> RestResponseStatus in
482-
483-
if let response = RxRestClient.checkBaseResponse(httpResponse, body) {
484-
return .base(state: RxRestClient.checkBaseState(response: response))
485-
} else {
486-
return .custom(response: (httpResponse, body))
484+
return .custom(response: (httpResponse, body))
485+
}
486+
.catchError { e in
487+
if let response = e as? BaseResponse {
488+
return Observable.just(.base(state: RxRestClient.checkBaseState(response: response)))
487489
}
490+
return Observable.error(e)
488491
}
492+
.retry(options.retryCount)
489493
.retryOnBecomesReachable(.base(state: BaseState.offline), reachabilityService: reachabilityService)
490494
.flatMap { response -> Observable<T> in
491495
switch response {
@@ -670,6 +674,21 @@ open class RxRestClient {
670674
)
671675
}
672676

677+
/// Do validation of response
678+
///
679+
/// - Parameters:
680+
/// - request: URL Request
681+
/// - response: URL Response
682+
/// - data: Response body
683+
/// - Returns: ValidationResult
684+
open func validate(_ request: URLRequest?, _ response: HTTPURLResponse, _ data: Data?) -> Request.ValidationResult {
685+
686+
if let baseResponse = RxRestClient.checkBaseResponse(response, data) {
687+
return .failure(baseResponse)
688+
}
689+
return .success
690+
}
691+
673692
// MARK: - Checking base response cases
674693
/// Checking base response cases.
675694
///

0 commit comments

Comments
 (0)