Go to the https://app.quicktype.io and convert the JSON into a model … you might want to mess a bit with the settings to keep the models consistent with ones in the project already. Also, any sub structs (unless they can be used elsewhere) should be moved inside of the parent model.
Oh yeah … and call the main class User! :) …
Import Vapor and conform the main model to Content instead of Codable.
Make a request extension
First you need to conform the User model to Queryable. This will enable the User.query(on: container) method.
extension User: Queryable { }
Next we create an extension on QueryableProperty which is generated each time you request a query on a container from the previous step. Make sure you specify the QueryableType == User
extension QueryableProperty where QueryableType == User {
/// Get all organizations for authenticated user
public func get() throws -> EventLoopFuture<User> {
return try github.get(path: "user")
}
}
All done
You should be able to call try User.query(on: c).get() and get an EventLoopFuture<User> with the details of your authenticated user.
GitHubKit
Super simple to use Github API client library written using Apple NIO
Functionality
The below probably don’t contain all methods from the spec but it’s a good start …
Usage
Add to your Package.swift file
Don’t forget about your target
Configure
Use in iOS and server side frameworks
Although this library has been only tested with Vapor 4, using Apple NIO HHTPClient should be compatible with any iOS or server side project.
Please let us know on our slack here how you getting on or should you need any support!
Use in Vapor 4?
Development
Adding a new API call is … surprisingly super simple too
Lets say you need to add a detail of a user
Go to the documentation
https://developer.github.com/v3/users/
Autogenerate a model
Copy the example JSON, for example:
Go to the https://app.quicktype.io and convert the JSON into a model … you might want to mess a bit with the settings to keep the models consistent with ones in the project already. Also, any sub structs (unless they can be used elsewhere) should be moved inside of the parent model.
Oh yeah … and call the main class
User
! :) …Import
Vapor
and conform the main model toContent
instead ofCodable
.Make a request extension
First you need to conform the
User
model toQueryable
. This will enable theUser.query(on: container)
method.Next we create an extension on
QueryableProperty
which is generated each time you request a query on a container from the previous step. Make sure you specify theQueryableType == User
All done
You should be able to call
try User.query(on: c).get()
and get anEventLoopFuture<User>
with the details of your authenticated user.Author
Ondrej Rafaj @rafiki270
(It wasn’t my token after all, was it?!)
License
MIT; See LICENSE file for details.