Skip to content

Commit

Permalink
Adapted new API response, where links can contain a dict too, not jus…
Browse files Browse the repository at this point in the history
…t an URL
  • Loading branch information
gklka committed Mar 7, 2024
1 parent ab0f4ac commit d7c84a0
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class UnsplashPhotoPicker: UINavigationController {

private func trackDownloads(for photos: [UnsplashPhoto]) {
for photo in photos {
if let downloadLocationURL = photo.links[.downloadLocation]?.appending(queryItems: [URLQueryItem(name: "client_id", value: Configuration.shared.accessKey)]) {
if let downloadLocationURL = photo.links.download_location?.appending(queryItems: [URLQueryItem(name: "client_id", value: Configuration.shared.accessKey)]) {
URLSession.shared.dataTask(with: downloadLocationURL).resume()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ public struct UnsplashPhoto: Codable {
case small
case thumb
}

public enum LinkKind: String, Codable {
case own = "self"
case html
case download
case downloadLocation = "download_location"

public struct Links: Codable {
var `self`: URL?
var html: URL?
var alternative_html: [String: URL]?
var download: URL?
var download_location: URL?
}

public let identifier: String
Expand All @@ -33,7 +34,7 @@ public struct UnsplashPhoto: Codable {
public let exif: UnsplashPhotoExif?
public let user: UnsplashUser
public let urls: [URLKind: URL]
public let links: [LinkKind: URL]
public let links: Links
public let likesCount: Int
public let downloadsCount: Int?
public let viewsCount: Int?
Expand Down Expand Up @@ -69,7 +70,7 @@ public struct UnsplashPhoto: Codable {
exif = try? container.decode(UnsplashPhotoExif.self, forKey: .exif)
user = try container.decode(UnsplashUser.self, forKey: .user)
urls = try container.decode([URLKind: URL].self, forKey: .urls)
links = try container.decode([LinkKind: URL].self, forKey: .links)
links = try container.decode(Links.self, forKey: .links)
likesCount = try container.decode(Int.self, forKey: .likesCount)
downloadsCount = try? container.decode(Int.self, forKey: .downloadsCount)
viewsCount = try? container.decode(Int.self, forKey: .viewsCount)
Expand All @@ -85,7 +86,7 @@ public struct UnsplashPhoto: Codable {
try? container.encode(exif, forKey: .exif)
try container.encode(user, forKey: .user)
try container.encode(urls.convert({ ($0.key.rawValue, $0.value.absoluteString) }), forKey: .urls)
try container.encode(links.convert({ ($0.key.rawValue, $0.value.absoluteString) }), forKey: .links)
try container.encode(links, forKey: .links)
try container.encode(likesCount, forKey: .likesCount)
try? container.encode(downloadsCount, forKey: .downloadsCount)
try? container.encode(viewsCount, forKey: .viewsCount)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ extension UnsplashPhotoItemProvider: NSItemProviderWriting {

case kUTTypeJPEG:
guard let url = photo.urls[.full] else {
completionHandler(nil, ItemProviderError.cannotDecodeLink(key: UnsplashPhoto.LinkKind.download.rawValue, photoIdentifier: photo.identifier))
completionHandler(nil, ItemProviderError.cannotDecodeLink(key: "download", photoIdentifier: photo.identifier))
return nil
}

let dataTask = URLSession.shared.dataTask(with: url, completionHandler: { (data, _, error) in
if error == nil, let downloadLocationURL = self.photo.links[.downloadLocation]?.appending(queryItems: [URLQueryItem(name: "client_id", value: Configuration.shared.accessKey)]) {
if error == nil, let downloadLocationURL = self.photo.links.download_location?.appending(queryItems: [URLQueryItem(name: "client_id", value: Configuration.shared.accessKey)]) {
let pingDownloadTask = URLSession.shared.dataTask(with: downloadLocationURL)
pingDownloadTask.resume()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,6 @@ extension KeyedDecodingContainer {
return result
}

func decode(_ type: [UnsplashPhoto.LinkKind: URL].Type, forKey key: Key) throws -> [UnsplashPhoto.LinkKind: URL] {
let linksDictionary = try self.decode([String: String].self, forKey: key)
var result = [UnsplashPhoto.LinkKind: URL]()
for (key, value) in linksDictionary {
if let kind = UnsplashPhoto.LinkKind(rawValue: key),
let url = URL(string: value) {
result[kind] = url
}
}
return result
}

func decode(_ type: [UnsplashUser.ProfileImageSize: URL].Type, forKey key: Key) throws -> [UnsplashUser.ProfileImageSize: URL] {
let sizesDictionary = try self.decode([String: String].self, forKey: key)
var result = [UnsplashUser.ProfileImageSize: URL]()
Expand Down

0 comments on commit d7c84a0

Please sign in to comment.