Skip to content

Commit cfd7830

Browse files
authored
Merge pull request #19 from VishwaiOSDev/refactor/vishwa/minor-changes
improved download speed
2 parents da017ee + b07d64b commit cfd7830

3 files changed

Lines changed: 18 additions & 16 deletions

File tree

Loadify/App/Enums/API.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ extension API: NetworkRequestable {
3434
}
3535
}
3636

37-
var queryParameter: [String : AnyHashable]? {
37+
var queryParameters: [String : AnyHashable]? {
3838
switch self {
3939
case .details(let url):
4040
return ["url": url]

Loadify/Services/NetworkRequestable.swift

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import Foundation
99

10-
enum HTTPMethod: String, Equatable {
10+
enum HTTPMethod: String {
1111
case delete = "DELETE"
1212
case get = "GET"
1313
case post = "POST"
@@ -16,14 +16,13 @@ enum HTTPMethod: String, Equatable {
1616
}
1717

1818
protocol NetworkRequestable {
19-
var url: URL { get throws }
2019
var host: String { get }
2120
var path: String { get }
2221
var port: Int? { get }
2322
var isSecure: Bool { get }
2423
var shouldRunLocal: Bool { get }
2524
var httpMethod: HTTPMethod { get }
26-
var queryParameter: [String: AnyHashable]? { get }
25+
var queryParameters: [String: AnyHashable]? { get }
2726
}
2827

2928
/// Protocol Extension for constructing `URL`
@@ -32,13 +31,17 @@ extension NetworkRequestable {
3231
/// Computed property to construct the URL based on the configuration.
3332
var url: URL {
3433
get throws {
35-
var urlComponent = URLComponents()
36-
urlComponent.path = path
37-
urlComponent.port = shouldRunLocal ? port : nil
38-
urlComponent.host = shouldRunLocal ? "localhost" : host
39-
urlComponent.scheme = isSecure && !shouldRunLocal ? "https" : "http"
40-
guard let url = urlComponent.url else { throw URLError(.badURL) }
41-
return url.addQueryParamIfNeeded(queryParameter)
34+
var urlComponents = URLComponents()
35+
urlComponents.path = path
36+
urlComponents.port = shouldRunLocal ? port : nil
37+
urlComponents.host = shouldRunLocal ? "localhost" : host
38+
urlComponents.scheme = isSecure && !shouldRunLocal ? "https" : "http"
39+
40+
guard let url = urlComponents.url else {
41+
throw URLError(.badURL)
42+
}
43+
44+
return url.addQueryParametersIfNeeded(queryParameters)
4245
}
4346
}
4447

@@ -61,13 +64,12 @@ extension NetworkRequestable {
6164
}
6265

6366
fileprivate extension URL {
64-
65-
func addQueryParamIfNeeded(_ queryParams: [String: Any]?) -> URL {
66-
guard let queryParams = queryParams,
67+
func addQueryParametersIfNeeded(_ queryParameters: [String: AnyHashable]?) -> URL {
68+
guard let queryParameters = queryParameters,
6769
var urlComponents = URLComponents(string: absoluteString) else {
6870
return absoluteURL
6971
}
70-
let queryItems = queryParams.map { URLQueryItem(name: $0, value: "\($1)") }
72+
let queryItems = queryParameters.map { URLQueryItem(name: $0, value: "\($1)") }
7173
urlComponents.queryItems = queryItems
7274
return urlComponents.url!
7375
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Follow the steps below to setup Loadify on your machine:
4747
The packages listed below are used by loadify:
4848

4949
- [FontKit](https://github.com/VishwaiOSDev/FontKit)
50-
- [LogKit](https://github.com/VishwaiOSDev/LogKit)
50+
- [LoggerKit](https://github.com/VishwaiOSDev/LogKit)
5151

5252
## Author
5353

0 commit comments

Comments
 (0)