Skip to content

Commit

Permalink
Merge pull request #1 from ragzy15/develop
Browse files Browse the repository at this point in the history
Publisher Model
  • Loading branch information
ragzy15 committed Dec 30, 2019
2 parents 881ac25 + d61d332 commit ba742f4
Show file tree
Hide file tree
Showing 29 changed files with 1,710 additions and 11 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions Package.resolved
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
}
9 changes: 8 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@ import PackageDescription

let package = Package(
name: "NetworkKit",
platforms: [
.iOS(.v8),
.macOS(.v10_10),
.tvOS(.v10),
.watchOS(.v4)
],
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "NetworkKit",
targets: ["NetworkKit"]),
],
dependencies: [
.package(url: "https://github.com/ragzy15/PublisherKit", from: .init(1, 0, 0))
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
Expand All @@ -20,7 +27,7 @@ let package = Package(
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "NetworkKit",
dependencies: []),
dependencies: ["PublisherKit"]),
.testTarget(
name: "NetworkKitTests",
dependencies: ["NetworkKit"]),
Expand Down
60 changes: 60 additions & 0 deletions Sources/NetworkKit/Connection/Connection Representable.swift
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 }
}
61 changes: 61 additions & 0 deletions Sources/NetworkKit/Create Request/Create Request.swift
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

}
}
Loading

0 comments on commit ba742f4

Please sign in to comment.