Skip to content

Commit

Permalink
FEATURE: Add logging (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamayoung authored Jul 27, 2023
1 parent 9c807a1 commit 16f68e7
Show file tree
Hide file tree
Showing 19 changed files with 715 additions and 193 deletions.
8 changes: 4 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ let package = Package(
defaultLocalization: "en",

platforms: [
.macOS(.v10_15),
.iOS(.v13),
.tvOS(.v13),
.watchOS(.v6)
.macOS(.v11),
.iOS(.v14),
.tvOS(.v14),
.watchOS(.v7)
],

products: [
Expand Down
27 changes: 24 additions & 3 deletions Sources/TMDb/Certifications/CertificationService.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import Foundation
import os

///
/// Provides an interface for obtaining certification data from TMDb.
///
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *)
@available(iOS 14.0, tvOS 14.0, watchOS 7.0, macOS 11.0, *)
public final class CertificationService {

private static let logger = Logger(subsystem: Logger.tmdb, category: "CertificationService")

private let apiClient: APIClient

///
Expand All @@ -29,7 +32,16 @@ public final class CertificationService {
/// - Returns: A dictionary of movie certifications.
///
public func movieCertifications() async throws -> [String: [Certification]] {
let certifications: Certifications = try await apiClient.get(endpoint: CertificationsEndpoint.movie)
Self.logger.info("fetching movie certifications")

let certifications: Certifications
do {
certifications = try await apiClient.get(endpoint: CertificationsEndpoint.movie)
} catch let error {
Self.logger.error("failed fetching movie certifications: \(error.localizedDescription, privacy: .public)")
throw error
}

return certifications.certifications
}

Expand All @@ -41,7 +53,16 @@ public final class CertificationService {
/// - Returns: A dictionary of TV show certifications.
///
public func tvShowCertifications() async throws -> [String: [Certification]] {
let certifications: Certifications = try await apiClient.get(endpoint: CertificationsEndpoint.tvShow)
Self.logger.info("fetching movie certifications")

let certifications: Certifications
do {
certifications = try await apiClient.get(endpoint: CertificationsEndpoint.tvShow)
} catch let error {
Self.logger.error("failed fetching TV show certifications: \(error.localizedDescription, privacy: .public)")
throw error
}

return certifications.certifications
}

Expand Down
18 changes: 16 additions & 2 deletions Sources/TMDb/Company/CompanyService.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import Foundation
import os

///
/// Provides an interface for obtaining company data from TMDb.
///
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *)
@available(iOS 14.0, tvOS 14.0, watchOS 7.0, macOS 11.0, *)
public final class CompanyService {

private static let logger = Logger(subsystem: Logger.tmdb, category: "CompanyService")

private let apiClient: APIClient

///
Expand All @@ -32,7 +35,18 @@ public final class CompanyService {
/// - Returns: Matching company.
///
public func details(forCompany id: Company.ID) async throws -> Company {
try await apiClient.get(endpoint: CompanyEndpoint.details(companyID: id))
Self.logger.info("fetching company \(id, privacy: .public)")

let company: Company
do {
company = try await apiClient.get(endpoint: CompanyEndpoint.details(companyID: id))
} catch let error {
// swiftlint:disable:next line_length
Self.logger.error("failed fetching company \(id, privacy: .public): \(error.localizedDescription, privacy: .public)")
throw error
}

return company
}

}
53 changes: 48 additions & 5 deletions Sources/TMDb/Configuration/ConfigurationService.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import Foundation
import os

///
/// Provides an interface for obtaining configuration data from TMDb.
///
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *)
@available(iOS 14.0, tvOS 14.0, watchOS 7.0, macOS 11.0, *)
public final class ConfigurationService {

private static let logger = Logger(subsystem: Logger.tmdb, category: "ConfigurationService")

private let apiClient: APIClient

///
Expand All @@ -30,7 +33,17 @@ public final class ConfigurationService {
/// - Returns: The API configuration.
///
public func apiConfiguration() async throws -> APIConfiguration {
try await apiClient.get(endpoint: ConfigurationEndpoint.api)
Self.logger.info("fetching api configuration")

let apiConfiguration: APIConfiguration
do {
apiConfiguration = try await apiClient.get(endpoint: ConfigurationEndpoint.api)
} catch let error {
Self.logger.error("failed fetching api configuration: \(error.localizedDescription, privacy: .public)")
throw error
}

return apiConfiguration
}

///
Expand All @@ -41,7 +54,17 @@ public final class ConfigurationService {
/// - Returns: Countries used throughout TMDb,
///
public func countries() async throws -> [Country] {
try await apiClient.get(endpoint: ConfigurationEndpoint.countries)
Self.logger.info("fetching countries")

let countries: [Country]
do {
countries = try await apiClient.get(endpoint: ConfigurationEndpoint.countries)
} catch let error {
Self.logger.error("failed fetching countries: \(error.localizedDescription, privacy: .public)")
throw error
}

return countries
}

///
Expand All @@ -52,7 +75,17 @@ public final class ConfigurationService {
/// - Returns: Jobs and departments used on TMDb.
///
public func jobsByDepartment() async throws -> [Department] {
try await apiClient.get(endpoint: ConfigurationEndpoint.jobs)
Self.logger.info("fetching jobs by department")

let departments: [Department]
do {
departments = try await apiClient.get(endpoint: ConfigurationEndpoint.jobs)
} catch let error {
Self.logger.error("failed fetching jobs by department: \(error.localizedDescription, privacy: .public)")
throw error
}

return departments
}

///
Expand All @@ -63,7 +96,17 @@ public final class ConfigurationService {
/// - Returns: Languages used throughout TMDb.
///
public func languages() async throws -> [Language] {
try await apiClient.get(endpoint: ConfigurationEndpoint.languages)
Self.logger.info("fetching languages")

let languages: [Language]
do {
languages = try await apiClient.get(endpoint: ConfigurationEndpoint.languages)
} catch let error {
Self.logger.error("failed fetching languages: \(error.localizedDescription, privacy: .public)")
throw error
}

return languages
}

}
31 changes: 28 additions & 3 deletions Sources/TMDb/Discover/DiscoverService.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import Foundation
import os

///
/// Provides an interface for discovering movies and TV shows from TMDb.
///
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *)
@available(iOS 14.0, tvOS 14.0, watchOS 7.0, macOS 11.0, *)
public final class DiscoverService {

private static let logger = Logger(subsystem: Logger.tmdb, category: "DiscoverService")

private let apiClient: APIClient

///
Expand Down Expand Up @@ -37,7 +40,19 @@ public final class DiscoverService {
///
public func movies(sortedBy: MovieSort? = nil, withPeople people: [Person.ID]? = nil,
page: Int? = nil) async throws -> MoviePageableList {
try await apiClient.get(endpoint: DiscoverEndpoint.movies(sortedBy: sortedBy, people: people, page: page))
Self.logger.info("fetching movies")

let movieList: MoviePageableList
do {
movieList = try await apiClient.get(
endpoint: DiscoverEndpoint.movies(sortedBy: sortedBy, people: people, page: page)
)
} catch let error {
Self.logger.error("failed fetching movies: \(error.localizedDescription, privacy: .public)")
throw error
}

return movieList
}

///
Expand All @@ -54,7 +69,17 @@ public final class DiscoverService {
/// - Returns: Matching TV shows as a pageable list.
///
public func tvShows(sortedBy: TVShowSort? = nil, page: Int? = nil) async throws -> TVShowPageableList {
try await apiClient.get(endpoint: DiscoverEndpoint.tvShows(sortedBy: sortedBy, page: page))
Self.logger.info("fetching TV shows")

let tvShowList: TVShowPageableList
do {
tvShowList = try await apiClient.get(endpoint: DiscoverEndpoint.tvShows(sortedBy: sortedBy, page: page))
} catch let error {
Self.logger.error("failed fetching TV shows: \(error.localizedDescription, privacy: .public)")
throw error
}

return tvShowList
}

}
27 changes: 24 additions & 3 deletions Sources/TMDb/Genres/GenreService.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import Foundation
import os

///
/// Provides an interface for obtaining movie and TV show genres from TMDb.
///
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *)
@available(iOS 14.0, tvOS 14.0, watchOS 7.0, macOS 11.0, *)
public final class GenreService {

private static let logger = Logger(subsystem: Logger.tmdb, category: "GenreService")

private let apiClient: APIClient

///
Expand All @@ -29,7 +32,16 @@ public final class GenreService {
/// - Returns: A list of genres.
///
public func movieGenres() async throws -> [Genre] {
let genreList: GenreList = try await apiClient.get(endpoint: GenresEndpoint.movie)
Self.logger.info("fetching movie genres")

let genreList: GenreList
do {
genreList = try await apiClient.get(endpoint: GenresEndpoint.movie)
} catch let error {
Self.logger.error("failed fetching movie genres: \(error.localizedDescription, privacy: .public)")
throw error
}

return genreList.genres
}

Expand All @@ -41,7 +53,16 @@ public final class GenreService {
/// - Returns: A list of genres.
///
public func tvShowGenres() async throws -> [Genre] {
let genreList: GenreList = try await apiClient.get(endpoint: GenresEndpoint.tvShow)
Self.logger.info("fetching TV show genres")

let genreList: GenreList
do {
genreList = try await apiClient.get(endpoint: GenresEndpoint.tvShow)
} catch let error {
Self.logger.error("failed fetching TV show genres: \(error.localizedDescription, privacy: .public)")
throw error
}

return genreList.genres
}

Expand Down
8 changes: 8 additions & 0 deletions Sources/TMDb/Logger+TMDb.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Foundation
import os

extension Logger {

static let tmdb = "uk.co.adam-young.TMDb"

}
16 changes: 0 additions & 16 deletions Sources/TMDb/Models/Show.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,6 @@ public enum Show: Identifiable, Equatable, Hashable {

}

extension Show: Comparable {

public static func < (lhs: Show, rhs: Show) -> Bool {
guard let lhsDate = lhs.date else {
return false
}

guard let rhsDate = rhs.date else {
return true
}

return lhsDate > rhsDate
}

}

extension Show: Decodable {

private enum CodingKeys: String, CodingKey {
Expand Down
Loading

0 comments on commit 16f68e7

Please sign in to comment.