Cocoapods. Network layer. Fetching repos.
This commit is contained in:
parent
e348ad1eb1
commit
3dafdbc09c
143 changed files with 12470 additions and 47 deletions
151
Pods/BaseAPI/BaseAPI/Classes/BaseAPI.swift
generated
Normal file
151
Pods/BaseAPI/BaseAPI/Classes/BaseAPI.swift
generated
Normal file
|
@ -0,0 +1,151 @@
|
|||
//
|
||||
// BaseAPI.swift
|
||||
// BaseAPI
|
||||
//
|
||||
// Created by Serhii Londar on 12/8/17.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public typealias BaseAPICompletion = (Data?, URLResponse?, Error?) -> Swift.Void
|
||||
public typealias BaseAPIResult = SynchronousDataTaskResult
|
||||
open class BaseAPI {
|
||||
var session: URLSession
|
||||
|
||||
public init() {
|
||||
self.session = URLSession(configuration: URLSessionConfiguration.default)
|
||||
}
|
||||
|
||||
public init(session: URLSession) {
|
||||
self.session = session
|
||||
}
|
||||
|
||||
public func get(url: String, parameters: [String : String]? = nil, headers: [String: String]? = nil, completion: @escaping BaseAPICompletion) {
|
||||
let request = Request(url: url, method: .GET, parameters: parameters, headers: headers, body: nil)
|
||||
let buildRequest = request.request()
|
||||
if let urlRequest = buildRequest.request {
|
||||
let task = session.dataTask(with: urlRequest, completionHandler: completion)
|
||||
task.resume()
|
||||
} else {
|
||||
completion(nil, nil, buildRequest.error)
|
||||
}
|
||||
}
|
||||
|
||||
public func get(url: String, parameters: [String : String]? = nil, headers: [String: String]? = nil) -> BaseAPIResult {
|
||||
let request = Request(url: url, method: .GET, parameters: parameters, headers: headers, body: nil)
|
||||
let buildRequest = request.request()
|
||||
if let urlRequest = buildRequest.request {
|
||||
return session.synchronousDataTask(request: urlRequest)
|
||||
} else {
|
||||
return (nil, nil, buildRequest.error)
|
||||
}
|
||||
}
|
||||
|
||||
public func head(url: String, parameters: [String : String]? = nil, headers: [String: String]? = nil, completion: @escaping BaseAPICompletion) {
|
||||
let request = Request(url: url, method: .HEAD, parameters: parameters, headers: headers, body: nil)
|
||||
let buildRequest = request.request()
|
||||
if let urlRequest = buildRequest.request {
|
||||
let task = session.dataTask(with: urlRequest, completionHandler: completion)
|
||||
task.resume()
|
||||
} else {
|
||||
completion(nil, nil, buildRequest.error)
|
||||
}
|
||||
}
|
||||
|
||||
public func head(url: String, parameters: [String : String]? = nil, headers: [String: String]? = nil) -> BaseAPIResult {
|
||||
let request = Request(url: url, method: .HEAD, parameters: parameters, headers: headers, body: nil)
|
||||
let buildRequest = request.request()
|
||||
if let urlRequest = buildRequest.request {
|
||||
return session.synchronousDataTask(request: urlRequest)
|
||||
} else {
|
||||
return (nil, nil, buildRequest.error)
|
||||
}
|
||||
}
|
||||
|
||||
public func post(url: String, parameters: [String : String]? = nil, headers: [String: String]? = nil, body: Data?, completion: @escaping BaseAPICompletion) {
|
||||
let request = Request(url: url, method: .POST, parameters: parameters, headers: headers, body: body)
|
||||
let buildRequest = request.request()
|
||||
if let urlRequest = buildRequest.request {
|
||||
let task = session.dataTask(with: urlRequest, completionHandler: completion)
|
||||
task.resume()
|
||||
} else {
|
||||
completion(nil, nil, buildRequest.error)
|
||||
}
|
||||
}
|
||||
|
||||
public func post(url: String, parameters: [String : String]? = nil, headers: [String: String]? = nil, body: Data?) -> BaseAPIResult {
|
||||
let request = Request(url: url, method: .POST, parameters: parameters, headers: headers, body: body)
|
||||
let buildRequest = request.request()
|
||||
if let urlRequest = buildRequest.request {
|
||||
return session.synchronousDataTask(request: urlRequest)
|
||||
} else {
|
||||
return (nil, nil, buildRequest.error)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public func patch(url: String, parameters: [String : String]? = nil, headers: [String: String]? = nil, body: Data?, completion: @escaping BaseAPICompletion) {
|
||||
let request = Request(url: url, method: .PATCH, parameters: parameters, headers: headers, body: body)
|
||||
let buildRequest = request.request()
|
||||
if let urlRequest = buildRequest.request {
|
||||
let task = session.dataTask(with: urlRequest, completionHandler: completion)
|
||||
task.resume()
|
||||
} else {
|
||||
completion(nil, nil, buildRequest.error)
|
||||
}
|
||||
}
|
||||
|
||||
public func patch(url: String, parameters: [String : String]? = nil, headers: [String: String]? = nil, body: Data?) -> BaseAPIResult {
|
||||
let request = Request(url: url, method: .PATCH, parameters: parameters, headers: headers, body: body)
|
||||
let buildRequest = request.request()
|
||||
if let urlRequest = buildRequest.request {
|
||||
return session.synchronousDataTask(request: urlRequest)
|
||||
} else {
|
||||
return (nil, nil, buildRequest.error)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public func put(url: String, parameters: [String : String]? = nil, headers: [String: String]? = nil, body: Data?, completion: @escaping BaseAPICompletion) {
|
||||
let request = Request(url: url, method: .PUT, parameters: parameters, headers: headers, body: body)
|
||||
let buildRequest = request.request()
|
||||
if let urlRequest = buildRequest.request {
|
||||
let task = session.dataTask(with: urlRequest, completionHandler: completion)
|
||||
task.resume()
|
||||
} else {
|
||||
completion(nil, nil, buildRequest.error)
|
||||
}
|
||||
}
|
||||
|
||||
public func put(url: String, parameters: [String : String]? = nil, headers: [String: String]? = nil, body: Data?) -> BaseAPIResult {
|
||||
let request = Request(url: url, method: .PUT, parameters: parameters, headers: headers, body: body)
|
||||
let buildRequest = request.request()
|
||||
if let urlRequest = buildRequest.request {
|
||||
return session.synchronousDataTask(request: urlRequest)
|
||||
} else {
|
||||
return (nil, nil, buildRequest.error)
|
||||
}
|
||||
}
|
||||
|
||||
public func delete(url: String, parameters: [String : String]? = nil, headers: [String: String]? = nil, body: Data? = nil, completion: @escaping BaseAPICompletion) {
|
||||
let request = Request(url: url, method: .DELETE, parameters: parameters, headers: headers, body: body)
|
||||
let buildRequest = request.request()
|
||||
if let urlRequest = buildRequest.request {
|
||||
let task = session.dataTask(with: urlRequest, completionHandler: completion)
|
||||
task.resume()
|
||||
} else {
|
||||
completion(nil, nil, buildRequest.error)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public func delete(url: String, parameters: [String : String]? = nil, headers: [String: String]? = nil, body: Data? = nil) -> BaseAPIResult {
|
||||
let request = Request(url: url, method: .DELETE, parameters: parameters, headers: headers, body: body)
|
||||
let buildRequest = request.request()
|
||||
if let urlRequest = buildRequest.request {
|
||||
return session.synchronousDataTask(request: urlRequest)
|
||||
} else {
|
||||
return (nil, nil, buildRequest.error)
|
||||
}
|
||||
}
|
||||
}
|
18
Pods/BaseAPI/BaseAPI/Classes/Extensions/CharacterSet.swift
generated
Normal file
18
Pods/BaseAPI/BaseAPI/Classes/Extensions/CharacterSet.swift
generated
Normal file
|
@ -0,0 +1,18 @@
|
|||
//
|
||||
// File.swift
|
||||
// BaseAPI
|
||||
//
|
||||
// Created by Serhii Londar on 1/5/18.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
extension CharacterSet {
|
||||
static func BaseAPI_URLQueryAllowedCharacterSet() -> CharacterSet {
|
||||
let generalDelimitersToEncode = ":#[]@"
|
||||
let subDelimitersToEncode = "!$&'()*+,;="
|
||||
var allowedCharacterSet = CharacterSet.urlQueryAllowed
|
||||
allowedCharacterSet.remove(charactersIn: generalDelimitersToEncode + subDelimitersToEncode)
|
||||
return allowedCharacterSet
|
||||
}
|
||||
}
|
12
Pods/BaseAPI/BaseAPI/Classes/Extensions/String.swift
generated
Normal file
12
Pods/BaseAPI/BaseAPI/Classes/Extensions/String.swift
generated
Normal file
|
@ -0,0 +1,12 @@
|
|||
//
|
||||
// String.swift
|
||||
// BaseAPI
|
||||
//
|
||||
// Created by Serhii Londar on 1/5/18.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
extension String: Error {
|
||||
|
||||
}
|
55
Pods/BaseAPI/BaseAPI/Classes/Extensions/URLSession.swift
generated
Normal file
55
Pods/BaseAPI/BaseAPI/Classes/Extensions/URLSession.swift
generated
Normal file
|
@ -0,0 +1,55 @@
|
|||
//
|
||||
// URLSession.swift
|
||||
// BaseAPI
|
||||
//
|
||||
// Created by Serhii Londar on 8/22/17.
|
||||
//
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Dispatch
|
||||
|
||||
public typealias SynchronousDataTaskResult = (data: Data?, response: URLResponse?, error: Error?)
|
||||
|
||||
extension URLSession {
|
||||
public func synchronousDataTask(request: URLRequest) -> SynchronousDataTaskResult {
|
||||
var data: Data?
|
||||
var response: URLResponse?
|
||||
var error: Error?
|
||||
|
||||
let semaphore = DispatchSemaphore(value: 0)
|
||||
|
||||
let dataTask = self.dataTask(with: request) { (rData, rResponse, eError) in
|
||||
data = rData
|
||||
response = rResponse
|
||||
error = eError
|
||||
semaphore.signal()
|
||||
}
|
||||
dataTask.resume()
|
||||
|
||||
_ = semaphore.wait(timeout: .distantFuture)
|
||||
|
||||
return (data, response, error)
|
||||
}
|
||||
|
||||
public func synchronousDataTask(url: URL) -> SynchronousDataTaskResult {
|
||||
var data: Data?
|
||||
var response: URLResponse?
|
||||
var error: Error?
|
||||
|
||||
let semaphore = DispatchSemaphore(value: 0)
|
||||
|
||||
let dataTask = self.dataTask(with: url) {
|
||||
data = $0
|
||||
response = $1
|
||||
error = $2
|
||||
|
||||
semaphore.signal()
|
||||
}
|
||||
dataTask.resume()
|
||||
|
||||
_ = semaphore.wait(timeout: .distantFuture)
|
||||
|
||||
return (data, response, error)
|
||||
}
|
||||
}
|
59
Pods/BaseAPI/BaseAPI/Classes/Request.swift
generated
Normal file
59
Pods/BaseAPI/BaseAPI/Classes/Request.swift
generated
Normal file
|
@ -0,0 +1,59 @@
|
|||
//
|
||||
// Request.swift
|
||||
// BaseAPI
|
||||
//
|
||||
// Created by Serhii Londar on 1/5/18.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public class Request {
|
||||
public var url: String
|
||||
public var method: RequestMethod
|
||||
public var parameters: [String : String]?
|
||||
public var headers: [String : String]?
|
||||
public var body: Data?
|
||||
|
||||
public init(url: String, method: RequestMethod, parameters: [String : String]? = nil, headers: [String : String]? = nil, body: Data? = nil) {
|
||||
self.url = url
|
||||
self.method = method
|
||||
self.parameters = parameters
|
||||
self.headers = headers
|
||||
self.body = body
|
||||
}
|
||||
|
||||
public func request() -> (request: URLRequest?, error: Error?) {
|
||||
let url = URL(string: self.urlWithParameters())
|
||||
if let url = url {
|
||||
var request = URLRequest(url: url)
|
||||
if let headers = headers {
|
||||
for headerKey in headers.keys {
|
||||
request.addValue(headers[headerKey]!, forHTTPHeaderField: headerKey)
|
||||
}
|
||||
}
|
||||
request.httpMethod = method.rawValue
|
||||
request.httpBody = body
|
||||
return (request, nil)
|
||||
} else {
|
||||
return (nil, "Unable to create URL")
|
||||
}
|
||||
}
|
||||
|
||||
func urlWithParameters() -> String {
|
||||
var retUrl = url
|
||||
if let parameters = parameters {
|
||||
if parameters.count > 0 {
|
||||
retUrl.append("?")
|
||||
parameters.keys.forEach {
|
||||
guard let value = parameters[$0] else { return }
|
||||
let escapedValue = value.addingPercentEncoding(withAllowedCharacters: CharacterSet.BaseAPI_URLQueryAllowedCharacterSet())
|
||||
if let escapedValue = escapedValue {
|
||||
retUrl.append("\($0)=\(escapedValue)&")
|
||||
}
|
||||
}
|
||||
retUrl.removeLast()
|
||||
}
|
||||
}
|
||||
return retUrl
|
||||
}
|
||||
}
|
29
Pods/BaseAPI/BaseAPI/Classes/RequestHeaderFields.swift
generated
Normal file
29
Pods/BaseAPI/BaseAPI/Classes/RequestHeaderFields.swift
generated
Normal file
|
@ -0,0 +1,29 @@
|
|||
//
|
||||
// RequestHeaderFields.swift
|
||||
// BaseAPI
|
||||
//
|
||||
// Created by Serhii Londar on 1/5/18.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public enum RequestHeaderFields: String {
|
||||
case acceptCharset = "Accept-Charset"
|
||||
case acceptEncoding = "Accept-Encoding"
|
||||
case acceptLanguage = "Accept-Language"
|
||||
case authorization = "Authorization"
|
||||
case expect = "Expect"
|
||||
case from = "From"
|
||||
case host = "Host"
|
||||
case ifMatch = "If-Match"
|
||||
case ifModifiedSince = "If-Modified-Since"
|
||||
case ifNoneMatch = "If-None-Match"
|
||||
case ifRange = "If-Range"
|
||||
case ifUnmodifiedSince = "If-Unmodified-Since"
|
||||
case maxForwards = "Max-Forwards"
|
||||
case proxyAuthorization = "Proxy-Authorization"
|
||||
case range = "Range"
|
||||
case referer = "Referer"
|
||||
case te = "TE"
|
||||
case userAgent = "User-Agent"
|
||||
}
|
20
Pods/BaseAPI/BaseAPI/Classes/RequestMethod.swift
generated
Normal file
20
Pods/BaseAPI/BaseAPI/Classes/RequestMethod.swift
generated
Normal file
|
@ -0,0 +1,20 @@
|
|||
//
|
||||
// RequestMethod.swift
|
||||
// BaseAPI
|
||||
//
|
||||
// Created by Serhii Londar on 1/5/18.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public enum RequestMethod: String {
|
||||
case OPTIONS
|
||||
case GET
|
||||
case HEAD
|
||||
case POST
|
||||
case PATCH
|
||||
case PUT
|
||||
case DELETE
|
||||
case TRACE
|
||||
case CONNECT
|
||||
}
|
19
Pods/BaseAPI/LICENSE
generated
Normal file
19
Pods/BaseAPI/LICENSE
generated
Normal file
|
@ -0,0 +1,19 @@
|
|||
Copyright (c) 2018 serhii-londar <serhii.londar@gmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
31
Pods/BaseAPI/README.md
generated
Normal file
31
Pods/BaseAPI/README.md
generated
Normal file
|
@ -0,0 +1,31 @@
|
|||
# BaseAPI
|
||||
|
||||
[](https://travis-ci.org/serhii-londar/BaseAPI)
|
||||
[](http://cocoapods.org/pods/BaseAPI)
|
||||
[](http://cocoapods.org/pods/BaseAPI)
|
||||
[](http://cocoapods.org/pods/BaseAPI)
|
||||
|
||||
BaseAPI is a small Swift library which helps you to implement any REST API. The main goal is to simplify sending HTTP request and receiving response.
|
||||
|
||||
## Example
|
||||
|
||||
To run the example project, clone the repo, and run `pod install` from the Example directory first.
|
||||
|
||||
## Requirements
|
||||
|
||||
## Installation
|
||||
|
||||
BaseAPI is available through [CocoaPods](http://cocoapods.org). To install
|
||||
it, simply add the following line to your Podfile:
|
||||
|
||||
```ruby
|
||||
pod 'BaseAPI'
|
||||
```
|
||||
|
||||
## Author
|
||||
|
||||
serhii-londar, serhii.londar@gmail.com
|
||||
|
||||
## License
|
||||
|
||||
BaseAPI is available under the MIT license. See the LICENSE file for more info.
|
Loading…
Add table
Add a link
Reference in a new issue