Skip to content

Instantly share code, notes, and snippets.

@tmm
Created August 2, 2020 20:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmm/c4c6b2e6cba73c39018a4287420adfdd to your computer and use it in GitHub Desktop.
Save tmm/c4c6b2e6cba73c39018a4287420adfdd to your computer and use it in GitHub Desktop.
import Foundation
struct ProjectUser: Codable {
var id: Int
enum CodingKeys: String, CodingKey {
case id = "userId"
}
}
struct Collaborator: Codable {
var avatar: String
var id: Int
var user: ProjectUser
var username: String
enum CodingKeys: String, CodingKey {
case avatar = "avatar_link"
case id
case user = "ProjectUser"
case username = "futureland_user"
}
}
struct Project: Codable {
var collaborators: [Collaborator] = []
var createdAt: String
var description: String
var id: Int
var lastOutputAt: String
var outputs: Int = 0
var owner: ProjectUser
var slug: String
var title: String
var updatedAt: String
enum CodingKeys: String, CodingKey {
case collaborators
case createdAt
case description
case id
case lastOutputAt = "last_output_at"
case outputs = "userOutputs"
case owner = "ProjectUser"
case slug
case title
case updatedAt
}
}
struct User: Codable {
var avatar: String
var id: Int
var marathons: Int = 0
var outputs: Int
var projects: [Project] = []
var supportPack: String?
var twitter: String?
var website: String?
var username: String
enum CodingKeys: String, CodingKey {
case avatar = "avatar_link"
case id
case marathons
case outputs
case projects
case supportPack = "support_pack"
case twitter = "twitterUsername"
case website
case username = "futureland_user"
}
}
let session = URLSession.shared
let url = URL(string: "https://futureland.tv/api/users/tmm")!
let task = session.dataTask(with: url) { data, response, error in
if error != nil || data == nil {
print("Client error")
return
}
guard let httpResponse = response as? HTTPURLResponse, (200...299).contains(httpResponse.statusCode) else {
print("Server error")
return
}
guard let mime = httpResponse.mimeType, mime == "application/json" else {
print("Wrong MIME type")
return
}
let user = try! JSONDecoder().decode(User.self, from: data!)
print(user.username)
print(user.id)
for project in user.projects {
print("\(project.title) has \(project.collaborators.filter { $0.id != user.id }.count) collaborators")
}
}
task.resume()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment