-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
801e365
commit aa498e7
Showing
7 changed files
with
113 additions
and
52 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
51 changes: 8 additions & 43 deletions
51
Modules/Common/Common/Sources/Common/Data/API/APIFactory.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 |
---|---|---|
@@ -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 } | ||
} |
50 changes: 50 additions & 0 deletions
50
Modules/Common/Common/Sources/Common/Data/API/GiphyAPI.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,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: "") | ||
} | ||
} |
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
40 changes: 40 additions & 0 deletions
40
Modules/Common/Common/Sources/Common/Data/API/TenorAPI.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,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 | ||
} | ||
} |
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