[Improvement] DTOs #19

Merged
javier merged 6 commits from improvement/dtos into main 2023-07-23 13:51:26 +00:00
4 changed files with 62 additions and 54 deletions
Showing only changes of commit c5701a4881 - Show all commits

View File

@ -0,0 +1,50 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the AmiiboService open source project
//
// Copyright (c) 2023 Röck+Cöde VoF. and the AmiiboService project authors
// Licensed under the EUPL 1.2 or later.
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of AmiiboService project authors
//
//===----------------------------------------------------------------------===//
public extension DTO {
/// This model is a concrete genetic definition that represents the following models: ``AmiiboSeries``, ``AmiiboType``, ``Character`` and ``GameSeries``.
struct KeyName {
// MARK: Properties
/// The key of the model.
public let key: String
/// The name of the model.
public let name: String
}
}
// MARK: - Type aliases
public extension DTO {
/// This model represents the series an amiibo belongs to.
typealias AmiiboSeries = DTO.KeyName
/// This model represents the type an amiibo belongs to.
typealias AmiiboType = KeyName
/// This model represents the character an amiibo is associated to.
typealias Character = KeyName
/// This model represents the games series an amiibo is associated to.
typealias GameSeries = KeyName
}
// MARK: - Decodable
extension DTO.KeyName: Decodable {}

View File

@ -1,42 +0,0 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the AmiiboService open source project
//
// Copyright (c) 2023 Röck+Cöde VoF. and the AmiiboService project authors
// Licensed under the EUPL 1.2 or later.
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of AmiiboService project authors
//
//===----------------------------------------------------------------------===//
/// This model is a concrete genetic definition that represents the following models: ``AmiiboSeries``, ``AmiiboType``, ``Character`` and ``GameSeries``.
public struct KeyName {
// MARK: Properties
/// The key of the model.
public let key: String
/// The name of the model.
public let name: String
}
// MARK: - Type aliases
/// This model represents the series an amiibo belongs to.
public typealias AmiiboSeries = KeyName
/// This model represents the type an amiibo belongs to.
public typealias AmiiboType = KeyName
/// This model represents the character an amiibo is associated to.
public typealias Character = KeyName
/// This model represents the games series an amiibo is associated to.
public typealias GameSeries = KeyName
// MARK: - Decodable
extension KeyName: Decodable {}

View File

@ -17,10 +17,10 @@ protocol Service {
// MARK: Functions // MARK: Functions
func amiibos(filter: AmiiboFilter) async throws -> [Amiibo] func amiibos(filter: AmiiboFilter) async throws -> [Amiibo]
func amiiboSeries(filter: AmiiboSeriesFilter) async throws -> [AmiiboSeries] func amiiboSeries(filter: AmiiboSeriesFilter) async throws -> [DTO.AmiiboSeries]
func amiiboTypes(filter: AmiiboTypeFilter) async throws -> [AmiiboType] func amiiboTypes(filter: AmiiboTypeFilter) async throws -> [DTO.AmiiboType]
func gameSeries(filter: GameSeriesFilter) async throws -> [GameSeries] func gameSeries(filter: GameSeriesFilter) async throws -> [DTO.GameSeries]
func characters(filter: CharacterFilter) async throws -> [Character] func characters(filter: CharacterFilter) async throws -> [DTO.Character]
func lastUpdated() async throws -> DTO.LastUpdated func lastUpdated() async throws -> DTO.LastUpdated
} }

View File

@ -62,12 +62,12 @@ extension AmiiboService: Service {
/// - Throws: A ``AmiiboClientError`` is thrown in case a request failed, or a `DecodingError` is thrown in case the decoding of some object failed. /// - Throws: A ``AmiiboClientError`` is thrown in case a request failed, or a `DecodingError` is thrown in case the decoding of some object failed.
public func amiiboSeries( public func amiiboSeries(
filter: AmiiboSeriesFilter = .init() filter: AmiiboSeriesFilter = .init()
) async throws -> [AmiiboSeries] { ) async throws -> [DTO.AmiiboSeries] {
client.setDateDecodingStrategy(.deferredToDate) client.setDateDecodingStrategy(.deferredToDate)
return try await client.request( return try await client.request(
endpoint: GetSeriesEndpoint(parameters: filter.makeParameters()), endpoint: GetSeriesEndpoint(parameters: filter.makeParameters()),
as: Result<AmiiboSeries>.self as: Result<DTO.AmiiboSeries>.self
).items ).items
} }
@ -77,12 +77,12 @@ extension AmiiboService: Service {
/// - Throws: A ``AmiiboClientError`` is thrown in case a request failed, or a `DecodingError` is thrown in case the decoding of some object failed. /// - Throws: A ``AmiiboClientError`` is thrown in case a request failed, or a `DecodingError` is thrown in case the decoding of some object failed.
public func amiiboTypes( public func amiiboTypes(
filter: AmiiboTypeFilter = .init() filter: AmiiboTypeFilter = .init()
) async throws -> [AmiiboType] { ) async throws -> [DTO.AmiiboType] {
client.setDateDecodingStrategy(.deferredToDate) client.setDateDecodingStrategy(.deferredToDate)
return try await client.request( return try await client.request(
endpoint: GetTypeEndpoint(parameters: filter.makeParameters()), endpoint: GetTypeEndpoint(parameters: filter.makeParameters()),
as: Result<AmiiboType>.self as: Result<DTO.AmiiboType>.self
).items ).items
} }
@ -92,12 +92,12 @@ extension AmiiboService: Service {
/// - Throws: A ``AmiiboClientError`` is thrown in case a request failed, or a `DecodingError` is thrown in case the decoding of some object failed. /// - Throws: A ``AmiiboClientError`` is thrown in case a request failed, or a `DecodingError` is thrown in case the decoding of some object failed.
public func characters( public func characters(
filter: CharacterFilter = .init() filter: CharacterFilter = .init()
) async throws -> [Character] { ) async throws -> [DTO.Character] {
client.setDateDecodingStrategy(.deferredToDate) client.setDateDecodingStrategy(.deferredToDate)
return try await client.request( return try await client.request(
endpoint: GetCharacterEndpoint(parameters: filter.makeParameters()), endpoint: GetCharacterEndpoint(parameters: filter.makeParameters()),
as: Result<Character>.self as: Result<DTO.Character>.self
).items ).items
} }
@ -107,12 +107,12 @@ extension AmiiboService: Service {
/// - Throws: A ``AmiiboClientError`` is thrown in case a request failed, or a `DecodingError` is thrown in case the decoding of some object failed. /// - Throws: A ``AmiiboClientError`` is thrown in case a request failed, or a `DecodingError` is thrown in case the decoding of some object failed.
public func gameSeries( public func gameSeries(
filter: GameSeriesFilter = .init() filter: GameSeriesFilter = .init()
) async throws -> [GameSeries] { ) async throws -> [DTO.GameSeries] {
client.setDateDecodingStrategy(.deferredToDate) client.setDateDecodingStrategy(.deferredToDate)
return try await client.request( return try await client.request(
endpoint: GetGameSeriesEndpoint(parameters: filter.makeParameters()), endpoint: GetGameSeriesEndpoint(parameters: filter.makeParameters()),
as: Result<GameSeries>.self as: Result<DTO.GameSeries>.self
).items ).items
} }