From aa498e753eb3a2d2729e14f19eb2ca57efdea463 Mon Sep 17 00:00:00 2001 From: Uwais Alqadri Date: Fri, 11 Oct 2024 19:48:13 +0700 Subject: [PATCH] feat: prepare tenor --- .../API/{Config.swift => APIConfig.swift} | 7 +-- .../Sources/Common/Data/API/APIFactory.swift | 51 +++---------------- .../Sources/Common/Data/API/GiphyAPI.swift | 50 ++++++++++++++++++ .../Common/Data/API/NetworkService.swift | 4 +- .../Sources/Common/Data/API/TenorAPI.swift | 40 +++++++++++++++ .../Remote/SearchRemoteDataSource.swift | 11 ++-- .../Remote/TrendingRemoteDataSource.swift | 2 +- 7 files changed, 113 insertions(+), 52 deletions(-) rename Modules/Common/Common/Sources/Common/Data/API/{Config.swift => APIConfig.swift} (76%) create mode 100644 Modules/Common/Common/Sources/Common/Data/API/GiphyAPI.swift create mode 100644 Modules/Common/Common/Sources/Common/Data/API/TenorAPI.swift diff --git a/Modules/Common/Common/Sources/Common/Data/API/Config.swift b/Modules/Common/Common/Sources/Common/Data/API/APIConfig.swift similarity index 76% rename from Modules/Common/Common/Sources/Common/Data/API/Config.swift rename to Modules/Common/Common/Sources/Common/Data/API/APIConfig.swift index 947421a..a5beae2 100644 --- a/Modules/Common/Common/Sources/Common/Data/API/Config.swift +++ b/Modules/Common/Common/Sources/Common/Data/API/APIConfig.swift @@ -7,9 +7,10 @@ import Foundation -struct Config { - static let baseUrl = "https://api.giphy.com/v1/gifs/" - +struct APIConfig { + static let giphyBaseUrl = "https://api.giphy.com/v1/gifs/" + static let tenorBaseUrl = "https://g.tenor.com/v1/" // coming soon + static var apiKey: String { guard let filePath = Bundle.main.path(forResource: "Info", ofType: "plist") else { fatalError("Couldn't find file 'Info.plist'.") diff --git a/Modules/Common/Common/Sources/Common/Data/API/APIFactory.swift b/Modules/Common/Common/Sources/Common/Data/API/APIFactory.swift index d348b3a..e6dc10d 100644 --- a/Modules/Common/Common/Sources/Common/Data/API/APIFactory.swift +++ b/Modules/Common/Common/Sources/Common/Data/API/APIFactory.swift @@ -1,50 +1,15 @@ // -// ApiFactory.swift -// +// APIFactory.swift // -// Created by Uwais Alqadri on 10/17/21. +// +// Created by Uwais Alqadri on 11/10/24. // import Foundation -public enum APIFactory { - case trending - case random - case search(query: String) -} - -public extension APIFactory { - var url: URL { - let params = parameter.map({ "\($0.key)=\($0.value)" }).joined(separator: "&") - let urlString = baseURL.appending(path) - .appending("?") - .appending(params) - return URL(string: urlString) ?? URL.init(fileURLWithPath: "") - } - - private var baseURL: String { - Config.baseUrl - } - - private var path: String { - switch self { - case .random: - return "random" - case .trending: - return "trending" - case .search: - return "search" - } - } - - private var parameter: [String: Any] { - var defaultParams = ["api_key": Config.apiKey] - switch self { - case .search(let query) where query.count > 0: - defaultParams["q"] = query - default: - break - } - return defaultParams - } +public protocol APIFactory { + var path: String { get } + var parameter: [String: Any] { get } + var composedURL: URL { get } + var baseURL: String { get } } diff --git a/Modules/Common/Common/Sources/Common/Data/API/GiphyAPI.swift b/Modules/Common/Common/Sources/Common/Data/API/GiphyAPI.swift new file mode 100644 index 0000000..7bce246 --- /dev/null +++ b/Modules/Common/Common/Sources/Common/Data/API/GiphyAPI.swift @@ -0,0 +1,50 @@ +// +// GiphyAPI.swift +// +// +// Created by Uwais Alqadri on 10/17/21. +// + +import Foundation + +public enum GiphyAPI { + case trending + case random + case search(query: String) +} + +extension GiphyAPI: APIFactory { + public var baseURL: String { + APIConfig.giphyBaseUrl + } + + public var path: String { + switch self { + case .random: + return "random" + case .trending: + return "trending" + case .search: + return "search" + } + } + + public var parameter: [String: Any] { + var defaultParams = ["api_key": APIConfig.apiKey] + switch self { + case .search(let query) where query.count > 0: + defaultParams["q"] = query + default: + break + } + return defaultParams + } + + public var composedURL: URL { + let params = parameter.map({ "\($0.key)=\($0.value)" }).joined(separator: "&") + let urlString = baseURL.appending(path) + .appending("?") + .appending(params) + return URL(string: urlString) ?? URL.init(fileURLWithPath: "") + } +} diff --git a/Modules/Common/Common/Sources/Common/Data/API/NetworkService.swift b/Modules/Common/Common/Sources/Common/Data/API/NetworkService.swift index 528cc05..972ba9e 100644 --- a/Modules/Common/Common/Sources/Common/Data/API/NetworkService.swift +++ b/Modules/Common/Common/Sources/Common/Data/API/NetworkService.swift @@ -12,9 +12,9 @@ import Alamofire public class NetworkService { public static let shared = NetworkService() - public func connect(api: URL, responseType: T.Type) async throws -> T { + public func connect(api: APIFactory, responseType: T.Type) async throws -> T { return try await withCheckedThrowingContinuation { continuation in - AF.request(api.absoluteString) + AF.request(api.composedURL) .prettyPrintedJsonResponse(of: responseType) .responseDecodable(of: responseType) { response in switch response.result { diff --git a/Modules/Common/Common/Sources/Common/Data/API/TenorAPI.swift b/Modules/Common/Common/Sources/Common/Data/API/TenorAPI.swift new file mode 100644 index 0000000..4c98b92 --- /dev/null +++ b/Modules/Common/Common/Sources/Common/Data/API/TenorAPI.swift @@ -0,0 +1,40 @@ +// +// TenorAPI.swift +// +// +// Created by Uwais Alqadri on 11/10/24. +// + +import Foundation + +public enum TenorAPI { + case search(query: String) +} + +extension TenorAPI: APIFactory { + public var path: String { + switch self { + case .search: + return "search" + } + } + + public var parameter: [String: Any] { + switch self { + case let .search(query): + return ["q": query, "key": "LIVDSRZULELA", "limit": 100] + } + } + + public var composedURL: URL { + let params = parameter.map({ "\($0.key)=\($0.value)" }).joined(separator: "&") + let urlString = baseURL.appending(path) + .appending("?") + .appending(params) + return URL(string: urlString) ?? URL.init(fileURLWithPath: "") + } + + public var baseURL: String { + APIConfig.tenorBaseUrl + } +} diff --git a/Modules/Common/Common/Sources/Common/Data/DataSource/Remote/SearchRemoteDataSource.swift b/Modules/Common/Common/Sources/Common/Data/DataSource/Remote/SearchRemoteDataSource.swift index 7726d31..d3a2478 100644 --- a/Modules/Common/Common/Sources/Common/Data/DataSource/Remote/SearchRemoteDataSource.swift +++ b/Modules/Common/Common/Sources/Common/Data/DataSource/Remote/SearchRemoteDataSource.swift @@ -15,11 +15,16 @@ public struct SearchRemoteDataSource: DataSource { public init() {} public func execute(request: String?) async throws -> [Giphy] { - let result = try await NetworkService.shared.connect( - api: APIFactory.search(query: request ?? "").url, + let giphyResult = try await NetworkService.shared.connect( + api: GiphyAPI.search(query: request ?? ""), responseType: GiphyDataResponse.self ).data.compactMap { $0.map() } - return result +// let tenorResult = try await NetworkService.shared.connect( +// api: TenorAPI.search(query: request ?? ""), +// responseType: GiphyDataResponse.self +// ).data.compactMap { $0.map() } + + return giphyResult } } diff --git a/Modules/Common/Common/Sources/Common/Data/DataSource/Remote/TrendingRemoteDataSource.swift b/Modules/Common/Common/Sources/Common/Data/DataSource/Remote/TrendingRemoteDataSource.swift index f9e6ea2..b4ee5b1 100644 --- a/Modules/Common/Common/Sources/Common/Data/DataSource/Remote/TrendingRemoteDataSource.swift +++ b/Modules/Common/Common/Sources/Common/Data/DataSource/Remote/TrendingRemoteDataSource.swift @@ -16,7 +16,7 @@ public struct TrendingRemoteDataSource: DataSource { public func execute(request: Int?) async throws -> [Giphy] { let result = try await NetworkService.shared.connect( - api: APIFactory.trending.url, + api: GiphyAPI.trending, responseType: GiphyDataResponse.self ).data.compactMap { $0.map() }