Skip to content

Commit

Permalink
Make structs public
Browse files Browse the repository at this point in the history
- it only took me a year 💀
  • Loading branch information
gionoa committed Jul 1, 2021
1 parent 4827d22 commit a778c64
Show file tree
Hide file tree
Showing 15 changed files with 257 additions and 311 deletions.
74 changes: 0 additions & 74 deletions Sources/ErgastAPI/Models/DriverStandings.swift

This file was deleted.

4 changes: 3 additions & 1 deletion Sources/Formula1API/Formula1API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public enum Formula1API {
completion: @escaping (Result<Drivers, APIError>) -> Void) {

URLSession.shared.fetch(.drivers,
for: season) { result in
for: season,
limit: limit,
offset: offset) { result in
completion(result)
}
}
Expand Down
16 changes: 12 additions & 4 deletions Sources/Formula1API/Helpers/Endpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,20 @@ extension Endpoint {
components.host = ErgastEndpoint.host
components.path = urlPath

if let limit = limit,
let offset = offset {
components.queryItems = [URLQueryItem(name: "limit", value: limit),
URLQueryItem(name: "offset", value: offset)]
var queryItems: [URLQueryItem] = .init()

if let limit = limit {
let item = URLQueryItem(name: "limit", value: limit)
queryItems.append(item)
}

if let offset = offset {
let item = URLQueryItem(name: "offset", value: offset)
queryItems.append(item)
}

components.queryItems = queryItems

guard let validURL = components.url else { fatalError("Could not construct URL.") }

return validURL
Expand Down
7 changes: 6 additions & 1 deletion Sources/Formula1API/Helpers/Season.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ import Foundation
// MARK: - Season
/// Season enum to indicate whether a networking function is to fetch all historical data, or data for a single season.
public enum Season {
/// A year, representing a specific season (1950-2020).
/// A year, representing a specific season (1950-2021).
case year(Int)

/// All historical data endpoint (1950-2021).
case all
}

extension Season {
Expand All @@ -21,6 +24,8 @@ extension Season {
switch self {
case .year(let year):
return "/\(year)"
case .all:
return ""
}
}
}
10 changes: 5 additions & 5 deletions Sources/Formula1API/Models/Circuits.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public struct Circuits: Codable {
public let data: CircuitData

enum CodingKeys: String, CodingKey {
private enum CodingKeys: String, CodingKey {
case data = "MRData"
}
}
Expand All @@ -25,7 +25,7 @@ public struct CircuitData: Codable {
public let total: String
public let circuitTable: CircuitTable

enum CodingKeys: String, CodingKey {
private enum CodingKeys: String, CodingKey {
case url
case limit
case offset
Expand All @@ -37,17 +37,17 @@ public struct CircuitData: Codable {
public struct CircuitTable: Codable {
public let circuits: [Circuit]

enum CodingKeys: String, CodingKey {
private enum CodingKeys: String, CodingKey {
case circuits = "Circuits"
}
}

public struct Circuit: Codable {
let circuitID: String
public let circuitID: String
public let circuitName: String
public let location: Location

enum CodingKeys: String, CodingKey {
private enum CodingKeys: String, CodingKey {
case circuitID = "circuitId"
case circuitName
case location = "Location"
Expand Down
44 changes: 23 additions & 21 deletions Sources/Formula1API/Models/Constructors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,24 @@ import Foundation

/// Codable struct, used for serializing JSON from the Constructors endpoint.
public struct Constructors: Codable {
let data: ConstructorsData
public let data: ConstructorsData

enum CodingKeys: String, CodingKey {
private enum CodingKeys: String, CodingKey {
case data = "MRData"
}
}

struct ConstructorsData: Codable {
let series: String
let url: String
let limit: String
let offset: String
let total: String
let constructorTable: ConstructorTable

enum CodingKeys: String, CodingKey {
public struct ConstructorsData: Codable {
public let xmlns: String
public let series: String
public let url: String
public let limit: String
public let offset: String
public let total: String
public let constructorTable: ConstructorTable

private enum CodingKeys: String, CodingKey {
case xmlns
case series
case url
case limit
Expand All @@ -34,23 +36,23 @@ struct ConstructorsData: Codable {
}
}

struct ConstructorTable: Codable {
let season: String
let constructors: [Constructor]
public struct ConstructorTable: Codable {
public let season: String?
public let constructors: [Constructor]

enum CodingKeys: String, CodingKey {
private enum CodingKeys: String, CodingKey {
case season
case constructors = "Constructors"
}
}

struct Constructor: Codable {
let constructorID: String
let url: String
let name: String
let nationality: String
public struct Constructor: Codable {
public let constructorID: String
public let url: String
public let name: String
public let nationality: String

enum CodingKeys: String, CodingKey {
private enum CodingKeys: String, CodingKey {
case constructorID = "constructorId"
case url
case name
Expand Down
57 changes: 29 additions & 28 deletions Sources/Formula1API/Models/DriverStandings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@

import Foundation

/// Codable struct, used for serializing JSON from the DriverStandings endpoint.
public struct DriverStandings: Codable {
let data: DriverStandingsData
public let data: DriverStandingsData

enum CodingKeys: String, CodingKey {
private enum CodingKeys: String, CodingKey {
case data = "MRData"
}
}

// MARK: - MRData
struct DriverStandingsData: Codable {
let series: String
let url: String
let limit: String
let offset: String
let total: String
let standingsTable: StandingsTable
public struct DriverStandingsData: Codable {
public let series: String
public let url: String
public let limit: String
public let offset: String
public let total: String
public let standingsTable: StandingsTable

enum CodingKeys: String, CodingKey {
private enum CodingKeys: String, CodingKey {
case series
case url
case limit
Expand All @@ -34,35 +34,36 @@ struct DriverStandingsData: Codable {
}
}

struct StandingsTable: Codable {
let season: String
let standingsLists: [StandingsList]
public struct StandingsTable: Codable {
public let season: String?
public let standingsLists: [StandingsList]

enum CodingKeys: String, CodingKey {
private enum CodingKeys: String, CodingKey {
case season
case standingsLists = "StandingsLists"
}
}

struct StandingsList: Codable {
let season, round: String
let driverStandings: [DriverStanding]
public struct StandingsList: Codable {
public let season, round: String
public let driverStandings: [DriverStanding]

enum CodingKeys: String, CodingKey {
case season, round
private enum CodingKeys: String, CodingKey {
case season
case round
case driverStandings = "DriverStandings"
}
}

struct DriverStanding: Codable {
let position: String
let positionText: String
let points: String
let wins: String
let driver: Driver
let constructors: [Constructor]
public struct DriverStanding: Codable {
public let position: String
public let positionText: String
public let points: String
public let wins: String
public let driver: Driver
public let constructors: [Constructor]

enum CodingKeys: String, CodingKey {
private enum CodingKeys: String, CodingKey {
case position
case positionText
case points
Expand Down
29 changes: 15 additions & 14 deletions Sources/Formula1API/Models/Drivers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@

import Foundation

/// Codable struct, used for serializing JSON from the Drivers endpoint.
public struct Drivers: Codable {
let data: DriversData
public let data: DriversData

enum CodingKeys: String, CodingKey {
private enum CodingKeys: String, CodingKey {
case data = "MRData"
}
}

struct DriversData: Codable {
let series: String
let url: String
let limit: String
let offset: String
let total: String
let driverTable: DriverTable
public struct DriversData: Codable {
public let series: String
public let url: String
public let limit: String
public let offset: String
public let total: String
public let driverTable: DriverTable

enum CodingKeys: String, CodingKey {
private enum CodingKeys: String, CodingKey {
case series
case url
case limit
Expand All @@ -33,11 +34,11 @@ struct DriversData: Codable {
}
}

struct DriverTable: Codable {
let season: String
let drivers: [Driver]
public struct DriverTable: Codable {
public let season: String?
public let drivers: [Driver]

enum CodingKeys: String, CodingKey {
private enum CodingKeys: String, CodingKey {
case season
case drivers = "Drivers"
}
Expand Down
Loading

0 comments on commit a778c64

Please sign in to comment.