Skip to content

Commit

Permalink
Add API model to simplify conforming to Codable and Sendable
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsaidi committed Oct 4, 2024
1 parent 3a27368 commit 14c7dc3
Show file tree
Hide file tree
Showing 24 changed files with 70 additions and 50 deletions.
6 changes: 6 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ Until then, breaking changes can happen in any version, and deprecated features



## 0.9.2

This version adds an `ApiModel` protocol that simplifies conforming to `Codable` and `Sendable`.



## 0.9.1

This version adjusts HTTP status code terminology.
Expand Down
2 changes: 1 addition & 1 deletion Sources/ApiKit/ApiClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ApiKit
//
// Created by Daniel Saidi on 2023-03-25.
// Copyright © 2023 Daniel Saidi. All rights reserved.
// Copyright © 2023-2024 Daniel Saidi. All rights reserved.
//

import Foundation
Expand Down
4 changes: 2 additions & 2 deletions Sources/ApiKit/ApiEnvironment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ApiKit
//
// Created by Daniel Saidi on 2023-03-24.
// Copyright © 2023 Daniel Saidi. All rights reserved.
// Copyright © 2023-2024 Daniel Saidi. All rights reserved.
//

import Foundation
Expand All @@ -21,7 +21,7 @@ import Foundation
/// headers and query parameters they need. Environments can
/// define global headers and query parameters, while routes
/// can define route-specific ones.
public protocol ApiEnvironment: ApiRequestData {
public protocol ApiEnvironment: ApiRequestData, Sendable {

/// The base URL of the environment.
var url: String { get }
Expand Down
2 changes: 1 addition & 1 deletion Sources/ApiKit/ApiError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ApiKit
//
// Created by Daniel Saidi on 2023-03-25.
// Copyright © 2023 Daniel Saidi. All rights reserved.
// Copyright © 2023-2024 Daniel Saidi. All rights reserved.
//

import Foundation
Expand Down
14 changes: 14 additions & 0 deletions Sources/ApiKit/ApiModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// ApiModel.swift
// ApiKit
//
// Created by Daniel Saidi on 2024-10-04.
// Copyright © 2024 Daniel Saidi. All rights reserved.
//

/// This protocol can be implemented by API-specific models.
///
/// This protocol makes a type conform to both `Codable` and
/// `Sendable`, which simplifies conforming to both when you
/// create your API models.
public protocol ApiModel: Codable, Sendable {}
2 changes: 1 addition & 1 deletion Sources/ApiKit/ApiRequestData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ApiKit
//
// Created by Daniel Saidi on 2023-03-28.
// Copyright © 2023 Daniel Saidi. All rights reserved.
// Copyright © 2023-2024 Daniel Saidi. All rights reserved.
//

import Foundation
Expand Down
2 changes: 1 addition & 1 deletion Sources/ApiKit/ApiResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ApiKit
//
// Created by Daniel Saidi on 2023-03-25.
// Copyright © 2023 Daniel Saidi. All rights reserved.
// Copyright © 2023-2024 Daniel Saidi. All rights reserved.
//

import Foundation
Expand Down
4 changes: 2 additions & 2 deletions Sources/ApiKit/ApiRoute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ApiKit
//
// Created by Daniel Saidi on 2023-03-24.
// Copyright © 2023 Daniel Saidi. All rights reserved.
// Copyright © 2023-2024 Daniel Saidi. All rights reserved.
//

import Foundation
Expand All @@ -27,7 +27,7 @@ import Foundation
/// headers and query parameters they need. Environments can
/// define global headers and query parameters, while routes
/// can define route-specific ones.
public protocol ApiRoute: ApiRequestData {
public protocol ApiRoute: Sendable, ApiRequestData {

/// The HTTP method to use for the route.
var httpMethod: HttpMethod { get }
Expand Down
2 changes: 1 addition & 1 deletion Sources/ApiKit/Extensions/String+UrlEncode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ApiKit
//
// Created by Daniel Saidi on 2023-03-24.
// Copyright © 2023 Daniel Saidi. All rights reserved.
// Copyright © 2023-2024 Daniel Saidi. All rights reserved.
//
// https://danielsaidi.com/blog/2020/06/04/string-urlencode
//
Expand Down
2 changes: 1 addition & 1 deletion Sources/ApiKit/Http/HttpMethod.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ApiKit
//
// Created by Daniel Saidi on 2023-03-24.
// Copyright © 2023 Daniel Saidi. All rights reserved.
// Copyright © 2023-2024 Daniel Saidi. All rights reserved.
//

import Foundation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ApiKit
//
// Created by Daniel Saidi on 2023-08-17.
// Copyright © 2023 Daniel Saidi. All rights reserved.
// Copyright © 2023-2024 Daniel Saidi. All rights reserved.
//

import Foundation
Expand Down
14 changes: 7 additions & 7 deletions Sources/ApiKit/Integrations/TheMovieDb/TheMovieDb+Models.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
// ApiKit
//
// Created by Daniel Saidi on 2023-08-17.
// Copyright © 2023 Daniel Saidi. All rights reserved.
// Copyright © 2023-2024 Daniel Saidi. All rights reserved.
//

import Foundation

public extension TheMovieDb {

/// This type represents a TheMovieDb movie.
struct Movie: Codable, Identifiable {
struct Movie: ApiModel, Identifiable {

public let id: Int
public let imdbId: String?
public let title: String
Expand Down Expand Up @@ -75,15 +75,15 @@ public extension TheMovieDb {
}

/// This type represents a TheMovieDb movie genre.
struct MovieGenre: Codable, Identifiable {
struct MovieGenre: ApiModel, Identifiable {

public let id: Int
public let name: String
}

/// This type represents a TheMovieDb pagination result.
struct MoviesPaginationResult: Codable {
struct MoviesPaginationResult: ApiModel {

public let page: Int
public let results: [Movie]
public let totalPages: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ApiKit
//
// Created by Daniel Saidi on 2023-08-17.
// Copyright © 2023 Daniel Saidi. All rights reserved.
// Copyright © 2023-2024 Daniel Saidi. All rights reserved.
//

import Foundation
Expand Down
2 changes: 1 addition & 1 deletion Sources/ApiKit/Integrations/TheMovieDb/TheMovieDb.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ApiKit
//
// Created by Daniel Saidi on 2023-03-28.
// Copyright © 2023 Daniel Saidi. All rights reserved.
// Copyright © 2023-2024 Daniel Saidi. All rights reserved.
//

import Foundation
Expand Down
2 changes: 1 addition & 1 deletion Sources/ApiKit/Integrations/Yelp/Yelp+Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ApiKit
//
// Created by Daniel Saidi on 2023-08-17.
// Copyright © 2023 Daniel Saidi. All rights reserved.
// Copyright © 2023-2024 Daniel Saidi. All rights reserved.
//

import Foundation
Expand Down
42 changes: 21 additions & 21 deletions Sources/ApiKit/Integrations/Yelp/Yelp+Models.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
// ApiKit
//
// Created by Daniel Saidi on 2023-08-17.
// Copyright © 2023 Daniel Saidi. All rights reserved.
// Copyright © 2023-2024 Daniel Saidi. All rights reserved.
//

import Foundation

public extension Yelp {

/// This type represents a Yelp restaurant (business).
struct Restaurant: Codable {
struct Restaurant: ApiModel {

public let id: String
public let alias: String?
public let name: String?
Expand Down Expand Up @@ -54,21 +54,21 @@ public extension Yelp {


/// This type represents a Yelp restaurant category.
struct RestaurantCategory: Codable {
struct RestaurantCategory: ApiModel {

public let title: String
}

/// This type represents Yelp restaurant coordinates.
struct RestaurantCoordinates: Codable {
struct RestaurantCoordinates: ApiModel {

public let latitude: Double?
public let longitude: Double?
}

/// This type represents a Yelp restaurant opening hours.
struct RestaurantHour: Codable {
struct RestaurantHour: ApiModel {

public let isOvernight: Bool
public let start: String
public let end: String
Expand All @@ -83,8 +83,8 @@ public extension Yelp {
}

/// This type represents a Yelp restaurant opening hour.
struct RestaurantHours: Codable {
struct RestaurantHours: ApiModel {

public let type: String
public let isOpenNow: Bool
public let open: [RestaurantHour]
Expand All @@ -97,8 +97,8 @@ public extension Yelp {
}

/// This type represents a Yelp restaurant location.
struct RestaurantLocation: Codable {
struct RestaurantLocation: ApiModel {

public let displayAddress: [String]

enum CodingKeys: String, CodingKey {
Expand All @@ -107,8 +107,8 @@ public extension Yelp {
}

/// This type represents a Yelp restaurant review.
struct RestaurantReview: Codable {
struct RestaurantReview: ApiModel {

public let id: String
public let url: String?
public let text: String?
Expand All @@ -117,8 +117,8 @@ public extension Yelp {
}

/// This type represents a Yelp restaurant review user.
struct RestaurantReviewUser: Codable {
struct RestaurantReviewUser: ApiModel {

public let id: String
public let name: String?
public let imageUrl: String?
Expand All @@ -137,8 +137,8 @@ public extension Yelp {
}

/// This type represents Yelp search parameters.
struct SearchParams {
struct SearchParams: Sendable {

public init(
skip: Int,
take: Int,
Expand All @@ -155,14 +155,14 @@ public extension Yelp {
self.openingHours = openingHours
}

public enum BudgetLevel: String {
public enum BudgetLevel: String, Sendable {
case level1 = "1"
case level2 = "2"
case level3 = "3"
case level4 = "4"
}

public enum OpeningHours: String {
public enum OpeningHours: String, Sendable {
case openNow
case showAll
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/ApiKit/Integrations/Yelp/Yelp+Route.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ApiKit
//
// Created by Daniel Saidi on 2023-08-17.
// Copyright © 2023 Daniel Saidi. All rights reserved.
// Copyright © 2023-2024 Daniel Saidi. All rights reserved.
//

import Foundation
Expand Down
2 changes: 1 addition & 1 deletion Sources/ApiKit/Integrations/Yelp/Yelp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ApiKit
//
// Created by Daniel Saidi on 2023-03-28.
// Copyright © 2023 Daniel Saidi. All rights reserved.
// Copyright © 2023-2024 Daniel Saidi. All rights reserved.
//

import Foundation
Expand Down
2 changes: 1 addition & 1 deletion Tests/ApiKitTests/ApiClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ApiKit
//
// Created by Daniel Saidi on 2023-03-24.
// Copyright © 2023 Daniel Saidi. All rights reserved.
// Copyright © 2023-2024 Daniel Saidi. All rights reserved.
//

import ApiKit
Expand Down
2 changes: 1 addition & 1 deletion Tests/ApiKitTests/ApiEnvironmentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ApiKit
//
// Created by Daniel Saidi on 2023-03-25.
// Copyright © 2023 Daniel Saidi. All rights reserved.
// Copyright © 2023-2024 Daniel Saidi. All rights reserved.
//

import XCTest
Expand Down
2 changes: 1 addition & 1 deletion Tests/ApiKitTests/ApiRequestDataTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ApiKit
//
// Created by Daniel Saidi on 2023-03-28.
// Copyright © 2023 Daniel Saidi. All rights reserved.
// Copyright © 2023-2024 Daniel Saidi. All rights reserved.
//

import ApiKit
Expand Down
2 changes: 1 addition & 1 deletion Tests/ApiKitTests/ApiRouteTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ApiKit
//
// Created by Daniel Saidi on 2023-03-24.
// Copyright © 2023 Daniel Saidi. All rights reserved.
// Copyright © 2023-2024 Daniel Saidi. All rights reserved.
//

import XCTest
Expand Down
2 changes: 1 addition & 1 deletion Tests/ApiKitTests/HttpMethodTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ApiKit
//
// Created by Daniel Saidi on 2023-03-24.
// Copyright © 2023 Daniel Saidi. All rights reserved.
// Copyright © 2023-2024 Daniel Saidi. All rights reserved.
//

import ApiKit
Expand Down
2 changes: 1 addition & 1 deletion Tests/ApiKitTests/TestTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ApiKit
//
// Created by Daniel Saidi on 2023-03-28.
// Copyright © 2023 Daniel Saidi. All rights reserved.
// Copyright © 2023-2024 Daniel Saidi. All rights reserved.
//

import ApiKit
Expand Down

0 comments on commit 14c7dc3

Please sign in to comment.