Skip to content

Commit 3244cdf

Browse files
committed
Public init
1 parent d9f0c21 commit 3244cdf

File tree

5 files changed

+201
-197
lines changed

5 files changed

+201
-197
lines changed

BuckoNetworking/Bucko.swift

Lines changed: 71 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Alamofire
1111
import SwiftyJSON
1212

1313
public protocol BuckoErrorHandler: class {
14-
func buckoRequest(request: URLRequest, error: Error)
14+
func buckoRequest(request: URLRequest, error: Error)
1515
}
1616

1717
public typealias BuckoResponseClosure = ((DataResponse<Any>) -> Void)
@@ -24,75 +24,79 @@ public typealias Body = Parameters
2424
public typealias Json = JSON
2525

2626
public struct Bucko {
27-
/**
28-
Can be overriden to configure the session manager.
29-
e.g - Creating server trust policies
30-
```
31-
let manager: SessionManager = {
32-
// Create the server trust policies
33-
let serverTrustPolicies: [String: ServerTrustPolicy] = [
34-
"0.0.0.0": .disableEvaluation // Use your server obviously. Can be a url as well, example.com
35-
]
36-
// Create custom manager
37-
let configuration = URLSessionConfiguration.default
38-
configuration.httpAdditionalHeaders = SessionManager.defaultHTTPHeaders
39-
let manager = SessionManager(
40-
configuration: URLSessionConfiguration.default,
41-
serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
42-
)
43-
return manager
44-
}()
45-
46-
Bucko.shared.manager = manager
47-
48-
```
49-
*/
50-
public var manager: SessionManager = SessionManager()
51-
public static let shared = Bucko()
52-
public weak var delegate: BuckoErrorHandler?
27+
/**
28+
Can be overriden to configure the session manager.
29+
e.g - Creating server trust policies
30+
```
31+
let manager: SessionManager = {
32+
// Create the server trust policies
33+
let serverTrustPolicies: [String: ServerTrustPolicy] = [
34+
"0.0.0.0": .disableEvaluation // Use your server obviously. Can be a url as well, example.com
35+
]
36+
// Create custom manager
37+
let configuration = URLSessionConfiguration.default
38+
configuration.httpAdditionalHeaders = SessionManager.defaultHTTPHeaders
39+
let manager = SessionManager(
40+
configuration: URLSessionConfiguration.default,
41+
serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
42+
)
43+
return manager
44+
}()
45+
46+
Bucko.shared.manager = manager
47+
48+
```
49+
*/
50+
public var manager: SessionManager = SessionManager()
51+
public static let shared = Bucko()
52+
public weak var delegate: BuckoErrorHandler?
53+
54+
public init() {
5355

54-
/**
55-
Make API requests
56-
57-
Example:
58-
59-
```
60-
let request = Bucko.shared.request(.getUser(id: "1")) { response in
61-
if let response.result.isSuccess {
62-
let json = JSON(response.result.value!)
56+
}
57+
58+
/**
59+
Make API requests
60+
61+
Example:
62+
63+
```
64+
let request = Bucko.shared.request(.getUser(id: "1")) { response in
65+
if let response.result.isSuccess {
66+
let json = JSON(response.result.value!)
67+
} else {
68+
// Handle error
69+
}
70+
```
71+
72+
- parameter endpoint: The endpoint to use.
73+
- parameter completion: The closure that will return the response from the server.
74+
- returns: The request that was made.
75+
*/
76+
@discardableResult
77+
public func request(endpoint: Endpoint, completion: @escaping BuckoResponseClosure) -> Request {
78+
let request = manager.request(
79+
endpoint.fullURL,
80+
method: endpoint.method,
81+
parameters: endpoint.body,
82+
encoding: endpoint.encoding,
83+
headers: endpoint.headers
84+
).responseJSON { response in
85+
86+
if response.result.isSuccess {
87+
debugPrint(response.result.description)
6388
} else {
64-
// Handle error
65-
}
66-
```
67-
68-
- parameter endpoint: The endpoint to use.
69-
- parameter completion: The closure that will return the response from the server.
70-
- returns: The request that was made.
71-
*/
72-
@discardableResult
73-
public func request(endpoint: Endpoint, completion: @escaping BuckoResponseClosure) -> Request {
74-
let request = manager.request(
75-
endpoint.fullURL,
76-
method: endpoint.method,
77-
parameters: endpoint.body,
78-
encoding: endpoint.encoding,
79-
headers: endpoint.headers
80-
).responseJSON { response in
81-
82-
if response.result.isSuccess {
83-
debugPrint(response.result.description)
84-
} else {
85-
debugPrint(response.result.error ?? "Error")
86-
// Can globably handle errors here if you want
87-
if let urlRequest = response.request, let error = response.result.error {
88-
self.delegate?.buckoRequest(request: urlRequest, error: error)
89-
}
90-
}
91-
92-
completion(response)
89+
debugPrint(response.result.error ?? "Error")
90+
// Can globably handle errors here if you want
91+
if let urlRequest = response.request, let error = response.result.error {
92+
self.delegate?.buckoRequest(request: urlRequest, error: error)
93+
}
9394
}
9495

95-
print(request.description)
96-
return request
96+
completion(response)
9797
}
98+
99+
print(request.description)
100+
return request
101+
}
98102
}

BuckoNetworking/BuckoError.swift

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -9,59 +9,59 @@
99
import Foundation.NSError
1010

1111
public final class BuckoError: NSError {
12-
13-
fileprivate enum Code: Int {
14-
case api = 1
15-
case validation
16-
case service // an error caused by a third party service
17-
case jsonMapping // an error caused by inability to map json responses
18-
case auth
19-
case unknown
20-
}
21-
22-
fileprivate static var domain: String {
23-
return Bundle.main.bundleIdentifier!
24-
}
25-
26-
// MARK: - Convenience error initializers
27-
convenience init(apiError reason: String, description: String? = nil) {
28-
self.init(code: .api, reason: reason, description: description)
29-
}
30-
31-
convenience init(validationError reason: String, description: String? = nil) {
32-
self.init(code: .validation, reason: reason, description: description)
33-
}
34-
35-
convenience init(serviceError reason: String) {
36-
self.init(code: .service, reason: reason)
37-
}
38-
39-
convenience init(mappingErrorKey key: String) {
40-
self.init(
41-
code: .jsonMapping,
42-
reason: "An unknown error occurred",
43-
description: "Could not parse value for key \(key)"
44-
)
45-
}
46-
47-
convenience init(authError reason: String, description: String) {
48-
self.init(code: .auth, reason: reason, description: description)
49-
}
50-
51-
fileprivate convenience init(code: Code, reason: String, description: String? = nil) {
52-
self.init(domain: BuckoError.domain, code: code.rawValue, userInfo: [
53-
NSLocalizedFailureReasonErrorKey: reason,
54-
NSLocalizedDescriptionKey: description ?? reason
55-
])
56-
}
57-
58-
// MARK: - Common errors
59-
static func invalidAPIResponse() -> BuckoError {
60-
return BuckoError(apiError: "An unknown error occurred", description: "Invalid Response")
61-
}
62-
63-
static func unknown() -> BuckoError {
64-
return BuckoError(code: .unknown, reason: "An unknown error occurred")
65-
}
66-
12+
13+
fileprivate enum Code: Int {
14+
case api = 1
15+
case validation
16+
case service // an error caused by a third party service
17+
case jsonMapping // an error caused by inability to map json responses
18+
case auth
19+
case unknown
20+
}
21+
22+
fileprivate static var domain: String {
23+
return Bundle.main.bundleIdentifier!
24+
}
25+
26+
// MARK: - Convenience error initializers
27+
convenience init(apiError reason: String, description: String? = nil) {
28+
self.init(code: .api, reason: reason, description: description)
29+
}
30+
31+
convenience init(validationError reason: String, description: String? = nil) {
32+
self.init(code: .validation, reason: reason, description: description)
33+
}
34+
35+
convenience init(serviceError reason: String) {
36+
self.init(code: .service, reason: reason)
37+
}
38+
39+
convenience init(mappingErrorKey key: String) {
40+
self.init(
41+
code: .jsonMapping,
42+
reason: "An unknown error occurred",
43+
description: "Could not parse value for key \(key)"
44+
)
45+
}
46+
47+
convenience init(authError reason: String, description: String) {
48+
self.init(code: .auth, reason: reason, description: description)
49+
}
50+
51+
fileprivate convenience init(code: Code, reason: String, description: String? = nil) {
52+
self.init(domain: BuckoError.domain, code: code.rawValue, userInfo: [
53+
NSLocalizedFailureReasonErrorKey: reason,
54+
NSLocalizedDescriptionKey: description ?? reason
55+
])
56+
}
57+
58+
// MARK: - Common errors
59+
static func invalidAPIResponse() -> BuckoError {
60+
return BuckoError(apiError: "An unknown error occurred", description: "Invalid Response")
61+
}
62+
63+
static func unknown() -> BuckoError {
64+
return BuckoError(code: .unknown, reason: "An unknown error occurred")
65+
}
66+
6767
}

BuckoNetworking/Protocols/Endpoint.swift

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,55 @@
77
//
88

99
/**
10-
Conform to Endpoint to create new endpoints to use with Bucko.
11-
You can create an extension to create default values. This is recommended for the baseURL.
10+
Conform to Endpoint to create new endpoints to use with Bucko.
11+
You can create an extension to create default values. This is recommended for the baseURL.
1212
*/
1313
public protocol Endpoint {
14-
var baseURL: String { get } // https://example.com
15-
var path: String { get } // /users/
16-
17-
/**
18-
This should **NOT** be set by the conforming type.
19-
This will automatically be set by the baseURL and the path.
20-
*/
21-
var fullURL: String { get }
22-
var method: HttpMethod { get }
23-
24-
/**
25-
By default, encoding will be set to URLEncoding for GET requests
26-
and JSONEncoding for everything else.
27-
You should override encoding if you need to customize this.
28-
29-
JSONEncoding.default
30-
URLEncoding.default
31-
PropertyListEncoding.default
32-
You can also create your own.
33-
*/
34-
var encoding: Encoding { get }
35-
36-
/**
37-
By default this will be set to empty - Parameters()
38-
*/
39-
var body: Body { get }
40-
41-
/**
42-
Authorization is usually set in the headers. You can set this to `[:]` if you don't have any
43-
headers to set. You can also create an extention on Endpoint to also have
44-
this default to a value.
45-
*/
46-
var headers: HttpHeaders { get }
14+
var baseURL: String { get } // https://example.com
15+
var path: String { get } // /users/
16+
17+
/**
18+
This should **NOT** be set by the conforming type.
19+
This will automatically be set by the baseURL and the path.
20+
*/
21+
var fullURL: String { get }
22+
var method: HttpMethod { get }
23+
24+
/**
25+
By default, encoding will be set to URLEncoding for GET requests
26+
and JSONEncoding for everything else.
27+
You should override encoding if you need to customize this.
28+
29+
JSONEncoding.default
30+
URLEncoding.default
31+
PropertyListEncoding.default
32+
You can also create your own.
33+
*/
34+
var encoding: Encoding { get }
35+
36+
/**
37+
By default this will be set to empty - Parameters()
38+
*/
39+
var body: Body { get }
40+
41+
/**
42+
Authorization is usually set in the headers. You can set this to `[:]` if you don't have any
43+
headers to set. You can also create an extention on Endpoint to also have
44+
this default to a value.
45+
*/
46+
var headers: HttpHeaders { get }
4747
}
4848

4949
public extension Endpoint {
50-
var encoding: Encoding {
51-
return method == .get ? UrlEncoding.default : JsonEncoding.default
52-
}
53-
54-
var fullURL: String {
55-
return baseURL + path
56-
}
57-
58-
var body: Body {
59-
return Body()
60-
}
50+
var encoding: Encoding {
51+
return method == .get ? UrlEncoding.default : JsonEncoding.default
52+
}
53+
54+
var fullURL: String {
55+
return baseURL + path
56+
}
57+
58+
var body: Body {
59+
return Body()
60+
}
6161
}

BuckoNetworking/Protocols/JSONDecodable.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
//
88

99
public protocol JSONDecodable {
10-
associatedtype Model
11-
static func map(from json: Json) throws -> Model
10+
associatedtype Model
11+
static func map(from json: Json) throws -> Model
1212
}

0 commit comments

Comments
 (0)