Skip to content

Commit

Permalink
Add support for iOS 13, tvOS 13, watchOS 6, and macOS 11
Browse files Browse the repository at this point in the history
  • Loading branch information
rudrankriyam committed Oct 3, 2021
1 parent e9bf2de commit ad3e1d8
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 7 deletions.
10 changes: 10 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/QuoteKit.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "QuoteKitTests"
BuildableName = "QuoteKitTests"
BlueprintName = "QuoteKitTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import PackageDescription

let package = Package(
name: "QuoteKit",
platforms: [.iOS(.v15)],
platforms: [.iOS(.v13), .macOS(.v11), .tvOS(.v13), .watchOS(.v6)],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
Expand Down
63 changes: 57 additions & 6 deletions Sources/QuoteKit/QuoteKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,56 @@

import Foundation

enum QuoteFetchError: Error {
case invalidURL
case missingData
}

public struct QuoteKit {
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
static func execute<Model: Decodable>(with endpoint: QuotableEndpoint) async throws -> Model {
let url = endpoint.url

let (data, _) = try await URLSession.shared.data(from: url)
return try JSONDecoder().decode(Model.self, from: data)
}

static func execute<Model: Decodable>(with endpoint: QuotableEndpoint, completion: @escaping (Result<Model, Error>) -> ()) {
let url = endpoint.url

URLSession.shared.dataTask(with: url) { data, _, error in
if let error = error {
completion(.failure(error))
return
}

guard let data = data else {
completion(.failure(QuoteFetchError.missingData))
return
}

do {
let model = try JSONDecoder().decode(Model.self, from: data)
completion(.success(model))
} catch {
completion(.failure(error))
}
}.resume()
}
}

// MARK: - QUOTES APIS
public extension QuoteKit {
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
static func quote(id: String) async throws -> Quote? {
try await execute(with: QuotableEndpoint(.quote(id)))
}

static func quote(id: String, completion: @escaping (Result<Quote?, Error>) -> ()) {
execute(with: QuotableEndpoint(.quote(id)), completion: completion)
}

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
static func quotes(minLength: Int? = nil,
maxLength: Int? = nil,
tags: [String]? = nil,
Expand All @@ -33,7 +68,7 @@ public extension QuoteKit {
page: Int = 1) async throws -> Quotes? {

var queryItems: [URLQueryItem] = []

queryItems.append(.limit(limit))
queryItems.append(.page(page))

Expand Down Expand Up @@ -64,18 +99,24 @@ public extension QuoteKit {
return try await execute(with: QuotableEndpoint(.quotes, queryItems: queryItems))
}

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
static func quotes() async throws -> Quotes? {
try await execute(with: QuotableEndpoint(.quotes))
}

static func quotes(completion: @escaping (Result<Quotes?, Error>) -> ()) {
execute(with: QuotableEndpoint(.quotes), completion: completion)
}

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
static func randomQuote(minLength: Int? = nil,
maxLength: Int? = nil,
tags: [String]? = nil,
type: URLQueryItemListType = .all,
authors: [String]? = nil) async throws -> Quote? {

var queryItems: [URLQueryItem] = []

if let minLength = minLength {
queryItems.append(.minLength(minLength))
}
Expand All @@ -102,18 +143,24 @@ public extension QuoteKit {
QuotableEndpoint(.authorProfile(size, slug), host: .images).url
}

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
static func author(id: String) async throws -> Author? {
try await execute(with: QuotableEndpoint(.author(id)))
}

static func author(id: String, completion: @escaping (Result<Author?, Error>) -> ()) {
execute(with: QuotableEndpoint(.author(id)), completion: completion)
}

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
static func authors(slugs: [String]? = nil,
sortBy: AuthorsAndTagsSortType? = nil,
order: QuotableListOrder? = nil,
limit: Int = 20,
page: Int = 1) async throws -> Authors? {

var queryItems: [URLQueryItem] = []

queryItems.append(.limit(limit))
queryItems.append(.page(page))

Expand All @@ -135,11 +182,12 @@ public extension QuoteKit {

// MARK: - TAGS APIS
public extension QuoteKit {
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
static func tags(sortBy: AuthorsAndTagsSortType? = nil,
order: QuotableListOrder? = nil) async throws -> Tags? {

var queryItems: [URLQueryItem] = []

if let sortBy = sortBy {
queryItems.append(.sortBy(sortBy))
}
Expand All @@ -154,21 +202,24 @@ public extension QuoteKit {

// MARK: - SEARCH APIS
public extension QuoteKit {
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
static func searchQuotes(for query: String, limit: Int = 20, page: Int = 1) async throws -> Quotes? {
try await search(path: .searchQuotes, query: query, limit: limit, page: page)
}


@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
static func searchAuthors(for query: String, limit: Int = 20, page: Int = 1) async throws -> Authors? {
try await search(path: .searchAuthors, query: query, limit: limit, page: page)
}

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
private static func search<Model: Decodable>(path: QuotableEndpointPath, query: String, limit: Int = 20, page: Int = 1) async throws -> Model {
var queryItems: [URLQueryItem] = []

queryItems.append(.search(query))
queryItems.append(.limit(limit))
queryItems.append(.page(page))

return try await execute(with: QuotableEndpoint(path, queryItems: queryItems))
}
}

0 comments on commit ad3e1d8

Please sign in to comment.