Skip to content

Commit 673695d

Browse files
committed
feat: improve NetworkService equally with MangaKu
1 parent b2f4bac commit 673695d

File tree

4 files changed

+74
-19
lines changed

4 files changed

+74
-19
lines changed

Modules/Common/Common/Sources/Common/Data/API/APIFactory.swift

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,40 @@
88
import Foundation
99

1010
public protocol APIFactory {
11-
var path: String { get }
12-
var parameter: [String: Any] { get }
13-
var baseURL: String { get }
14-
15-
var composedURL: URL { get }
11+
var path: String { get }
12+
var parameters: [String: Any] { get }
13+
var baseURL: String { get }
14+
var method: HTTPMethod { get }
15+
var headers: [String: String] { get }
16+
var bodyData: Data? { get }
17+
18+
var composedURL: URL { get }
1619
}
1720

1821
public extension APIFactory {
19-
var composedURL: URL {
20-
let params = parameter.map({ "\($0.key)=\($0.value)" }).joined(separator: "&")
21-
let urlString = baseURL.appending(path)
22-
.appending("?")
23-
.appending(params)
24-
return URL(string: urlString) ?? URL.init(fileURLWithPath: "")
25-
}
22+
var composedURL: URL {
23+
let params = parameters.map({ "\($0.key)=\($0.value)" }).joined(separator: "&")
24+
let urlString = baseURL.appending(path)
25+
.appending("?")
26+
.appending(params)
27+
return URL(string: urlString) ?? URL.init(fileURLWithPath: "")
28+
}
29+
}
30+
31+
public enum HTTPMethod {
32+
case get
33+
case post(body: Data? = nil)
34+
case put
35+
case delete
36+
case patch
37+
38+
var value: String {
39+
switch self {
40+
case .get: return "GET"
41+
case .post: return "POST"
42+
case .put: return "PUT"
43+
case .delete: return "DELETE"
44+
case .patch: return "PATCH"
45+
}
46+
}
2647
}

Modules/Common/Common/Sources/Common/Data/API/GiphyAPI.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ extension GiphyAPI: APIFactory {
2929
}
3030
}
3131

32-
public var parameter: [String: Any] {
32+
public var parameters: [String: Any] {
3333
var defaultParams: [String: Any] = ["api_key": APIConfig.giphyConfig.apiKey]
3434
switch self {
3535
case .search(let query) where query.count > 0:
@@ -39,4 +39,16 @@ extension GiphyAPI: APIFactory {
3939
}
4040
return defaultParams
4141
}
42+
43+
public var method: HTTPMethod {
44+
return .get // assuming all are GET requests
45+
}
46+
47+
public var headers: [String: String] {
48+
[:] // or provide default headers if needed
49+
}
50+
51+
public var bodyData: Data? {
52+
nil // not used in GET requests
53+
}
4254
}

Modules/Common/Common/Sources/Common/Data/API/NetworkService.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// NetworkService.swift
3-
//
3+
//
44
//
55
// Created by Uwais Alqadri on 10/17/21.
66
//
@@ -11,18 +11,28 @@ import Alamofire
1111

1212
public class NetworkService {
1313
public static let shared = NetworkService()
14-
14+
1515
public func connect<T: Codable>(api: APIFactory, responseType: T.Type) async throws -> T {
1616
return try await withCheckedThrowingContinuation { continuation in
17-
AF.request(api.composedURL)
17+
var urlRequest = URLRequest(url: api.composedURL)
18+
urlRequest.httpMethod = api.method.value
19+
urlRequest.allHTTPHeaderFields = api.headers
20+
urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
21+
22+
if let body = api.bodyData {
23+
urlRequest.httpBody = body
24+
}
25+
26+
AF.request(urlRequest)
27+
.validate()
1828
.prettyPrintedJsonResponse(of: responseType)
1929
.responseDecodable(of: responseType) { response in
2030
switch response.result {
2131
case .success(let data):
22-
32+
2333
print("[NETWORK][\(response.response?.statusCode ?? 0)] \(api)")
2434
continuation.resume(with: .success(data))
25-
35+
2636
case .failure(let error):
2737
continuation.resume(with: .failure(error))
2838
print("[NETWORK][\(response.response?.statusCode ?? 0)] \(error)")

Modules/Common/Common/Sources/Common/Data/API/TenorAPI.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extension TenorAPI: APIFactory {
1919
}
2020
}
2121

22-
public var parameter: [String: Any] {
22+
public var parameters: [String: Any] {
2323
var defaultParams: [String: Any] = ["key": APIConfig.tenorConfig.apiKey]
2424
switch self {
2525
case let .search(query, limit) where query.count > 0:
@@ -34,4 +34,16 @@ extension TenorAPI: APIFactory {
3434
public var baseURL: String {
3535
APIConfig.tenorConfig.baseUrl
3636
}
37+
38+
public var method: HTTPMethod {
39+
return .get // assuming all are GET requests
40+
}
41+
42+
public var headers: [String: String] {
43+
[:] // or provide default headers if needed
44+
}
45+
46+
public var bodyData: Data? {
47+
nil // not used in GET requests
48+
}
3749
}

0 commit comments

Comments
 (0)