Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improved download speed #19

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Loadify/App/Enums/API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extension API: NetworkRequestable {
}
}

var queryParameter: [String : AnyHashable]? {
var queryParameters: [String : AnyHashable]? {
switch self {
case .details(let url):
return ["url": url]
Expand Down
30 changes: 16 additions & 14 deletions Loadify/Services/NetworkRequestable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

enum HTTPMethod: String, Equatable {
enum HTTPMethod: String {
case delete = "DELETE"
case get = "GET"
case post = "POST"
Expand All @@ -16,14 +16,13 @@ enum HTTPMethod: String, Equatable {
}

protocol NetworkRequestable {
var url: URL { get throws }
var host: String { get }
var path: String { get }
var port: Int? { get }
var isSecure: Bool { get }
var shouldRunLocal: Bool { get }
var httpMethod: HTTPMethod { get }
var queryParameter: [String: AnyHashable]? { get }
var queryParameters: [String: AnyHashable]? { get }
}

/// Protocol Extension for constructing `URL`
Expand All @@ -32,13 +31,17 @@ extension NetworkRequestable {
/// Computed property to construct the URL based on the configuration.
var url: URL {
get throws {
var urlComponent = URLComponents()
urlComponent.path = path
urlComponent.port = shouldRunLocal ? port : nil
urlComponent.host = shouldRunLocal ? "localhost" : host
urlComponent.scheme = isSecure && !shouldRunLocal ? "https" : "http"
guard let url = urlComponent.url else { throw URLError(.badURL) }
return url.addQueryParamIfNeeded(queryParameter)
var urlComponents = URLComponents()
urlComponents.path = path
urlComponents.port = shouldRunLocal ? port : nil
urlComponents.host = shouldRunLocal ? "localhost" : host
urlComponents.scheme = isSecure && !shouldRunLocal ? "https" : "http"

guard let url = urlComponents.url else {
throw URLError(.badURL)
}

return url.addQueryParametersIfNeeded(queryParameters)
}
}

Expand All @@ -61,13 +64,12 @@ extension NetworkRequestable {
}

fileprivate extension URL {

func addQueryParamIfNeeded(_ queryParams: [String: Any]?) -> URL {
guard let queryParams = queryParams,
func addQueryParametersIfNeeded(_ queryParameters: [String: AnyHashable]?) -> URL {
guard let queryParameters = queryParameters,
var urlComponents = URLComponents(string: absoluteString) else {
return absoluteURL
}
let queryItems = queryParams.map { URLQueryItem(name: $0, value: "\($1)") }
let queryItems = queryParameters.map { URLQueryItem(name: $0, value: "\($1)") }
urlComponents.queryItems = queryItems
return urlComponents.url!
}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Follow the steps below to setup Loadify on your machine:
The packages listed below are used by loadify:

- [FontKit](https://github.com/VishwaiOSDev/FontKit)
- [LogKit](https://github.com/VishwaiOSDev/LogKit)
- [LoggerKit](https://github.com/VishwaiOSDev/LogKit)

## Author

Expand Down