https://github.com/benchr267/resty
https://blog.benchr.me/post/pop-network-1/
https://github.com/benchr267/resty
Last synced: 12 months ago
JSON representation
https://blog.benchr.me/post/pop-network-1/
- Host: GitHub
- URL: https://github.com/benchr267/resty
- Owner: BenchR267
- License: mit
- Created: 2017-02-04T13:25:43.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-02-15T18:09:11.000Z (over 9 years ago)
- Last Synced: 2025-03-29T04:31:41.616Z (about 1 year ago)
- Language: Swift
- Size: 11.7 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Protocol oriented network abstraction in Swift
Please visit [my blog](https://blog.benchr.me/post/pop-network-1/) for more explanation.
This is a working example to fetch all public repositories of the authenticated user:
```Swift
let githubToken = "xxx"
let githubAuthenticator = SimpleAuthenticator {
$0.setValue("token \(githubToken)", forHTTPHeaderField: "Authorization")
}
struct Repository: JSONObject {
let desc: String
let name: String
init?(json: Any) {
guard let j = json as? [String: Any] else {
return nil
}
guard
let d = j["description"] as? String,
let n = j["full_name"] as? String
else {
return nil
}
self.desc = d
self.name = n
}
}
struct RepositoryEndpoint: GET {
typealias ResponseType = JSONArray
let path = "/user/repos"
var urlParameter: [String : String] {
return ["visibility": "public"]
}
}
let client = Client(baseUrl: "https://api.github.com", authenticator: githubAuthenticator)
client.get(RepositoryEndpoint()) { response in
response.res?.array // -> is now an array of all requested repositories
}
```