Reputation: 620
I am trying to parse the messages and it just pulls nil EVERY time. It would be fine if it was like once or twice, but it does it every time so something is. definitely going wrong here.
Here is what the console output is looking like
commitJson(sha: "3665294d1e813d35594d6bcdc0a61983caa6e0cd", message: nil, url: "https://api.github.com/repos/apple/swift/commits/3665294d1e813d35594d6bcdc0a61983caa6e0cd", commit: GitHubCommits.commit(author: GitHubCommits.author(date: Optional("2018-10-03T19:12:15Z"), name: "Karoy Lorentey")))
It is pulling everything but the message. I might be missing something, but I think it's better if I let my code talk. Sorry for the struct layout......
Here is the struct with the json
struct author : Codable{
var date: String
var name: String
}
struct commit : Codable {
var author: author
}
struct commitJson : Codable {
var sha: String
var message: String?
var url: String
var commit: commit
}
seems solid right? I need the optional or the thing will crash on me....
Here is the parsing
guard let url = URL(string: "https://api.github.com/repos/apple/swift/commits?per_page=100") else {return}
URLSession.shared.dataTask(with: url) { (data, statusCode, error) in
//print(statusCode)
if let error = error{
print("error : \(error)")
return
}
guard let data = data else {return}
do{
let decoder = JSONDecoder()
self.commitsArray = try decoder.decode([commitJson].self, from: data)
for commit in self.commitsArray{
print(commit)
}
} catch {
print("I have failed you with \(error)")
}
}.resume()
I feel like I am not doing anything wrong, but I wouldn't be here if I wasn't. I tried converting the thing into a string and switching the some stuff like the quotes then back into a data object, but I either got it wrong or it doesn't help at all.
Here is a cleaner sample to show what I want out of there.
*note this is all wrapped around an array brackets at the start and end
{
"sha": "80d765034c61d8bcad1d858cfa38ec599017a2f0",
"commit": {
"author": {
"name": "swift-ci",
"date": "2018-10-08T18:59:06Z"
}
"message": "Merge pull request #19764 from tokorom/vim-syntax-case-label-region",
}
Here is what a sample of what the GitHub full data block example looks like.
{
"sha": "80d765034c61d8bcad1d858cfa38ec599017a2f0",
"node_id": "MDY6Q29tbWl0NDQ4Mzg5NDk6ODBkNzY1MDM0YzYxZDhiY2FkMWQ4NThjZmEzOGVjNTk5MDE3YTJmMA==",
"commit": {
"author": {
"name": "swift-ci",
"email": "[email protected]",
"date": "2018-10-08T18:59:06Z"
},
"committer": {
"name": "GitHub",
"email": "[email protected]",
"date": "2018-10-08T18:59:06Z"
},
"message": "Merge pull request #19764 from tokorom/vim-syntax-case-label-region",
"tree": {
"sha": "d6bd4fe23f4efabcfee7fbfb6e91e5aac9b4bf6d",
"url": "https://api.github.com/repos/apple/swift/git/trees/d6bd4fe23f4efabcfee7fbfb6e91e5aac9b4bf6d"
},
"url": "https://api.github.com/repos/apple/swift/git/commits/80d765034c61d8bcad1d858cfa38ec599017a2f0",
"comment_count": 0,
"verification": {
"verified": true,
"reason": "valid",
"signature": "-----BEGIN PGP SIGNATURE-----\n\nwsBcBAABCAAQBQJbu6j6CRBK7hj4Ov3rIwAAdHIIAKv4lE8AwQ/hrqfjNaOdW/EW\nsFqNisjTOhj1YiW64VSU7l2uztogJJG0Shl/+zQQQGFNVcvxlNXjq3JF9rrThrPl\nFKwvNZoSZBgNoEbTNoMPCkS+GMVDlMw96VVHrSo4Nae4yiU+Y+WSnCqf6I+TUSRp\n5JyL6oMlSqaihgq9gkIqlDnp6i0lRJWtMyGJ7xUrJ0C985RyGyb6fG20/34UJ4TT\nzT/Beb0RyYOdwnXy+mOm/NnmhcVozOrBbZlR3X2e4myQJ6Q7INOOyYPpmAZxEXps\nmajg6J73cwaH2x6PxRmMJ3+qxCau+bX3v4pEEeT5nYEIH+hDK2uC2wC/PkM7VsU=\n=2jhi\n-----END PGP SIGNATURE-----\n",
"payload": "tree d6bd4fe23f4efabcfee7fbfb6e91e5aac9b4bf6d\nparent 52deae30eb5833e53ba68ebc8a9a87614630751d\nparent ea2c860ddb4817dc83c7152035aa05569f3a2770\nauthor swift-ci <[email protected]> 1539025146 -0700\ncommitter GitHub <[email protected]> 1539025146 -0700\n\nMerge pull request #19764 from tokorom/vim-syntax-case-label-region\n\n"
}
}
Here is the link to the API. It does have like a 60 requests per hour without an API Key limit, so be wary of that.
Upvotes: 0
Views: 51
Reputation: 114975
message
is part of the commit
, not part of the outer object.
You need:
struct author : Codable{
var date: String
var name: String
}
struct commit : Codable {
var author: author
var message: String?
}
struct commitJson : Codable {
var sha: String
var url: String
var commit: commit
}
Upvotes: 2