forked from thoughtbot/Swish
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update documentation for master branch.
- Documentation/Basics.md - Added SwishExamples.playground playground file, with the contents of Basics.md
- Loading branch information
Showing
10 changed files
with
118 additions
and
194 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"id": 4, | ||
"commentText": "Pretty good. Pret-ty pre-ty pre-ty good.", | ||
"username": "LarryDavid" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import PlaygroundSupport | ||
import Swish | ||
import Result | ||
|
||
PlaygroundPage.current.needsIndefiniteExecution = true | ||
|
||
/*: overview | ||
|
||
Let's say we have an endpoint, | ||
`GET https://raw.githubusercontent.com/thoughtbot/Swish/master/Documentation/example.json` | ||
that returns the following JSON: | ||
|
||
{ | ||
"id": 4, | ||
"commentText": "Pretty good. Pret-ty pre-ty pre-ty good.", | ||
"username": "LarryDavid" | ||
} | ||
|
||
We will model the data with the following struct, | ||
and implement the `Codable` protocol so we can | ||
tell it how to turn from JSON into a Swift object. | ||
*/ | ||
|
||
struct Comment: Codable { | ||
let id: Int | ||
let body: String | ||
let username: String | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case id | ||
case body = "commentText" | ||
case username | ||
} | ||
} | ||
|
||
/*: | ||
We can model the request by defining a struct that | ||
implements Swish's `Request` protocol | ||
*/ | ||
|
||
struct CommentRequest: Request { | ||
typealias ResponseObject = Comment | ||
|
||
func build() -> URLRequest { | ||
let url = URL(string: "https://raw.githubusercontent.com/thoughtbot/Swish/master/Documentation/example.json")! | ||
return URLRequest(url: url) | ||
} | ||
} | ||
|
||
/*: | ||
We can then use the Swish's default APIClient to make the request: | ||
*/ | ||
|
||
APIClient().perform(CommentRequest()) { (response: Result<Comment, SwishError>) in | ||
switch response { | ||
case let .success(comment): | ||
print("Here's the comment: \(comment)") | ||
case let .failure(error): | ||
print("Oh no, an error: \(error)") | ||
} | ||
} | ||
|
||
/*: | ||
And that's it! | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<playground version='5.0' target-platform='ios' display-mode='rendered'> | ||
<timeline fileName='timeline.xctimeline'/> | ||
</playground> |
7 changes: 7 additions & 0 deletions
7
SwishExamples.playground/playground.xcworkspace/contents.xcworkspacedata
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.