Skip to content

Commit

Permalink
Add with-top-coins flag to coin categories
Browse files Browse the repository at this point in the history
  • Loading branch information
ant013 committed Feb 11, 2025
1 parent c9c3868 commit 235ef7d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 22 deletions.
4 changes: 2 additions & 2 deletions Sources/MarketKit/Classes/Kit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ public extension Kit {

// Categories

func coinCategories(currencyCode: String) async throws -> [CoinCategory] {
try await hsProvider.coinCategories(currencyCode: currencyCode)
func coinCategories(currencyCode: String, withTopCoins: Bool = false) async throws -> [CoinCategory] {
try await hsProvider.coinCategories(currencyCode: currencyCode, withTopCoins: withTopCoins)
}

func coinCategoryMarketCapChart(category: String, currencyCode: String?, timePeriod: HsTimePeriod) async throws -> [CategoryMarketPoint] {
Expand Down
9 changes: 6 additions & 3 deletions Sources/MarketKit/Classes/Models/CoinCategory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ import Foundation
import ObjectMapper

public class CoinCategory: ImmutableMappable {
public let id: Int
public let id: Int?
public let uid: String
public let name: String
public let descriptions: [String: String]
public let marketCap: Decimal?
public let diff24H: Decimal?
public let diff1W: Decimal?
public let diff1M: Decimal?
public let topCoins: [String]?

public required init(map: Map) throws {
id = try map.value("id")
id = try? map.value("id")
uid = try map.value("uid")
name = try map.value("name")
descriptions = try map.value("description")
Expand All @@ -21,6 +22,8 @@ public class CoinCategory: ImmutableMappable {
diff24H = try? map.value("change_24h", using: Transform.stringToDecimalTransform)
diff1W = try? map.value("change_1w", using: Transform.stringToDecimalTransform)
diff1M = try? map.value("change_1m", using: Transform.stringToDecimalTransform)

topCoins = try? map.value("top_coins")
}
}

Expand All @@ -38,6 +41,6 @@ public extension CoinCategory {

extension CoinCategory: CustomStringConvertible {
public var description: String {
"CoinCategory [uid: \(uid); id: \(id); name: \(name); descriptionCount: \(descriptions.count)]"
"CoinCategory [uid: \(uid); id: \(id ?? -1); name: \(name); descriptionCount: \(descriptions.count)]"
}
}
2 changes: 1 addition & 1 deletion Sources/MarketKit/Classes/Models/CoinPriceResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct CoinPriceResponse: ImmutableMappable {
let priceChange1d: Decimal?
let lastUpdated: TimeInterval

init(uid: String, price: Decimal, priceChange: Decimal?, priceChange24h: Decimal?, priceChange1d: Decimal?, lastUpdated: TimeInterval) {
init(uid: String, price: Decimal, priceChange _: Decimal?, priceChange24h: Decimal?, priceChange1d: Decimal?, lastUpdated: TimeInterval) {
self.uid = uid
self.price = price
self.priceChange24h = priceChange24h
Expand Down
28 changes: 14 additions & 14 deletions Sources/MarketKit/Classes/Models/HsTimePeriod.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@ public enum HsTimePeriod: String, CaseIterable {

private var range: TimeInterval {
switch self {
case .hour24: return .days(1)
case .day1: return 0
case .week1: return .days(7)
case .week2: return .days(14)
case .month1: return .days(30)
case .month3: return .days(90)
case .month6: return .days(180)
case .year1: return .days(365)
case .year2: return 2 * .days(365)
case .year5: return 5 * .days(365)
case .hour24: return .days(1)
case .day1: return 0
case .week1: return .days(7)
case .week2: return .days(14)
case .month1: return .days(30)
case .month3: return .days(90)
case .month6: return .days(180)
case .year1: return .days(365)
case .year2: return 2 * .days(365)
case .year5: return 5 * .days(365)
}
}

var startTimestamp: TimeInterval {
switch self {
case .day1:
return .midnightUTC() + .minutes(1)
default:
return Date().timeIntervalSince1970 - range
case .day1:
return .midnightUTC() + .minutes(1)
default:
return Date().timeIntervalSince1970 - range
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions Sources/MarketKit/Classes/Providers/HsProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,17 @@ extension HsProvider {

// Coin Categories

func coinCategories(currencyCode: String? = nil) async throws -> [CoinCategory] {
func coinCategories(currencyCode: String? = nil, withTopCoins: Bool = false) async throws -> [CoinCategory] {
var parameters: Parameters = [:]
if let currencyCode {
parameters["currency"] = currencyCode.lowercased()
}

return try await networkManager.fetch(url: "\(baseUrl)/v1/categories", method: .get, parameters: parameters, headers: headers)
var url = "\(baseUrl)/v1/categories"
if withTopCoins {
url += "/with-top-coins"
}
return try await networkManager.fetch(url: url, method: .get, parameters: parameters, headers: headers)
}

func coinCategoryMarketCapChart(category: String, currencyCode: String?, timePeriod: HsTimePeriod) async throws -> [CategoryMarketPoint] {
Expand Down

0 comments on commit 235ef7d

Please sign in to comment.