-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from ragzy15/develop
Publisher Model
- Loading branch information
Showing
29 changed files
with
1,710 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,16 @@ | ||
{ | ||
"object": { | ||
"pins": [ | ||
{ | ||
"package": "PublisherKit", | ||
"repositoryURL": "https://github.com/ragzy15/PublisherKit", | ||
"state": { | ||
"branch": null, | ||
"revision": "ad211780f0f532a473e3d5d1cef2a2644024d7c4", | ||
"version": "1.0.0" | ||
} | ||
} | ||
] | ||
}, | ||
"version": 1 | ||
} |
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
60 changes: 60 additions & 0 deletions
60
Sources/NetworkKit/Connection/Connection Representable.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,60 @@ | ||
// | ||
// ConnectionRepresentable.swift | ||
// NetworkKit | ||
// | ||
// Created by Raghav Ahuja on 15/10/19. | ||
// Copyright © 2019 Raghav Ahuja. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/** | ||
A type that represents as a connection or an endpoint. | ||
|
||
``` | ||
let url = "https://api.example.com/users/all" | ||
// `/users/all` is a connection. | ||
``` | ||
*/ | ||
public protocol ConnectionRepresentable { | ||
|
||
/** | ||
The path subcomponent. It is the connection endpoint for the url. | ||
|
||
``` | ||
let url = "https://api.example.com/users/all" | ||
// `/users/all` is the path for this connection | ||
``` | ||
|
||
Setting this property assumes the subcomponent or component string is not percent encoded and will add percent encoding (if the component allows percent encoding). | ||
*/ | ||
var path: String { get } | ||
|
||
/// Connection name if any. Use for console logging. Defaults to connection url if provided `nil`. | ||
var name: String? { get } // for console logging purposes only | ||
|
||
/// HTTP Method for the connection request. | ||
var method: HTTPMethod { get } | ||
|
||
/// Default Headers attached to connection request. Example: ["User-Agent": "iOS_13_0"] | ||
var httpHeaders: HTTPHeaderParameters { get } | ||
|
||
/// The scheme subcomponent of the URL. Default value is `.https` | ||
var scheme: Scheme { get } | ||
|
||
/// Host for the connection. | ||
var host: HostRepresentable { get } | ||
|
||
/// Default URL Query for connection. Example: ["client": "ios"] | ||
var defaultQuery: URLQuery? { get } | ||
|
||
/// API Type for connection. Default value is `host.defaultAPIType`. | ||
var apiType: APIRepresentable? { get } | ||
} | ||
|
||
public extension ConnectionRepresentable { | ||
|
||
var scheme: Scheme { .https } | ||
|
||
var apiType: APIRepresentable? { host.defaultAPIType } | ||
} |
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,61 @@ | ||
// | ||
// CreateRequest.swift | ||
// NetworkKit | ||
// | ||
// Created by Raghav Ahuja on 15/10/19. | ||
// Copyright © 2019 Raghav Ahuja. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public typealias URLQuery = [String: String?] | ||
public typealias HTTPHeaderParameters = [String: String] | ||
|
||
public struct CreateRequest { | ||
|
||
public let request: URLRequest | ||
|
||
public init?(with connection: ConnectionRepresentable, query urlQuery: Set<URLQueryItem>, body: Data?, headers: HTTPHeaderParameters) { | ||
|
||
var components = URLComponents() | ||
components.scheme = connection.scheme.rawValue | ||
|
||
let subURL = connection.apiType?.subUrl ?? "" | ||
let endPoint = connection.apiType?.endPoint ?? "" | ||
|
||
components.host = (subURL.isEmpty ? subURL : subURL + ".") + connection.host.host | ||
components.path = endPoint + connection.path | ||
|
||
var queryItems = Set<URLQueryItem>() | ||
queryItems.addURLQuery(query: connection.defaultQuery) | ||
queryItems.addURLQuery(query: connection.host.defaultUrlQuery) | ||
queryItems = queryItems.union(urlQuery) | ||
|
||
let method = connection.method | ||
|
||
if !queryItems.isEmpty { | ||
components.queryItems = Array(queryItems) | ||
} | ||
|
||
guard let url = components.url else { | ||
return nil | ||
} | ||
|
||
var urlRequest = URLRequest(url: url) | ||
urlRequest.httpMethod = method.rawValue | ||
|
||
let defaultHeaderFields = connection.host.defaultHeaders | ||
let connectionHeaderFields = connection.httpHeaders | ||
|
||
var headerFields = defaultHeaderFields.merging(connectionHeaderFields) { (_, new) in new } | ||
headerFields.merge(headers) { (_, new) in new } | ||
|
||
if !headerFields.isEmpty { | ||
urlRequest.allHTTPHeaderFields = headerFields | ||
} | ||
|
||
urlRequest.httpBody = body | ||
request = urlRequest | ||
|
||
} | ||
} |
Oops, something went wrong.