-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from 417-72KI/config-folder-composition
Change folder composition in config directory
- Loading branch information
Showing
37 changed files
with
457 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// BaseEntities.swift | ||
// BuildConfigSwiftDemo | ||
// | ||
// Created by 417.72KI on 2023/10/05. | ||
// Copyright © 2023 417.72KI. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol Request: Encodable, Hashable { | ||
} | ||
|
||
protocol Response: Decodable, Hashable { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// LoginRequest.swift | ||
// BuildConfigSwiftDemo | ||
// | ||
// Created by 417.72KI on 2023/10/05. | ||
// Copyright © 2023 417.72KI. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
struct LoginRequest: Request { | ||
var id: String | ||
var password: String | ||
} |
14 changes: 14 additions & 0 deletions
14
Demo/BuildConfigSwiftDemo/Sources/Data/LoginResponse.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// LoginResponse.swift | ||
// BuildConfigSwiftDemo | ||
// | ||
// Created by 417.72KI on 2023/10/05. | ||
// Copyright © 2023 417.72KI. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
struct LoginResponse: Response { | ||
var accessToken: String | ||
var refreshToken: String | ||
} |
19 changes: 19 additions & 0 deletions
19
Demo/BuildConfigSwiftDemo/Sources/Data/SearchResponse.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// SearchResponse.swift | ||
// BuildConfigSwiftDemo | ||
// | ||
// Created by 417.72KI on 2023/10/05. | ||
// Copyright © 2023 417.72KI. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
struct SearchResponse: Response { | ||
var items: [Item] | ||
} | ||
|
||
extension SearchResponse { | ||
struct Item: Response { | ||
var name: String | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
Demo/BuildConfigSwiftDemo/Sources/Repository/APIClient.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// | ||
// APIClient.swift | ||
// BuildConfigSwiftDemo | ||
// | ||
// Created by 417.72KI on 2023/10/05. | ||
// Copyright © 2023 417.72KI. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
struct APIClient { | ||
var config: BuildConfig.Api | ||
var session: URLSession | ||
} | ||
|
||
extension APIClient { | ||
var host: URL { URL(string: "https://\(config.host)")! } | ||
|
||
var decoder: JSONDecoder { | ||
let decoder = JSONDecoder() | ||
decoder.keyDecodingStrategy = .convertFromSnakeCase | ||
return decoder | ||
} | ||
|
||
var encoder: JSONEncoder { | ||
let encoder = JSONEncoder() | ||
encoder.keyEncodingStrategy = .convertToSnakeCase | ||
return encoder | ||
} | ||
} | ||
|
||
extension APIClient { | ||
func endpoint<E>(_ keyPath: KeyPath<BuildConfig.Api.Endpoint, E>) -> E { | ||
config.endpoint[keyPath: keyPath] | ||
} | ||
} | ||
|
||
extension APIClient { | ||
func login(id: String, password: String) async throws -> LoginResponse { | ||
let endpoint = endpoint(\.login) | ||
let url = host.appendingPathComponent(endpoint.path) | ||
var request = URLRequest(url: url) | ||
request.httpMethod = endpoint.method | ||
request.httpBody = try encoder.encode(LoginRequest(id: id, password: password)) | ||
let (data, _) = try await session.data(for: request) | ||
return try decoder.decode(LoginResponse.self, from: data) | ||
} | ||
|
||
@available(iOS 16.0, *) | ||
func search(_ text: String) async throws -> SearchResponse { | ||
let endpoint = endpoint(\.search) | ||
let url = host.appendingPathComponent(endpoint.path) | ||
.appending(queryItems: [ | ||
URLQueryItem(name: "text", value: text) | ||
]) | ||
var request = URLRequest(url: url) | ||
request.httpMethod = endpoint.method | ||
let (data, _) = try await session.data(for: request) | ||
return try decoder.decode(SearchResponse.self, from: data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
Demo/BuildConfigSwiftDemo/Sources/Util/BuildConfig+Environment.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// BuildConfig+Environment.swift | ||
// BuildConfigSwiftDemo | ||
// | ||
// Created by 417.72KI on 2023/10/09. | ||
// Copyright © 2023 417.72KI. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct BuildConfigKey: EnvironmentKey { | ||
static let defaultValue = BuildConfig.default | ||
} | ||
|
||
extension EnvironmentValues { | ||
var buildConfig: BuildConfig { | ||
get { self[BuildConfigKey.self] } | ||
set { self[BuildConfigKey.self] = newValue } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// main.swift | ||
// BuildConfigSwiftDemo | ||
// | ||
// Created by 417.72KI on 2023/10/09. | ||
// Copyright © 2023 417.72KI. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
if let clazz = NSClassFromString("FakeApp") as? any App.Type { | ||
clazz.main() | ||
} else { | ||
DemoApp.main() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// | ||
// APIClientTests.swift | ||
// BuildConfigSwiftDemoTests | ||
// | ||
// Created by 417.72KI on 2023/10/05. | ||
// Copyright © 2023 417.72KI. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
import StubNetworkKit | ||
|
||
@testable import BuildConfigSwiftDemo | ||
|
||
final class APIClientTests: XCTestCase { | ||
var config = BuildConfig.fake.api | ||
|
||
var apiClient: APIClient! | ||
|
||
override func setUpWithError() throws { | ||
apiClient = APIClient(config: config, | ||
session: defaultStubSession) | ||
} | ||
|
||
override func tearDownWithError() throws { | ||
clearStubs() | ||
} | ||
|
||
func testLogin() async throws { | ||
stub { | ||
Scheme.is("https") | ||
Host.is("localhost") | ||
Path.is("/login") | ||
Method.isPost() | ||
Body.isJson(["id": "john_doe", "password": "password"]) | ||
}.responseJson(["access_token": "foo", "refresh_token": "bar"]) | ||
|
||
let response = try await apiClient.login(id: "john_doe", | ||
password: "password") | ||
XCTAssertEqual("foo", response.accessToken) | ||
XCTAssertEqual("bar", response.refreshToken) | ||
} | ||
|
||
@available(iOS 16.0, *) | ||
func testSearch() async throws { | ||
stub { | ||
Scheme.is("https") | ||
Host.is("localhost") | ||
Path.is("/search") | ||
Method.isGet() | ||
QueryParams.contains(["text": "寿限無"]) | ||
}.responseJson([ | ||
"items": [ | ||
["name": "foo"], | ||
["name": "bar"], | ||
["name": "baz"], | ||
] | ||
]) | ||
|
||
let response = try await apiClient.search("寿限無") | ||
XCTAssertEqual(3, response.items.count) | ||
XCTAssertEqual("foo", response.items.first?.name) | ||
XCTAssertEqual("baz", response.items.last?.name) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// FakeApp.swift | ||
// BuildConfigSwiftDemoTests | ||
// | ||
// Created by 417.72KI on 2023/10/09. | ||
// Copyright © 2023 417.72KI. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
@objc(FakeApp) | ||
final class FakeApp: NSObject, App { | ||
override init() { super.init() } | ||
|
||
var body: some Scene { | ||
WindowGroup { | ||
Text("This is a fake app.") | ||
.foregroundStyle(Color.white) | ||
.frame(maxWidth: .infinity, maxHeight: .infinity) | ||
.background { | ||
Color.black | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// | ||
// TestHelper.swift | ||
// BuildConfigSwiftDemoTests | ||
// | ||
// Created by 417.72KI on 2023/10/05. | ||
// Copyright © 2023 417.72KI. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
@testable import BuildConfigSwiftDemo | ||
|
||
final class TestHelper { | ||
private init() {} | ||
} | ||
|
||
extension TestHelper { | ||
static var bundle: Bundle { Bundle(for: self.self) } | ||
} | ||
|
||
extension TestHelper { | ||
static func path(forResource name: String, ofType ext: String) -> String? { | ||
bundle.path(forResource: name, ofType: ext) | ||
} | ||
} | ||
|
||
extension BuildConfig { | ||
static var fake: Self { | ||
.load(from: TestHelper.path(forResource: "test_config", ofType: "json")!) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"api": { | ||
"version": 100, | ||
"host": "localhost", | ||
"endpoint": { | ||
"login": { | ||
"path": "/login", | ||
"method": "POST" | ||
}, | ||
"profile": { | ||
"path": "/profile", | ||
"method": "GET" | ||
}, | ||
"search": { | ||
"path": "/search", | ||
"method": "GET" | ||
} | ||
} | ||
}, | ||
"environment": "staging", | ||
"is_debug": false, | ||
"pi": 3.14 | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.