Skip to content

Commit

Permalink
Add full support for iOS 13, tvOS 13, macOS 11, and watchOS 6
Browse files Browse the repository at this point in the history
  • Loading branch information
rudrankriyam committed Oct 3, 2021
1 parent ad3e1d8 commit 8be869a
Show file tree
Hide file tree
Showing 5 changed files with 379 additions and 179 deletions.
73 changes: 73 additions & 0 deletions Sources/QuoteKit/APIs/AuthorAPIs.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//
// File.swift
//
//
// Created by Rudrank Riyam on 03/10/21.
//

import Foundation

public extension QuoteKit {
static func authorImage(with slug: String, size: Int = 700) -> URL {
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? {

let queryItems = authorsParameters(slugs: slugs, sortBy: sortBy, order: order, limit: limit, page: page)

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

static func authors(slugs: [String]? = nil,
sortBy: AuthorsAndTagsSortType? = nil,
order: QuotableListOrder? = nil,
limit: Int = 20,
page: Int = 1,
completion: @escaping (Result<Authors?, Error>) -> ()) {

let queryItems = authorsParameters(slugs: slugs, sortBy: sortBy, order: order, limit: limit, page: page)

return execute(with: QuotableEndpoint(.authors, queryItems: queryItems), completion: completion)
}

private static func authorsParameters(slugs: [String]? = nil,
sortBy: AuthorsAndTagsSortType? = nil,
order: QuotableListOrder? = nil,
limit: Int = 20,
page: Int = 1) -> [URLQueryItem] {

var queryItems: [URLQueryItem] = []

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

if let slugs = slugs {
queryItems.append(.slugs(slugs))
}

if let sortBy = sortBy {
queryItems.append(.sortBy(sortBy))
}

if let order = order {
queryItems.append(.order(order))
}

return queryItems
}
}
183 changes: 183 additions & 0 deletions Sources/QuoteKit/APIs/QuoteAPIs.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
//
// File.swift
//
//
// Created by Rudrank Riyam on 03/10/21.
//

import Foundation

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)))
}

@available(iOS 13.0, macOS 11.0, tvOS 13.0, watchOS 6.0, *)
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,
type: URLQueryItemListType = .all,
authors: [String]? = nil,
sortBy: QuotesSortType? = nil,
order: QuotableListOrder? = nil,
limit: Int = 20,
page: Int = 1) async throws -> Quotes? {

let queryItems = quotesParameter(minLength: minLength,
maxLength: maxLength,
tags: tags,
type: type,
authors: authors,
sortBy: sortBy,
order: order,
limit: limit,
page: page)

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

@available(iOS 13.0, macOS 11.0, tvOS 13.0, watchOS 6.0, *)
static func quotes(minLength: Int? = nil,
maxLength: Int? = nil,
tags: [String]? = nil,
type: URLQueryItemListType = .all,
authors: [String]? = nil,
sortBy: QuotesSortType? = nil,
order: QuotableListOrder? = nil,
limit: Int = 20,
page: Int = 1,
completion: @escaping (Result<Quotes?, Error>) -> ()) {

let queryItems = quotesParameter(minLength: minLength,
maxLength: maxLength,
tags: tags,
type: type,
authors: authors,
sortBy: sortBy,
order: order,
limit: limit,
page: page)

return execute(with: QuotableEndpoint(.quotes, queryItems: queryItems), completion: completion)
}

@available(iOS 13.0, macOS 11.0, tvOS 13.0, watchOS 6.0, *)
static private func quotesParameter(minLength: Int? = nil,
maxLength: Int? = nil,
tags: [String]? = nil,
type: URLQueryItemListType = .all,
authors: [String]? = nil,
sortBy: QuotesSortType? = nil,
order: QuotableListOrder? = nil,
limit: Int = 20,
page: Int = 1) -> [URLQueryItem] {

var queryItems: [URLQueryItem] = []

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

if let minLength = minLength {
queryItems.append(.minLength(minLength))
}

if let maxLength = maxLength {
queryItems.append(.maxLength(maxLength))
}

if let tags = tags {
queryItems.append(.tags(tags, type))
}

if let authors = authors {
queryItems.append(.authors(authors))
}

if let sortBy = sortBy {
queryItems.append(.sortQuotesBy(sortBy))
}

if let order = order {
queryItems.append(.order(order))
}

return 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))
}

