Skip to content

Commit

Permalink
Add 'fullart', 'gravestone' frame effects, improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobHearst committed Aug 18, 2023
1 parent c0a0e05 commit 25c54c4
Show file tree
Hide file tree
Showing 16 changed files with 117 additions and 62 deletions.
5 changes: 0 additions & 5 deletions .swiftpm/xcode/xcshareddata/xcschemes/ScryfallKit.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@
BlueprintName = "ScryfallKitTests"
ReferencedContainer = "container:">
</BuildableReference>
<SkippedTests>
<Test
Identifier = "SmokeTests/testAllNewCards()">
</Test>
</SkippedTests>
</TestableReference>
</Testables>
</TestAction>
Expand Down
24 changes: 11 additions & 13 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
{
"object": {
"pins": [
{
"package": "SwiftDocCPlugin",
"repositoryURL": "https://github.com/apple/swift-docc-plugin",
"state": {
"branch": null,
"revision": "3303b164430d9a7055ba484c8ead67a52f7b74f6",
"version": "1.0.0"
}
"pins" : [
{
"identity" : "swift-docc-plugin",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-docc-plugin",
"state" : {
"revision" : "3303b164430d9a7055ba484c8ead67a52f7b74f6",
"version" : "1.0.0"
}
]
},
"version": 1
}
],
"version" : 2
}
5 changes: 3 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ let package = Package(
targets: ["ScryfallKit"])
],
dependencies: [
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0")
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
],
targets: [
.target(
name: "ScryfallKit",
dependencies: []),
dependencies: [
]),
.testTarget(
name: "ScryfallKitTests",
dependencies: ["ScryfallKit"])
Expand Down
7 changes: 6 additions & 1 deletion Sources/ScryfallKit/Extensions/Card+helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import Foundation
import OSLog

public extension Card {
/// Get the legality of a card in a given format
Expand Down Expand Up @@ -82,7 +83,11 @@ public extension Card {
}

guard let uri = uris.uri(for: type) else {
print("No URI for image type \(type)")
if #available(iOS 14.0, macOS 11.0, *) {
Logger.main.error("No URI for image type \(type.rawValue)")
} else {
print("No URI for image type \(type)")
}
return nil
}

Expand Down
16 changes: 16 additions & 0 deletions Sources/ScryfallKit/Logger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// Logger.swift
//

import Foundation
import OSLog

@available(macOS 11.0, *)
@available(iOS 14.0, *)
extension Logger {
static let subsystem = "dev.hearst.scryfallkit"
static let main = Logger(subsystem: subsystem, category: "ScryfallKit")
static let client = Logger(subsystem: subsystem, category: "ScryfallClient")
static let network = Logger(subsystem: subsystem, category: "Network")
static let decoder = Logger(subsystem: subsystem, category: "Decoder")
}
21 changes: 16 additions & 5 deletions Sources/ScryfallKit/Models/Card/Card+enums.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//

import Foundation
import OSLog

