Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swift5 #19

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

Unsplash API client written in Swift.

This Fork updates the Official API section to swift 5.0

[Unsplash](https://unsplash.com/) offers 2 APIs:
- [Source API](https://source.unsplash.com/) (unlimited requests)
- [Official API](https://unsplash.com/documentation)
Expand Down
5 changes: 3 additions & 2 deletions UnsplashKit.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ Swift client for unsplash.com API
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'Caramba.io' => '[email protected]' }
s.source = { :git => 'https://github.com/carambalabs/UnsplashKit.git', :tag => s.version.to_s }
s.swift_version = '5.0'

s.default_subspec = 'Source'

s.subspec "Foundation" do |ss|
ss.source_files = 'UnsplashKit/Classes/Foundation/**/*.swift'
ss.dependency 'Unbox', '~> 2.3'
ss.dependency 'Result', '~> 3.1'
ss.dependency 'Unbox', '~> 4.0'
ss.dependency 'Result', '~> 5.0'
ss.dependency 'HTTPStatusCodes', '~> 3.1'
ss.frameworks = ["CoreLocation"]
end
Expand Down
6 changes: 3 additions & 3 deletions UnsplashKit/Classes/Foundation/Resource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ public struct Resource<A> {
internal init(request: @escaping (URLComponents) -> URLRequest,
jsonParse: @escaping (Any, HTTPURLResponse) throws -> A) {
self.request = request
self.parse = { input -> A in
guard let json = try JSONSerialization.jsonObject(with: input.0, options: []) as? [String: Any] else {
self.parse = { data,response -> A in
guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] else {
throw ResourceError.invalidData
}
return try jsonParse(json, input.1)
return try jsonParse(json, response)
}
}

Expand Down
13 changes: 6 additions & 7 deletions UnsplashKit/Classes/Foundation/WebLinking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ public struct Link: Equatable, Hashable {
self.parameters = parameters ?? [:]
}

/// Returns the hash value
public var hashValue: Int {
return uri.hashValue
public func hash(into hasher: inout Hasher) {
hasher.combine(uri)
}

/// Relation type of the Link.
Expand Down Expand Up @@ -170,9 +169,9 @@ func split(_ separator: String) -> (String) -> (String, String) {
let range = input.range(of: separator, options: NSString.CompareOptions(rawValue: 0), range: nil, locale: nil)

if let range = range {
let lhs = input.substring(to: range.lowerBound)
let rhs = input.substring(from: range.upperBound)
return (lhs, rhs)
let lhs = input[..<range.lowerBound]
let rhs = input[range.upperBound...]
return (String(lhs), String(rhs))
}

return (input, "")
Expand All @@ -193,7 +192,7 @@ func takeFirst(_ input: [String]) -> (String, ArraySlice<String>) {
func trim(_ lhs: Character, _ rhs: Character) -> (String) -> String {
return { input in
if input.hasPrefix("\(lhs)") && input.hasSuffix("\(rhs)") {
return input[input.characters.index(after: input.startIndex)..<input.characters.index(before: input.endIndex)]
return String(input[input.index(after: input.startIndex)..<input.index(before: input.endIndex)])
}

return input
Expand Down
6 changes: 3 additions & 3 deletions UnsplashKit/Classes/UnsplashAPI/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public final class UnsplashClient {

// MARK: - Constants

fileprivate struct Constants {
static var location: String = "https://api.unsplash.com/"
public struct Constants {
public static var location: String = "https://api.unsplash.com/"
}

// MARK: - Attributes
Expand Down Expand Up @@ -119,7 +119,7 @@ public final class UnsplashClient {

public extension UnsplashClient {

convenience public init(location: URL = URL(string: UnsplashClient.Constants.location)!,
convenience init(location: URL = URL(string: UnsplashClient.Constants.location)!,
session: URLSession = URLSession.shared,
headersProvider: HeadersProvider) {
self.init(location: location, session: session, headers: headersProvider.headers)
Expand Down
32 changes: 16 additions & 16 deletions UnsplashKit/Classes/UnsplashAPI/Models/Collection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ public struct Collection: Unboxable {
public init(unboxer: Unboxer) throws {
self.id = try unboxer.unbox(key: "id")
self.title = try unboxer.unbox(key: "title")
self.description = unboxer.unbox(key: "description")
self.description = try? unboxer.unbox(key: "description")
self.publishedAt = try unboxer.unbox(key: "published_at", formatter: DateFormatter.unsplash)
self.curated = try unboxer.unbox(key: "curated")
self.featured = try unboxer.unbox(key: "featured")
self.totalPhotos = try unboxer.unbox(key: "total_photos")
self.isPrivate = try unboxer.unbox(key: "private")
self.coverPhoto = unboxer.unbox(key: "cover_photo")
self.user = unboxer.unbox(key: "user")
self.links = unboxer.unbox(key: "links")
self.coverPhoto = try? unboxer.unbox(key: "cover_photo")
self.user = try? unboxer.unbox(key: "user")
self.links = try? unboxer.unbox(key: "links")
}

}
Expand All @@ -69,7 +69,7 @@ public extension Collection {
/// - page: page to be fetched.
/// - perPage: number of items per page.
/// - Returns: resource for fetching the collections.
public static func list(page: Int = 1,
static func list(page: Int = 1,
perPage: Int = 10) -> Resource<[Collection]> {
var queryItems: [URLQueryItem] = []
queryItems.append(URLQueryItem(name: "page", value: "\(page)"))
Expand All @@ -90,7 +90,7 @@ public extension Collection {
/// - page: page to be fetched.
/// - perPage: number of items to be fetched.
/// - Returns: resource for fetching the featured collections.
public static func featured(page: Int = 1,
static func featured(page: Int = 1,
perPage: Int = 10) -> Resource<[Collection]> {
var queryItems: [URLQueryItem] = []
queryItems.append(URLQueryItem(name: "page", value: "\(page)"))
Expand All @@ -111,7 +111,7 @@ public extension Collection {
/// - page: page to be fetched.
/// - perPage: number of items to be fetched.
/// - Returns: resource for fetching curated collections.
public static func curated(page: Int = 1,
static func curated(page: Int = 1,
perPage: Int = 10) -> Resource<[Collection]> {
var queryItems: [URLQueryItem] = []
queryItems.append(URLQueryItem(name: "page", value: "\(page)"))
Expand All @@ -130,7 +130,7 @@ public extension Collection {
///
/// - Parameter id: collection identifier.
/// - Returns: resource for fetching the collection.
public static func get(id: String) -> Resource<Collection> {
static func get(id: String) -> Resource<Collection> {
let queryItems: [URLQueryItem] = []
return Resource { (components: URLComponents) -> URLRequest in
var mutable: URLComponents = components
Expand All @@ -149,7 +149,7 @@ public extension Collection {
/// - page: page to be fetched.
/// - perPage: items per page.
/// - Returns: resource for fetching the photos.
public static func photos(id: String,
static func photos(id: String,
page: Int = 1,
perPage: Int = 10) -> Resource<[Photo]> {
var queryItems: [URLQueryItem] = []
Expand All @@ -172,7 +172,7 @@ public extension Collection {
/// - page: page to be fetched.
/// - perPage: number of items per page.
/// - Returns: resource for fetching the photos.
public static func curatedPhotos(id: String,
static func curatedPhotos(id: String,
page: Int = 1,
perPage: Int = 10) -> Resource<[Photo]> {
var queryItems: [URLQueryItem] = []
Expand All @@ -192,7 +192,7 @@ public extension Collection {
///
/// - Parameter id: collection identifier whose related ones will be fetched.
/// - Returns: resource for fetching the related collections.
public static func related(id: String) -> Resource<[Collection]> {
static func related(id: String) -> Resource<[Collection]> {
let queryItems: [URLQueryItem] = []
return Resource { (components: URLComponents) -> URLRequest in
var mutable: URLComponents = components
Expand All @@ -211,7 +211,7 @@ public extension Collection {
/// - description: collection title.
/// - isPrivate: collection private value.
/// - Returns: resource for creating the collection.
public static func create(title: String,
static func create(title: String,
description: String? = nil,
isPrivate: Bool? = nil) -> Resource<Collection> {
var queryItems: [URLQueryItem] = []
Expand Down Expand Up @@ -240,7 +240,7 @@ public extension Collection {
/// - description: new description.
/// - isPrivate: new private value.
/// - Returns: resource for updating theh collection.
public static func update(id: String,
static func update(id: String,
title: String? = nil,
description: String? = nil,
isPrivate: Bool? = nil) -> Resource<Collection> {
Expand Down Expand Up @@ -268,7 +268,7 @@ public extension Collection {
///
/// - Parameter id: collection identifier.
/// - Returns: resource for deleting the collection.
public static func delete(id: String) -> Resource<Void> {
static func delete(id: String) -> Resource<Void> {
let queryItems: [URLQueryItem] = []
return Resource(request: { (components) -> URLRequest in
var mutable: URLComponents = components
Expand All @@ -288,7 +288,7 @@ public extension Collection {
/// - id: photo identifier.
/// - collection: collection identifier.
/// - Returns: resource for adding the photo.
public static func addPhoto(with id: String,
static func addPhoto(with id: String,
to collection: String) -> Resource<Void> {
var queryItems: [URLQueryItem] = []
queryItems.append(URLQueryItem(name: "photo_id", value: "\(id)"))
Expand All @@ -310,7 +310,7 @@ public extension Collection {
/// - id: photo identifier.
/// - collection: collection identifier.
/// - Returns: resource for deleting the photo.
public static func deletePhoto(with id: String,
static func deletePhoto(with id: String,
from collection: String) -> Resource<Void> {
var queryItems: [URLQueryItem] = []
queryItems.append(URLQueryItem(name: "photo_id", value: "\(id)"))
Expand Down
38 changes: 23 additions & 15 deletions UnsplashKit/Classes/UnsplashAPI/Models/Photo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public struct Photo: Unboxable {
/// Photo current user collections.
public let currentUserCollections: [Collection]?

/// Photo description
public let description: String?

/// Photo alternative description
public let altDescription: String?

// MARK: - Unboxable

/// Initialize an instance of this model by unboxing a dictionary using an Unboxer
Expand All @@ -51,11 +57,13 @@ public struct Photo: Unboxable {
self.height = try unboxer.unbox(key: "height")
self.color = try unboxer.unbox(key: "color")
self.likes = try unboxer.unbox(key: "likes")
self.urls = unboxer.unbox(key: "urls")
self.links = unboxer.unbox(key: "links")
self.categories = unboxer.unbox(key: "categories")
self.user = unboxer.unbox(key: "user")
self.currentUserCollections = unboxer.unbox(key: "current_user_collections")
self.urls = try? unboxer.unbox(key: "urls")
self.links = try? unboxer.unbox(key: "links")
self.categories = try? unboxer.unbox(key: "categories")
self.user = try? unboxer.unbox(key: "user")
self.currentUserCollections = try? unboxer.unbox(key: "current_user_collections")
self.description = try? unboxer.unbox(key: "description")
self.altDescription = try? unboxer.unbox(key: "alt_description")
}

}
Expand All @@ -72,7 +80,7 @@ public extension Photo {
/// - perPage: number of items per page.
/// - orderBy: order by value.
/// - Returns: resource for fetching photos.
public static func list(page: Int = 1,
static func list(page: Int = 1,
perPage: Int = 10,
orderBy: Order = .latest) -> Resource<[Photo]> {
var queryItems: [URLQueryItem] = []
Expand All @@ -96,7 +104,7 @@ public extension Photo {
/// - perPage: number of items per page.
/// - orderBy: order by value.
/// - Returns: resource for fetching the curated photos.
public static func curated(page: Int = 1,
static func curated(page: Int = 1,
perPage: Int = 10,
orderBy: Order = .latest) -> Resource<[Photo]> {
var queryItems: [URLQueryItem] = []
Expand All @@ -120,7 +128,7 @@ public extension Photo {
/// - size: photo size.
/// - rect: photo rect.
/// - Returns: resource for fetching a photo.
public static func get(id: String,
static func get(id: String,
size: CGSize? = nil,
rect: CGRect? = nil) -> Resource<Photo> {
var queryItems: [URLQueryItem] = []
Expand Down Expand Up @@ -152,7 +160,7 @@ public extension Photo {
/// - size: size of the photos.
/// - orientation: orientation filtering.
/// - Returns: resource for fetching the photos.
public static func random(categories: [String] = [],
static func random(categories: [String] = [],
collections: [String] = [],
featured: Bool? = nil,
username: String? = nil,
Expand Down Expand Up @@ -195,7 +203,7 @@ public extension Photo {
/// - size: size of the photos.
/// - orientation: orientation used for filtering.
/// - Returns: resource for fetching the photos.
public static func random(count: Int,
static func random(count: Int,
categories: [String] = [],
collections: [String] = [],
featured: Bool? = nil,
Expand Down Expand Up @@ -233,7 +241,7 @@ public extension Photo {
///
/// - Parameter id: photo identifier.
/// - Returns: resource for fetching the stats.
public static func stats(id: String) -> Resource<PhotoStats> {
static func stats(id: String) -> Resource<PhotoStats> {
return Resource { (components: URLComponents) -> URLRequest in
var mutable: URLComponents = components
mutable.path = "/photos/\(id)/stats"
Expand All @@ -247,7 +255,7 @@ public extension Photo {
///
/// - Parameter id: photo identifier.
/// - Returns: resource for fetching the photo download link.
public static func downloadLink(id: String) -> Resource<String> {
static func downloadLink(id: String) -> Resource<String> {
return Resource(request: { (components) -> URLRequest in
var mutable: URLComponents = components
mutable.path = "/photos/\(id)/download"
Expand Down Expand Up @@ -275,7 +283,7 @@ public extension Photo {
/// - exifFocalLength: photo new exif focal length.
/// - exifIsoSpeedRatings: photo new exif iso speed ratings.
/// - Returns: resource for updating the photo.
public static func update(id: String,
static func update(id: String,
locationPosition: CLLocation? = nil,
locationName: String? = nil,
locationCity: String? = nil,
Expand Down Expand Up @@ -336,7 +344,7 @@ public extension Photo {
///
/// - Parameter id: photo to be liked.
/// - Returns: resource for liking the photo.
public static func like(id: String) -> Resource<Void> {
static func like(id: String) -> Resource<Void> {
return Resource(request: { (components) -> URLRequest in
var mutable: URLComponents = components
mutable.path = "/photos/\(id)/like"
Expand All @@ -352,7 +360,7 @@ public extension Photo {
///
/// - Parameter id: photo to be unliked.
/// - Returns: resource for unliking the photo.
public static func unlike(id: String) -> Resource<Void> {
static func unlike(id: String) -> Resource<Void> {
return Resource(request: { (components) -> URLRequest in
var mutable: URLComponents = components
mutable.path = "/photos/\(id)/like"
Expand Down
2 changes: 1 addition & 1 deletion UnsplashKit/Classes/UnsplashAPI/Models/PhotoCategory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public struct PhotoCategory: Unboxable {
public init(unboxer: Unboxer) throws {
self.id = try unboxer.unbox(key: "id")
self.title = try unboxer.unbox(key: "title")
self.links = unboxer.unbox(key: "links")
self.links = try? unboxer.unbox(key: "links")
}

}
12 changes: 6 additions & 6 deletions UnsplashKit/Classes/UnsplashAPI/Models/PhotoExif.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ public struct PhotoExif: Unboxable {

/// Initialize an instance of this model by unboxing a dictionary using an Unboxer
public init(unboxer: Unboxer) throws {
self.make = unboxer.unbox(key: "make")
self.model = unboxer.unbox(key: "model")
self.exposureTime = unboxer.unbox(key: "exposure_time")
self.aperture = unboxer.unbox(key: "aperture")
self.focalLength = unboxer.unbox(key: "focal_length")
self.iso = unboxer.unbox(key: "iso")
self.make = try? unboxer.unbox(key: "make")
self.model = try? unboxer.unbox(key: "model")
self.exposureTime = try? unboxer.unbox(key: "exposure_time")
self.aperture = try? unboxer.unbox(key: "aperture")
self.focalLength = try? unboxer.unbox(key: "focal_length")
self.iso = try? unboxer.unbox(key: "iso")
}
}
10 changes: 5 additions & 5 deletions UnsplashKit/Classes/UnsplashAPI/Models/PhotoLocation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ public struct PhotoLocation: Unboxable {

/// Initialize an instance of this model by unboxing a dictionary using an Unboxer
public init(unboxer: Unboxer) throws {
self.name = unboxer.unbox(key: "name")
self.city = unboxer.unbox(key: "city")
self.country = unboxer.unbox(key: "country")
if let latitude: Double = unboxer.unbox(key: "position.latitude"),
let longitude: Double = unboxer.unbox(key: "position.longitude") {
self.name = try? unboxer.unbox(key: "name")
self.city = try? unboxer.unbox(key: "city")
self.country = try? unboxer.unbox(key: "country")
if let latitude: Double = try? unboxer.unbox(key: "position.latitude"),
let longitude: Double = try? unboxer.unbox(key: "position.longitude") {
self.location = CLLocation(latitude: latitude, longitude: longitude)
} else {
self.location = nil
Expand Down
Loading