Skip to content

Commit

Permalink
Change to Decodable, Remove Cheetah, Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pepijn98 committed Nov 20, 2019
1 parent 1e48864 commit 14ebcfd
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 37 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,5 @@ fastlane/test_output

# IDE
.idea/

.env
9 changes: 9 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@
"revision": "ccf96bbe65ecc7c1558ab0dba7ffabdea5c1d31f",
"version": "2.4.4"
}
},
{
"package": "SwiftDotEnv",
"repositoryURL": "https://github.com/SwiftOnTheServer/SwiftDotEnv.git",
"state": {
"branch": null,
"revision": "2a7527210766fc726fb3d26fa9b8f295841e73df",
"version": "2.0.1"
}
}
]
},
Expand Down
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ let package = Package(
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
// .package(url: "https://github.com/OpenKitten/Cheetah.git", from: "2.0.3-swift5"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.8.0"),
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.0.0"),
.package(url: "https://github.com/SwiftOnTheServer/SwiftDotEnv.git", from: "2.0.1"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand All @@ -26,6 +26,6 @@ let package = Package(
dependencies: ["AsyncHTTPClient"]),
.testTarget(
name: "AzurLaneTests",
dependencies: ["AzurLane"]),
dependencies: ["AzurLane", "SwiftDotEnv"]),
]
)
2 changes: 1 addition & 1 deletion Sources/AzurLane/AzurLane.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public struct Options {
}

public struct AzurLane {
public static let version = "1.2.0"
public static let version = "1.2.1"
private let client: Client

public init(_ options: Options) {
Expand Down
20 changes: 1 addition & 19 deletions Sources/AzurLane/Client.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Foundation
// import Cheetah
import AsyncHTTPClient
import NIOHTTP1

Expand All @@ -8,17 +7,6 @@ internal struct Client {
var token = ""
let endpoints = Endpoints()
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
// Seems like Cheetah.JSONDecoder() has issues decoding the returned json into my structs atm
//
// <compiler-generated>:0: error: undefined reference to '$s7Cheetah11JSONDecoderCMa'
// <compiler-generated>:0: error: undefined reference to '$s7Cheetah11JSONDecoderCACycfC'
// /media/pepijn/Data/Projects/Swift/AzurLane/Sources/AzurLane/Client.swift:11: error: undefined reference to '$s7Cheetah11JSONDecoderCMa'
// /media/pepijn/Data/Projects/Swift/AzurLane/Sources/AzurLane/Client.swift:11: error: undefined reference to '$s7Cheetah11JSONDecoderCACycfC'
// clang-7: error: linker command failed with exit code 1 (use -v to see invocation)
// :0: error: link command failed with exit code 1 (use -v to see invocation)
// [2/3] Linking AzurLanePackageTests.xctest
//
// let decoder = Cheetah.JSONDecoder()
let decoder = JSONDecoder()

init(_ options: Options) {
Expand Down Expand Up @@ -68,22 +56,16 @@ internal struct Client {
return
}

// guard let data = String(bytes: bytes, encoding: .utf8) else {
// completion(.failure(AzurLaneAPIError(reason: .decodeError, message: "Failed converting bytes to string")))
// return
// }

guard 200..<299 ~= response.status.code else {
let error = try self.decoder.decode(ErrorResponse.self, from: Data(bytes))
// let error = try self.decoder.decode(ErrorResponse.self, from: data)
completion(.failure(AzurLaneAPIError(reason: .invalidResponse, message: error.message)))
return
}

let values = try self.decoder.decode(T.self, from: Data(bytes))
// let values = try self.decoder.decode(T.self, from: data)
completion(.success(values))
} catch {
// print("Unexpected error: \(error).")
completion(.failure(AzurLaneAPIError(reason: .decodeError)))
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/AzurLane/Responses/BuildResponse.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
public struct Construction: Codable {
public struct Construction: Decodable {
public let time: String
public let wikiUrl: String
public let ships: [String]
}

public struct BuildResponse: Codable {
public struct BuildResponse: Decodable {
public let statusCode: Int
public let statusMessage: String
public let message: String
Expand Down
2 changes: 1 addition & 1 deletion Sources/AzurLane/Responses/ErrorResponse.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public struct ErrorResponse: Codable {
public struct ErrorResponse: Decodable {
public let statusCode: Int
public let statusMessage: String
public let message: String
Expand Down
18 changes: 9 additions & 9 deletions Sources/AzurLane/Responses/ShipResponse.swift
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
public struct Names: Codable {
public struct Names: Decodable {
public let en: String?
public let cn: String?
public let jp: String?
public let kr: String?
}

public struct Skin: Codable {
public struct Skin: Decodable {
public let title: String?
public let image: String?
public let chibi: String?
}

public struct Stars: Codable {
public struct Stars: Decodable {
public let value: String?
public let count: Int
}

public struct Stat: Codable {
public struct Stat: Decodable {
public let name: String?
public let image: String?
public let value: String?
}

public struct Stats: Codable {
public struct Stats: Decodable {
public let level100: [Stat]?
public let level120: [Stat]?
public let base: [Stat]?
public let retrofit100: [Stat]?
public let retrofit120: [Stat]?
}

public struct MiscellaneousData: Codable {
public struct MiscellaneousData: Decodable {
public let link: String?
public let name: String?
}

public struct Miscellaneous: Codable {
public struct Miscellaneous: Decodable {
public let artist: MiscellaneousData?
public let web: MiscellaneousData?
public let pixiv: MiscellaneousData?
public let twitter: MiscellaneousData?
public let voiceActerss: MiscellaneousData?
}

public struct Ship: Codable {
public struct Ship: Decodable {
public let wikiUrl: String
public let id: String?
public let names: Names
Expand All @@ -60,7 +60,7 @@ public struct Ship: Codable {
public let miscellaneous: Miscellaneous
}

public struct ShipResponse: Codable {
public struct ShipResponse: Decodable {
public let statusCode: Int
public let statusMessage: String
public let message: String
Expand Down
4 changes: 2 additions & 2 deletions Sources/AzurLane/Responses/ShipsResponse.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
public struct SmallShip: Codable, Equatable {
public struct SmallShip: Decodable, Equatable {
public let id: String
public let name: String
}

public struct ShipsResponse: Codable {
public struct ShipsResponse: Decodable {
public let statusCode: Int
public let statusMessage: String
public let message: String
Expand Down
9 changes: 8 additions & 1 deletion Tests/AzurLaneTests/AzurLaneTests.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import XCTest
import DotEnv
@testable import AzurLane

final class AzurLaneTests: XCTestCase {
let azurlane = AzurLane(Options(token: ""))
let env = DotEnv()
let azurlane: AzurLane

public required init(name: String, testClosure: @escaping XCTestCaseClosure) {
azurlane = AzurLane(Options(token: env.get("token") ?? ""))
super.init(name: name, testClosure: testClosure)
}

func testGetShipByName() throws {
// This is an example of a functional test case.
Expand Down

0 comments on commit 14ebcfd

Please sign in to comment.