Skip to content

Commit

Permalink
Merge pull request Moya#1605 from LucianoPAlmeida/master
Browse files Browse the repository at this point in the history
Adding convenience Range filter.
  • Loading branch information
SD10 authored Mar 27, 2018
2 parents 8744753 + 048f779 commit f29999d
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 10 deletions.
14 changes: 9 additions & 5 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# Next

### Added

- Convenience filter by `Range<Int>` on reponses. [#1605](https://github.com/Moya/Moya/pull/1605) by [@LucianoPAlmeida](https://github.com/LucianoPAlmeida).

# [11.0.1] - 2018-02-26

### Fixed
- Fixed Alamofire validation not being performed on `.uploadMultipart` requests.
[#1591](https://github.com/Moya/Moya/pull/1591) by [@SD10](https://github.com/SD10).
[#1591](https://github.com/Moya/Moya/pull/1591) by [@SD10](https://github.com/SD10).
- Fixed Alamofire validation not being performed on stubbed requests.
[#1593](https://github.com/Moya/Moya/pull/1593) by [@SD10](https://github.com/sd10).
[#1593](https://github.com/Moya/Moya/pull/1593) by [@SD10](https://github.com/sd10).

# [11.0.0] - 2018-02-07
- No changes
Expand All @@ -22,13 +26,13 @@

### Changed
- **Breaking Change** Updated minimum version of `ReactiveSwift` to 3.0.
[#1470](https://github.com/Moya/Moya/pull/1470) by [@larryonoff](https://github.com/larryonoff).
[#1470](https://github.com/Moya/Moya/pull/1470) by [@larryonoff](https://github.com/larryonoff).
- **Breaking Change** Changed the `validate` property of `TargetType` to use new `ValidationType` enum representing valid status codes. [#1505](https://github.com/Moya/Moya/pull/1505) by [@SD10](https://github.com/sd10), [@amaurydavid](https://github.com/amaurydavid).

# [10.0.2] - 2018-01-26
### Fixed
- Fixed a bug where modifying `.uploadMultipart`, `.uploadCompositeMultipart`, `.uploadFile`, `.downloadDestination`, and `.downloadParameters` tasks through an `endpointClosure` has no effect on the final request.
[#1550](https://github.com/Moya/Moya/pull/1550) by [@SD10](https://github.com/sd10), [@sunshinejr](https://github.com/sunshinejr).
[#1550](https://github.com/Moya/Moya/pull/1550) by [@SD10](https://github.com/sd10), [@sunshinejr](https://github.com/sunshinejr).
- Fixed a bug where `URLEncoding.httpBody` wasn't allowed as `bodyEncoding` in `Task.requestCompositeParameters()`. [#1557](https://github.com/Moya/Moya/pull/1557) by [@sunshinejr](https://github.com/sunshinejr).

# [10.0.1] - 2017-11-23
Expand Down Expand Up @@ -172,7 +176,7 @@
- **Breaking Change** Renamed `URL` in `Endpoint` to `url`.
- **Breaking Change** Renamed `StructTarget` to `MultiTarget`.
- Demo project has been updated with new DemoMultiTarget target, new project
structure and more.
structure and more.

### Added
- Readded support for iOS 8 and macOS 10.10.
Expand Down
13 changes: 12 additions & 1 deletion Sources/Moya/Response.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public final class Response: CustomDebugStringConvertible, Equatable {
public extension Response {

/**
Returns the `Response` if the `statusCode` falls within the specified range.
Returns the `Response` if the `statusCode` falls within the specified closed range.

- parameters:
- statusCodes: The range of acceptable status codes.
Expand All @@ -55,6 +55,17 @@ public extension Response {
return self
}

/**
Returns the `Response` if the `statusCode` falls within the specified range.

- parameters:
- statusCodes: The range of acceptable status codes.
- throws: `MoyaError.statusCode` when others are encountered.
*/
public func filter(statusCodes: Range<Int>) throws -> Response {
return try filter(statusCodes: statusCodes.lowerBound...statusCodes.upperBound-1)
}

/**
Returns the `Response` if it has the specified `statusCode`.

Expand Down
10 changes: 10 additions & 0 deletions Sources/ReactiveMoya/SignalProducer+Response.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,28 @@ extension SignalProducerProtocol where Value == Response, Error == MoyaError {
}
}

/// Filters out responses that don't fall within the given range, generating errors when others are encountered.
public func filter(statusCodes: Range<Int>) -> SignalProducer<Value, MoyaError> {
return producer.flatMap(.latest) { response -> SignalProducer<Value, Error> in
return unwrapThrowable { try response.filter(statusCodes: statusCodes) }
}
}

/// Filters out responses that have the specified `statusCode`.
public func filter(statusCode: Int) -> SignalProducer<Value, MoyaError> {
return producer.flatMap(.latest) { response -> SignalProducer<Value, MoyaError> in
return unwrapThrowable { try response.filter(statusCode: statusCode) }
}
}

/// Filters out responses where `statusCode` falls within the range 200 - 299.
public func filterSuccessfulStatusCodes() -> SignalProducer<Value, MoyaError> {
return producer.flatMap(.latest) { response -> SignalProducer<Value, MoyaError> in
return unwrapThrowable { try response.filterSuccessfulStatusCodes() }
}
}

/// Filters out responses where `statusCode` falls within the range 200 - 399
public func filterSuccessfulStatusAndRedirectCodes() -> SignalProducer<Value, MoyaError> {
return producer.flatMap(.latest) { response -> SignalProducer<Value, MoyaError> in
return unwrapThrowable { try response.filterSuccessfulStatusAndRedirectCodes() }
Expand Down
10 changes: 10 additions & 0 deletions Sources/RxMoya/Observable+Response.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,28 @@ extension ObservableType where E == Response {
}
}

/// Filters out responses that don't fall within the given range, generating errors when others are encountered.
public func filter(statusCodes: Range<Int>) -> Observable<E> {
return flatMap { response -> Observable<E> in
return Observable.just(try response.filter(statusCodes: statusCodes))
}
}

/// Filters out responses that has the specified `statusCode`.
public func filter(statusCode: Int) -> Observable<E> {
return flatMap { response -> Observable<E> in
return Observable.just(try response.filter(statusCode: statusCode))
}
}

/// Filters out responses where `statusCode` falls within the range 200 - 299.
public func filterSuccessfulStatusCodes() -> Observable<E> {
return flatMap { response -> Observable<E> in
return Observable.just(try response.filterSuccessfulStatusCodes())
}
}

/// Filters out responses where `statusCode` falls within the range 200 - 399
public func filterSuccessfulStatusAndRedirectCodes() -> Observable<E> {
return flatMap { response -> Observable<E> in
return Observable.just(try response.filterSuccessfulStatusAndRedirectCodes())
Expand Down
12 changes: 11 additions & 1 deletion Sources/RxMoya/Single+Response.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,35 @@ import RxSwift
/// Extension for processing raw NSData generated by network access.
extension PrimitiveSequence where TraitType == SingleTrait, ElementType == Response {

/// Filters out responses that don't fall within the given range, generating errors when others are encountered.
/// Filters out responses that don't fall within the given closed range, generating errors when others are encountered.
public func filter(statusCodes: ClosedRange<Int>) -> Single<ElementType> {
return flatMap { response -> Single<ElementType> in
return Single.just(try response.filter(statusCodes: statusCodes))
}
}

/// Filters out responses that don't fall within the given range, generating errors when others are encountered.
public func filter(statusCodes: Range<Int>) -> Single<ElementType> {
return flatMap { response -> Single<ElementType> in
return Single.just(try response.filter(statusCodes: statusCodes))
}
}

/// Filters out responses that have the specified `statusCode`.
public func filter(statusCode: Int) -> Single<ElementType> {
return flatMap { response -> Single<ElementType> in
return Single.just(try response.filter(statusCode: statusCode))
}
}

/// Filters out responses where `statusCode` falls within the range 200 - 299.
public func filterSuccessfulStatusCodes() -> Single<ElementType> {
return flatMap { response -> Single<ElementType> in
return Single.just(try response.filterSuccessfulStatusCodes())
}
}

/// Filters out responses where `statusCode` falls within the range 200 - 399
public func filterSuccessfulStatusAndRedirectCodes() -> Single<ElementType> {
return flatMap { response -> Single<ElementType> in
return Single.just(try response.filterSuccessfulStatusAndRedirectCodes())
Expand Down
59 changes: 58 additions & 1 deletion Tests/Observable+MoyaSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Nimble
final class ObservableMoyaSpec: QuickSpec {
override func spec() {
describe("status codes filtering") {
it("filters out unrequested status codes") {
it("filters out unrequested status codes closed range upperbound") {
let data = Data()
let observable = Response(statusCode: 10, data: data).asObservable()

Expand All @@ -25,6 +25,63 @@ final class ObservableMoyaSpec: QuickSpec {
expect(errored).to(beTruthy())
}

it("filters out unrequested status codes closed range lowerbound") {
let data = Data()
let observable = Response(statusCode: -1, data: data).asObservable()

var errored = false
_ = observable.filter(statusCodes: 0...9).subscribe { event in
switch event {
case .next(let object):
fail("called on non-correct status code: \(object)")
case .error:
errored = true
default:
break
}
}

expect(errored).to(beTruthy())
}

it("filters out unrequested status codes range upperbound") {
let data = Data()
let observable = Response(statusCode: 10, data: data).asObservable()

var errored = false
_ = observable.filter(statusCodes: 0..<10).subscribe { event in
switch event {
case .next(let object):
fail("called on non-correct status code: \(object)")
case .error:
errored = true
default:
break
}
}

expect(errored).to(beTruthy())
}

it("filters out unrequested status codes range lowerbound") {
let data = Data()
let observable = Response(statusCode: -1, data: data).asObservable()

var errored = false
_ = observable.filter(statusCodes: 0..<10).subscribe { event in
switch event {
case .next(let object):
fail("called on non-correct status code: \(object)")
case .error:
errored = true
default:
break
}
}

expect(errored).to(beTruthy())
}

it("filters out non-successful status codes") {
let data = Data()
let observable = Response(statusCode: 404, data: data).asObservable()
Expand Down
53 changes: 52 additions & 1 deletion Tests/SignalProducer+MoyaSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ private func signalSendingData(_ data: Data, statusCode: Int = 200) -> SignalPro
final class SignalProducerMoyaSpec: QuickSpec {
override func spec() {
describe("status codes filtering") {
it("filters out unrequested status codes") {
it("filters out unrequested status codes closed range upperbound") {
let data = Data()
let signal = signalSendingData(data, statusCode: 10)

Expand All @@ -27,6 +27,57 @@ final class SignalProducerMoyaSpec: QuickSpec {
expect(errored).to(beTruthy())
}

it("filters out unrequested status codes closed range lowerbound") {
let data = Data()
let signal = signalSendingData(data, statusCode: -1)

var errored = false
signal.filter(statusCodes: 0...9).startWithResult { event in
switch event {
case .success(let object):
fail("called on non-correct status code: \(object)")
case .failure:
errored = true
}
}

expect(errored).to(beTruthy())
}

it("filters out unrequested status codes range upperbound") {
let data = Data()
let signal = signalSendingData(data, statusCode: 10)

var errored = false
signal.filter(statusCodes: 0..<10).startWithResult { event in
switch event {
case .success(let object):
fail("called on non-correct status code: \(object)")
case .failure:
errored = true
}
}

expect(errored).to(beTruthy())
}

it("filters out unrequested status codes range lowerbound") {
let data = Data()
let signal = signalSendingData(data, statusCode: -1)

var errored = false
signal.filter(statusCodes: 0..<10).startWithResult { event in
switch event {
case .success(let object):
fail("called on non-correct status code: \(object)")
case .failure:
errored = true
}
}

expect(errored).to(beTruthy())
}

it("filters out non-successful status codes") {
let data = Data()
let signal = signalSendingData(data, statusCode: 404)
Expand Down
53 changes: 52 additions & 1 deletion Tests/Single+MoyaSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Nimble
final class SingleMoyaSpec: QuickSpec {
override func spec() {
describe("status codes filtering") {
it("filters out unrequested status codes") {
it("filters out unrequested status codes closed range upperbound") {
let data = Data()
let single = Response(statusCode: 10, data: data).asSingle()

Expand All @@ -23,6 +23,57 @@ final class SingleMoyaSpec: QuickSpec {
expect(errored).to(beTruthy())
}

it("filters out unrequested status codes closed range lowerbound") {
let data = Data()
let single = Response(statusCode: -1, data: data).asSingle()

var errored = false
_ = single.filter(statusCodes: 0...9).subscribe { event in
switch event {
case .success(let object):
fail("called on non-correct status code: \(object)")
case .error:
errored = true
}
}

expect(errored).to(beTruthy())
}

it("filters out unrequested status codes with range upperbound") {
let data = Data()
let single = Response(statusCode: 10, data: data).asSingle()

var errored = false
_ = single.filter(statusCodes: 0..<10).subscribe { event in
switch event {
case .success(let object):
fail("called on non-correct status code: \(object)")
case .error:
errored = true
}
}

expect(errored).to(beTruthy())
}

it("filters out unrequested status codes with range lowerbound") {
let data = Data()
let single = Response(statusCode: -1, data: data).asSingle()

var errored = false
_ = single.filter(statusCodes: 0..<10).subscribe { event in
switch event {
case .success(let object):
fail("called on non-correct status code: \(object)")
case .error:
errored = true
}
}

expect(errored).to(beTruthy())
}

it("filters out non-successful status codes") {
let data = Data()
let single = Response(statusCode: 404, data: data).asSingle()
Expand Down

0 comments on commit f29999d

Please sign in to comment.