Skip to content

Commit

Permalink
Update for backwards async/await
Browse files Browse the repository at this point in the history
  • Loading branch information
rudrankriyam committed Oct 31, 2021
1 parent 418be2d commit 48f6b50
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 49 deletions.
4 changes: 2 additions & 2 deletions Sources/QuoteKit/APIs/AuthorAPIs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ 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)))
}
Expand All @@ -21,7 +21,7 @@ public extension QuoteKit {
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,
Expand Down
20 changes: 10 additions & 10 deletions Sources/QuoteKit/APIs/QuoteAPIs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
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,
Expand All @@ -42,7 +42,7 @@ public extension QuoteKit {
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,
Expand All @@ -67,7 +67,7 @@ public extension QuoteKit {
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,
Expand Down Expand Up @@ -110,17 +110,17 @@ public extension QuoteKit {
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,
Expand All @@ -136,7 +136,7 @@ public extension QuoteKit {
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,
Expand All @@ -153,7 +153,7 @@ public extension QuoteKit {
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,
Expand Down
36 changes: 15 additions & 21 deletions Sources/QuoteKit/APIs/SearchAPIs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,18 @@
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,
Expand All @@ -49,7 +30,20 @@ public extension QuoteKit {
return try await execute(with: QuotableEndpoint(path, queryItems: queryItems))
}

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

static func searchAuthors(for query: String,
limit: Int = 20,
page: Int = 1,
completion: @escaping (Result<Authors?, Error>) -> ()) {
search(path: .searchAuthors, query: query, completion: completion)
}

private static func search<Model: Decodable>(path: QuotableEndpointPath,
query: String,
limit: Int = 20,
Expand All @@ -61,7 +55,7 @@ public extension QuoteKit {
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] {
Expand Down
6 changes: 3 additions & 3 deletions Sources/QuoteKit/APIs/TagAPIs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
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? {

Expand All @@ -17,7 +17,7 @@ public extension QuoteKit {
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>) -> ()) {
Expand All @@ -27,7 +27,7 @@ public extension QuoteKit {
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] {

Expand Down
2 changes: 2 additions & 0 deletions Sources/QuoteKit/Endpoint/QuotableURLHost.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import Foundation

/// Learnt this way of creating a URLHost from this article - https://www.swiftbysundell.com/articles/testing-networking-logic-in-swift/

struct QuotableURLHost: RawRepresentable {
var rawValue: String
}
Expand Down
13 changes: 13 additions & 0 deletions Sources/QuoteKit/Extension/QuoteFetchError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// QuoteFetchError.swift
//
//
// Created by Rudrank Riyam on 31/10/21.
//

import Foundation

enum QuoteFetchError: Error {
case invalidURL
case missingData
}
33 changes: 33 additions & 0 deletions Sources/QuoteKit/Extension/URLSession.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// URLSession.swift
//
//
// Created by Rudrank Riyam on 31/10/21.
//

import Foundation

// Taken from Swift by Sundell -
// (Making async system APIs backward compatible)[https://www.swiftbysundell.com/articles/making-async-system-apis-backward-compatible/]
@available(iOS, deprecated: 15.0, message: "Use the built-in API instead")
extension URLSession {
func data(from url: URL) async throws -> (Data, URLResponse) {
try await withCheckedThrowingContinuation { continuation in
let task = self.dataTask(with: url) { data, response, error in
if let error = error {
return continuation.resume(throwing: error)
}

guard let response = response else {
return continuation.resume(throwing: URLError(.badServerResponse))
}

guard let data = data else {
return continuation.resume(throwing: QuoteFetchError.missingData)
}
continuation.resume(returning: (data, response))
}
task.resume()
}
}
}
6 changes: 0 additions & 6 deletions Sources/QuoteKit/QuoteKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@

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

Expand Down
6 changes: 3 additions & 3 deletions Tests/QuoteKitTests/Authors/AuthorsDataTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import XCTest
@testable import QuoteKit

final class AuthorsDataTests: XCTestCase {
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)

func testAuthorMatchesParticularID() async throws {
do {
let author = try await QuoteKit.author(id: "XYxYtSeixS-o")
Expand All @@ -26,7 +26,7 @@ final class AuthorsDataTests: XCTestCase {
}
}

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)

func testAuthorsReturnsManyAuthors() async throws {
do {
let authors = try await QuoteKit.authors()
Expand All @@ -38,7 +38,7 @@ final class AuthorsDataTests: XCTestCase {
}
}

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)

func testAuthorsSearchForParticularQuery() async throws {
do {
let authors = try await QuoteKit.searchAuthors(for: "aesop")
Expand Down
6 changes: 3 additions & 3 deletions Tests/QuoteKitTests/Quotes/QuotesDataTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import XCTest
@testable import QuoteKit

final class QuotesDataTests: XCTestCase {
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)

func testQuoteForParticularIDAsync() async throws {
do {
let quote = try await QuoteKit.quote(id: "2xpHvSOQMD")
Expand Down Expand Up @@ -59,7 +59,7 @@ final class QuotesDataTests: XCTestCase {
XCTAssertEqual(unwrappedQuote.dateModified, "2021-06-18")
}

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)

func testQuotesReturnsManyQuotesAsync() async throws {
do {
let quotes = try await QuoteKit.quotes()
Expand Down Expand Up @@ -95,7 +95,7 @@ final class QuotesDataTests: XCTestCase {
XCTAssertGreaterThan(unwrappedQuotes.count, 1)
}

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)

func testQuotesSearchForParticularQueryAsync() async throws {
do {
let quotes = try await QuoteKit.searchQuotes(for: "biggest room")
Expand Down
2 changes: 1 addition & 1 deletion Tests/QuoteKitTests/Tags/QuoteKitTagsDataTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import XCTest
@testable import QuoteKit

final class QuoteKitTagsDataTests: XCTestCase {
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)

func testTagsReturnsManyTags() async throws {
do {
let tags = try await QuoteKit.tags()
Expand Down

0 comments on commit 48f6b50

Please sign in to comment.