Skip to content

Commit

Permalink
Update Documentation/Advanced.md for Swish 2
Browse files Browse the repository at this point in the history
  • Loading branch information
sharplet committed Dec 28, 2016
1 parent d9ae654 commit 561020e
Showing 1 changed file with 44 additions and 32 deletions.
76 changes: 44 additions & 32 deletions Documentation/Advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,48 @@ with `"yes"` or `"no"` as a response body. We'll have to do a few things:
- Define a custom `ResponseParser` to provide to our `Request` object, because
the default one parses `JSON`.
- Define a custom `Deserializer` to provide to our `APIClient`, because the
default one uses `NSJSONSerialization`.
default one uses `JSONSerialization`.

We'll start with the custom `Deserializer`. It will simply pass along the
`NSData` if it exists:
`Data` if it exists:

```swift
struct DataDeserializer: Deserializer {
func deserialize(data: NSData?) -> Result<AnyObject, SwishError> {
guard let data = data where data.length > 0 else {
return .Success(NSNull())
func deserialize(_ data: Data?) -> Result<Any, SwishError> {
guard let data = data, !data.isEmpty else {
return .success(NSNull())
}

return .Success(data)
return .success(data)
}
}
```

Then, our custom `ResponseParser` will use `String(data:encoding:)` to
transform the incoming `NSData` to a `String` representation:
transform the incoming `Data` to a `String` representation:

```swift
import Swish

enum StringParserError: Error {
case badData(Any)
case invalidUTF8(Data)
case unexpectedResponse(String)
}

struct StringParser: Parser {
typealias Representation = String

static func parse(j: AnyObject) -> Result<String, SwishError> {
guard let data = j as? NSData else {
let error = NSError.error("Bad data!")

return .Failure(.InvalidJSONResponse(error))
static func parse(_ object: Any) -> Result<String, SwishError> {
guard let data = object as? Data else {
return .failure(.parseError(StringParserError.badData(object)))
}

let error = NSError.error("Couldn't convert to string!")
let string = String(data: data, encoding: NSUTF8StringEncoding)
guard let string = String(data: data, encoding: .utf8) else {
return .failure(.parseError(StringParserError.invalidUTF8(data)))
}

return Result(string, failWith: .InvalidJSONResponse(error))
return .success(string)
}
}
```
Expand All @@ -53,21 +60,19 @@ struct AuthorizedRequest: Request {
typealias ResponseObject = Bool
typealias ResponseParser = StringParser

func build() -> NSURLRequest {
let endpoint = NSURL(string: "https://www.example.com/isAuthorized")!

return NSURLRequest(URL: endpoint)
func build() -> URLRequest {
let endpoint = URL(string: "https://www.example.com/isAuthorized")!
return URLRequest(url: endpoint)
}

func parse(j: String) -> Result<Bool, SwishError> {
switch j {
case "yes":
return Result(true)
case "no":
return Result(false)
default:
let error = NSError.error("Unexpected response body!")
return Result(error: .InvalidJSONResponse(error))
func parse(_ string: String) -> Result<Bool, SwishError> {
switch string {
case "yes":
return .success(true)
case "no":
return .success(false)
default:
return .failure(.parseError(StringParserError.unexpectedResponse(string)))
}
}
}
Expand All @@ -80,10 +85,17 @@ let client = APIClient(
requestPerformer: NetworkRequestPerformer(),
deserializer: DataDeserializer()
)
let request = AuthorizedRequest()

let dataTask = client.performRequest(request) { (result: Result<Bool, SwishError>) in
// whatever you need to do
let isAuthorizedRequest = AuthorizedRequest()

client.perform(isAuthorizedRequest) { result in
switch result {
case .success(true):
// authorized
case .success(false):
// not authorized
case let .failure(error):
// handle the error
}
}
```

Expand Down

0 comments on commit 561020e

Please sign in to comment.