@available(iOS 13.0, macOS 11.0, tvOS 13.0, watchOS 6.0, *)
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? {

let queryItems = randomQuoteParameters(minLength: minLength,
maxLength: maxLength,
tags: tags,
type: type,
authors: authors)

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

@available(iOS 13.0, macOS 11.0, tvOS 13.0, watchOS 6.0, *)
static func randomQuote(minLength: Int? = nil,
maxLength: Int? = nil,
tags: [String]? = nil,
type: URLQueryItemListType = .all,
authors: [String]? = nil,
completion: @escaping (Result<Quote?, Error>) -> ()) {

let queryItems = randomQuoteParameters(minLength: minLength,
maxLength: maxLength,
tags: tags,
type: type,
authors: authors)

return execute(with: QuotableEndpoint(.randomQuote, queryItems: queryItems), completion: completion)
}

@available(iOS 13.0, macOS 11.0, tvOS 13.0, watchOS 6.0, *)
static private func randomQuoteParameters(minLength: Int? = nil,
maxLength: Int? = nil,
tags: [String]? = nil,
type: URLQueryItemListType = .all,
authors: [String]? = nil) -> [URLQueryItem] {

var queryItems: [URLQueryItem] = []

if let minLength = minLength {
queryItems.append(.minLength(minLength))
}

if let maxLength = maxLength {
queryItems.append(.maxLength(maxLength))
}

if let tags = tags {
queryItems.append(.tags(tags, type))
}

if let authors = authors {
queryItems.append(.authors(authors))
}

return queryItems
}
}
76 changes: 76 additions & 0 deletions Sources/QuoteKit/APIs/SearchAPIs.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// File.swift
//
//
// Created by Rudrank Riyam on 03/10/21.
//

import Foundation

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 13.0, macOS 11.0, tvOS 13.0, watchOS 6.0, *)
static func searchQuotes(for query: String,
limit: Int = 20,
page: Int = 1,
completion: @escaping (Result<Quotes?, Error>) -> ()) {
search(path: .searchQuotes, query: query, completion: completion)
}

@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 13.0, macOS 11.0, tvOS 13.0, watchOS 6.0, *)
static func searchAuthors(for query: String,
limit: Int = 20,
page: Int = 1,
completion: @escaping (Result<Authors?, Error>) -> ()) {
search(path: .searchAuthors, query: query, completion: completion)
}

@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 {

let queryItems = searchParameters(query: query, limit: limit, page: page)

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

@available(iOS 13.0, macOS 11.0, tvOS 13.0, watchOS 6.0, *)
private static func search<Model: Decodable>(path: QuotableEndpointPath,
query: String,
limit: Int = 20,
page: Int = 1,
completion: @escaping (Result<Model, Error>) -> ()) {

let queryItems = searchParameters(query: query, limit: limit, page: page)

return execute(with: QuotableEndpoint(path, queryItems: queryItems), completion: completion)
}

@available(iOS 13.0, macOS 11.0, tvOS 13.0, watchOS 6.0, *)
private static func searchParameters(query: String,
limit: Int = 20,
page: Int = 1) -> [URLQueryItem] {
var queryItems: [URLQueryItem] = []

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

return queryItems
}
}
46 changes: 46 additions & 0 deletions Sources/QuoteKit/APIs/TagAPIs.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// File.swift
//
//
// Created by Rudrank Riyam on 03/10/21.
//

import Foundation

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? {

let queryItems = tagsParameters(sortBy: sortBy, order: order)

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

@available(iOS 13.0, macOS 11.0, tvOS 13.0, watchOS 6.0, *)
static func tags(sortBy: AuthorsAndTagsSortType? = nil,
order: QuotableListOrder? = nil,
completion: @escaping (Result<Tags?, Error>) -> ()) {

let queryItems = tagsParameters(sortBy: sortBy, order: order)

return execute(with: QuotableEndpoint(.tags, queryItems: queryItems), completion: completion)
}

@available(iOS 13.0, macOS 11.0, tvOS 13.0, watchOS 6.0, *)
private static func tagsParameters(sortBy: AuthorsAndTagsSortType? = nil,
order: QuotableListOrder? = nil) -> [URLQueryItem] {

var queryItems: [URLQueryItem] = []

if let sortBy = sortBy {
queryItems.append(.sortBy(sortBy))
}

if let order = order {
queryItems.append(.order(order))
}

return queryItems
}
}
Loading

0 comments on commit 8be869a

Please sign in to comment.