extension Card {
/// A value or combination of values that uniquely identify a Magic card
Expand Down Expand Up @@ -138,7 +139,11 @@ extension Card {
public init(from decoder: Decoder) throws {
self = (try? Self(rawValue: decoder.singleValueContainer().decode(RawValue.self))) ?? .unknown
if self == .unknown, let rawValue = try? String(from: decoder) {
print("Decoded unknown FrameEffect: \(rawValue)")
if #available(iOS 14.0, macOS 11.0, *) {
Logger.decoder.error("Decoded unknown Layout: \(rawValue)")
} else {
print("Decoded unknown Layout: \(rawValue)")
}
}
}
}
Expand All @@ -156,8 +161,10 @@ extension Card {

public var label: String {
switch self {
case .notLegal: return "Not Legal"
default: return rawValue.capitalized
case .notLegal:
return "Not Legal"
default:
return rawValue.capitalized
}
}
}
Expand Down Expand Up @@ -193,12 +200,16 @@ extension Card {
///
/// [Scryfall documentation](https://scryfall.com/docs/api/frames#frame-effects)
public enum FrameEffect: String, Codable, CaseIterable {
case legendary, miracle, nyxtouched, draft, devoid, tombstone, colorshifted, inverted, sunmoondfc, compasslanddfc, originpwdfc, mooneldrazidfc, waxingandwaningmoondfc, showcase, extendedart, companion, etched, snow, lesson, convertdfc, fandfc, battle, unknown
case legendary, miracle, nyxtouched, draft, devoid, tombstone, colorshifted, inverted, sunmoondfc, compasslanddfc, originpwdfc, mooneldrazidfc, waxingandwaningmoondfc, showcase, extendedart, companion, etched, snow, lesson, convertdfc, fandfc, battle, gravestone, fullart, unknown

public init(from decoder: Decoder) throws {
self = try FrameEffect(rawValue: decoder.singleValueContainer().decode(RawValue.self)) ?? .unknown
if self == .unknown, let rawValue = try? String(from: decoder) {
print("Decoded unknown FrameEffect: \(rawValue)")
if #available(iOS 14.0, macOS 11.0, *) {
Logger.decoder.error("Decoded unknown FrameEffect: \(rawValue)")
} else {
print("Decoded unknown FrameEffect: \(rawValue)")
}
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion Sources/ScryfallKit/Models/MTGSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//

import Foundation
import OSLog

/// A set represents a group of related Magic cards
///
Expand Down Expand Up @@ -42,7 +43,11 @@ public struct MTGSet: Codable, Identifiable, Hashable {
public init(from decoder: Decoder) throws {
self = try Self(rawValue: decoder.singleValueContainer().decode(RawValue.self)) ?? .unknown
if self == .unknown, let rawValue = try? String(from: decoder) {
print("Decoded unknown MTGSet Type: \(rawValue)")
if #available(iOS 14.0, macOS 11.0, *) {
Logger.main.warning("Decoded unknown MTGSet Type: \(rawValue)")
} else {
print("Decoded unknown MTGSet Type: \(rawValue)")
}
}
}
}
Expand Down
28 changes: 19 additions & 9 deletions Sources/ScryfallKit/Networking/EndpointRequests/CardRequests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
//

import Foundation
import OSLog

struct SearchCards: EndpointRequest {
var body: Data?
var requestMethod: RequestMethod = .GET
var path: String? = "cards/search"
var path = "cards/search"
var queryParams: [URLQueryItem]

init(query: String,
Expand Down Expand Up @@ -35,7 +36,7 @@ struct SearchCards: EndpointRequest {
struct GetCardNamed: EndpointRequest {
var body: Data?
var requestMethod: RequestMethod = .GET
var path: String? = "cards/named"
var path = "cards/named"
var queryParams: [URLQueryItem]

init(exact: String? = nil, fuzzy: String? = nil, set: String? = nil) {
Expand All @@ -50,7 +51,7 @@ struct GetCardNamed: EndpointRequest {
struct GetCardAutocomplete: EndpointRequest {
var body: Data?
var requestMethod: RequestMethod = .GET
var path: String? = "cards/autocomplete"
var path = "cards/autocomplete"
var queryParams: [URLQueryItem]

init(query: String, includeExtras: Bool? = nil) {
Expand All @@ -64,7 +65,7 @@ struct GetCardAutocomplete: EndpointRequest {
struct GetRandomCard: EndpointRequest {
var body: Data?
var requestMethod: RequestMethod = .GET
var path: String? = "cards/random"
var path = "cards/random"
var queryParams: [URLQueryItem]

init(query: String?) {
Expand All @@ -77,7 +78,7 @@ struct GetRandomCard: EndpointRequest {
struct GetCard: EndpointRequest {
let identifier: Card.Identifier

var path: String? {
var path: String {
switch identifier {
case .scryfallID(let id):
return "cards/\(id)"
Expand All @@ -88,8 +89,13 @@ struct GetCard: EndpointRequest {
default:
// This guard should never trip. The only card identifier that doesn't have provider/id is the set code/collector
guard let id = identifier.id else {
print("Provided identifier doesn't have a provider or doesn't have an id")
return nil
if #available(iOS 14.0, macOS 11.0, *) {
Logger.main.error("Provided identifier doesn't have a provider or doesn't have an id")
} else {
print("Provided identifier doesn't have a provider or doesn't have an id")
}

fatalError("Encountered a situation that shouldn't be possible: Card identifier's id property was nil")
}

return "cards/\(identifier.provider)/\(id)"
Expand All @@ -102,7 +108,7 @@ struct GetCard: EndpointRequest {
}

struct GetCardCollection: EndpointRequest {
var path: String? = "cards/collection"
var path = "cards/collection"
var queryParams: [URLQueryItem] = []
var requestMethod: RequestMethod = .POST
var body: Data?
Expand All @@ -114,7 +120,11 @@ struct GetCardCollection: EndpointRequest {
do {
body = try JSONSerialization.data(withJSONObject: requestBody)
} catch {
print("Errored serializing dict to JSON for GetCardCollection request")
if #available(iOS 14.0, macOS 11.0, *) {
Logger.main.error("Errored serializing dict to JSON for GetCardCollection request")
} else {
print("Errored serializing dict to JSON for GetCardCollection request")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Foundation
struct GetCatalog: EndpointRequest {
var catalogType: Catalog.`Type`

var path: String? {
var path: String {
return "catalog/\(catalogType.rawValue)"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@
//

import Foundation
import OSLog

protocol EndpointRequest {
var path: String? { get }
var path: String { get }
var queryParams: [URLQueryItem] { get }
var requestMethod: RequestMethod { get }
var body: Data? { get }
}

extension EndpointRequest {
var urlRequest: URLRequest? {
guard let path = path else {
print("Couldn't get path for request")
return nil
}

var urlComponents = URLComponents(string: "https://api.scryfall.com/\(path)")
if !queryParams.isEmpty {
urlComponents?.queryItems = queryParams.compactMap { $0.value == nil ? nil : $0 }
}

guard let url = urlComponents?.url else {
print("Couldn't make url")
if #available(iOS 14.0, macOS 11.0, *) {
Logger.main.error("Couldn't make url")
} else {
print("Couldn't make url")
}
return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Foundation
struct GetRulings: EndpointRequest {
var identifier: Card.Ruling.Identifier

var path: String? {
var path: String {
switch identifier {
case .multiverseID(let id):
return "cards/multiverse/\(id)/rulings"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import Foundation

struct GetSets: EndpointRequest {
var path: String? = "sets"
var path = "sets"
var queryParams: [URLQueryItem] = []
var requestMethod: RequestMethod = .GET
var body: Data?
Expand All @@ -14,7 +14,7 @@ struct GetSets: EndpointRequest {
struct GetSet: EndpointRequest {
var identifier: MTGSet.Identifier

var path: String? {
var path: String {
switch self.identifier {
case .code(let code):
return "sets/\(code)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import Foundation

struct GetSymbology: EndpointRequest {
var path: String? = "symbology"
var path = "symbology"
var queryParams: [URLQueryItem] = []
var requestMethod: RequestMethod = .GET
var body: Data?
Expand All @@ -16,7 +16,7 @@ struct ParseManaCost: EndpointRequest {
var cost: String

// Protocol vars
var path: String? = "symbology/parse-mana"
var path = "symbology/parse-mana"
var queryParams: [URLQueryItem]
var requestMethod: RequestMethod = .GET
var body: Data?
Expand Down
Loading

0 comments on commit 25c54c4

Please sign in to comment.