Skip to content

Commit ba742f4

Browse files
authored
Merge pull request #1 from ragzy15/develop
Publisher Model
2 parents 881ac25 + d61d332 commit ba742f4

29 files changed

+1710
-11
lines changed

.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.resolved

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,20 @@ import PackageDescription
55

66
let package = Package(
77
name: "NetworkKit",
8+
platforms: [
9+
.iOS(.v8),
10+
.macOS(.v10_10),
11+
.tvOS(.v10),
12+
.watchOS(.v4)
13+
],
814
products: [
915
// Products define the executables and libraries produced by a package, and make them visible to other packages.
1016
.library(
1117
name: "NetworkKit",
1218
targets: ["NetworkKit"]),
1319
],
1420
dependencies: [
21+
.package(url: "https://github.com/ragzy15/PublisherKit", from: .init(1, 0, 0))
1522
// Dependencies declare other packages that this package depends on.
1623
// .package(url: /* package url */, from: "1.0.0"),
1724
],
@@ -20,7 +27,7 @@ let package = Package(
2027
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
2128
.target(
2229
name: "NetworkKit",
23-
dependencies: []),
30+
dependencies: ["PublisherKit"]),
2431
.testTarget(
2532
name: "NetworkKitTests",
2633
dependencies: ["NetworkKit"]),
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//
2+
// ConnectionRepresentable.swift
3+
// NetworkKit
4+
//
5+
// Created by Raghav Ahuja on 15/10/19.
6+
// Copyright © 2019 Raghav Ahuja. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
/**
12+
A type that represents as a connection or an endpoint.
13+
14+
```
15+
let url = "https://api.example.com/users/all"
16+
// `/users/all` is a connection.
17+
```
18+
*/
19+
public protocol ConnectionRepresentable {
20+
21+
/**
22+
The path subcomponent. It is the connection endpoint for the url.
23+
24+
```
25+
let url = "https://api.example.com/users/all"
26+
// `/users/all` is the path for this connection
27+
```
28+
29+
Setting this property assumes the subcomponent or component string is not percent encoded and will add percent encoding (if the component allows percent encoding).
30+
*/
31+
var path: String { get }
32+
33+
/// Connection name if any. Use for console logging. Defaults to connection url if provided `nil`.
34+
var name: String? { get } // for console logging purposes only
35+
36+
/// HTTP Method for the connection request.
37+
var method: HTTPMethod { get }
38+
39+
/// Default Headers attached to connection request. Example: ["User-Agent": "iOS_13_0"]
40+
var httpHeaders: HTTPHeaderParameters { get }
41+
42+
/// The scheme subcomponent of the URL. Default value is `.https`
43+
var scheme: Scheme { get }
44+
45+
/// Host for the connection.
46+
var host: HostRepresentable { get }
47+
48+
/// Default URL Query for connection. Example: ["client": "ios"]
49+
var defaultQuery: URLQuery? { get }
50+
51+
/// API Type for connection. Default value is `host.defaultAPIType`.
52+
var apiType: APIRepresentable? { get }
53+
}
54+
55+
public extension ConnectionRepresentable {
56+
57+
var scheme: Scheme { .https }
58+
59+
var apiType: APIRepresentable? { host.defaultAPIType }
60+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//
2+
// CreateRequest.swift
3+
// NetworkKit
4+
//
5+
// Created by Raghav Ahuja on 15/10/19.
6+
// Copyright © 2019 Raghav Ahuja. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
public typealias URLQuery = [String: String?]
12+
public typealias HTTPHeaderParameters = [String: String]
13+
14+
public struct CreateRequest {
15+
16+
public let request: URLRequest
17+
18+
public init?(with connection: ConnectionRepresentable, query urlQuery: Set<URLQueryItem>, body: Data?, headers: HTTPHeaderParameters) {
19+
20+
var components = URLComponents()
21+
components.scheme = connection.scheme.rawValue
22+
23+
let subURL = connection.apiType?.subUrl ?? ""
24+
let endPoint = connection.apiType?.endPoint ?? ""
25+
26+
components.host = (subURL.isEmpty ? subURL : subURL + ".") + connection.host.host
27+
components.path = endPoint + connection.path
28+
29+
var queryItems = Set<URLQueryItem>()
30+
queryItems.addURLQuery(query: connection.defaultQuery)
31+
queryItems.addURLQuery(query: connection.host.defaultUrlQuery)
32+
queryItems = queryItems.union(urlQuery)
33+
34+
let method = connection.method
35+
36+
if !queryItems.isEmpty {
37+
components.queryItems = Array(queryItems)
38+
}
39+
40+
guard let url = components.url else {
41+
return nil
42+
}
43+
44+
var urlRequest = URLRequest(url: url)
45+
urlRequest.httpMethod = method.rawValue
46+
47+
let defaultHeaderFields = connection.host.defaultHeaders
48+
let connectionHeaderFields = connection.httpHeaders
49+
50+
var headerFields = defaultHeaderFields.merging(connectionHeaderFields) { (_, new) in new }
51+
headerFields.merge(headers) { (_, new) in new }
52+
53+
if !headerFields.isEmpty {
54+
urlRequest.allHTTPHeaderFields = headerFields
55+
}
56+
57+
urlRequest.httpBody = body
58+
request = urlRequest
59+
60+
}
61+
}

0 commit comments

Comments
 (0)