Skip to content

Commit

Permalink
Merge pull request #15 from ibm-bluemix-mobile-services/development
Browse files Browse the repository at this point in the history
Updated dependencies and Swift version
  • Loading branch information
AnthonyOliveri authored Feb 20, 2017
2 parents 60faebd + f7e7ca1 commit 06cf53a
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0.1
3.0.2
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ language: generic

env:
global:
- SWIFT_VERSION=3.0.1
- SWIFT_VERSION=3.0.2

notifications:
email:
Expand Down
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PackageDescription
let package = Package(
name: "SimpleHttpClient",
dependencies:[
.Package(url: "https://github.com/ibm-bluemix-mobile-services/bluemix-simple-logger-swift.git", majorVersion: 0, minor: 4),
.Package(url: "https://github.com/IBM-Swift/Kitura-net.git", majorVersion: 1, minor: 1)
.Package(url: "https://github.com/ibm-bluemix-mobile-services/bluemix-simple-logger-swift.git", majorVersion: 0, minor: 5),
.Package(url: "https://github.com/IBM-Swift/Kitura-net.git", majorVersion: 1, minor: 6)
]
)
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ let package = Package(
)
```

* 0.5.x releases were tested on OSX and Linux with 3.0.1
* 0.5.x releases were tested on OSX and Linux with DEVELOPMENT-SNAPSHOT-2016-09-07-a
* 0.4.x releases were tested on OSX and Linux with DEVELOPMENT-SNAPSHOT-2016-06-20-a
* 0.3.x releases were tested on OSX and Linux with DEVELOPMENT-SNAPSHOT-2016-06-06-a
* 0.2.x releases were tested on OSX and Linux with DEVELOPMENT-SNAPSHOT-2016-05-03-a
* 0.1.x releases were tested on OSX and Linux with DEVELOPMENT-SNAPSHOT-2016-04-25-a
* 0.6.x releases were tested on OSX and Linux with Swift 3.0.2
* 0.5.x releases were tested on OSX and Linux with Swift 3.0.1
* 0.5.x releases were tested on OSX and Linux with Swift DEVELOPMENT-SNAPSHOT-2016-09-07-a
* 0.4.x releases were tested on OSX and Linux with Swift DEVELOPMENT-SNAPSHOT-2016-06-20-a
* 0.3.x releases were tested on OSX and Linux with Swift DEVELOPMENT-SNAPSHOT-2016-06-06-a
* 0.2.x releases were tested on OSX and Linux with Swift DEVELOPMENT-SNAPSHOT-2016-05-03-a
* 0.1.x releases were tested on OSX and Linux with Swift DEVELOPMENT-SNAPSHOT-2016-04-25-a

## Setup

Expand Down
124 changes: 65 additions & 59 deletions Sources/HttpClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,98 +27,104 @@ public class HttpClient{
//public static let urlSession = URLSession(configuration: URLSessionConfiguration.default)

/**
Send a GET request
- Parameter resource: HttpResource instance describing URI schema, host, port and path
- Parameter headers: Dictionary of Http headers to add to request
- Parameter completionHandler: NetworkRequestCompletionHandler instance
Send a GET request

- parameter resource: HttpResource instance describing URI schema, host, port and path
- parameter headers: Dictionary of Http headers to add to request
- parameter completionHandler: NetworkRequestCompletionHandler instance
*/
public class func get(resource: HttpResource, headers:[String:String]? = nil, completionHandler: @escaping NetworkRequestCompletionHandler = NOOPNetworkRequestCompletionHandler){

HttpClient.sendRequest(to: resource, method: "GET" , headers: headers, completionHandler: completionHandler)
}

/**
Send a PUT request
- Parameter resource: HttpResource instance describing URI schema, host, port and path
- Parameter headers: Dictionary of Http headers to add to request
- Parameter data: The data to send in request body
- Parameter completionHandler: NetworkRequestCompletionHandler instance
Send a PUT request

- parameter resource: HttpResource instance describing URI schema, host, port and path
- parameter headers: Dictionary of Http headers to add to request
- parameter data: The data to send in request body
- parameter completionHandler: NetworkRequestCompletionHandler instance
*/
public class func put(resource: HttpResource, headers:[String:String]? = nil, data:Data? = nil, completionHandler: @escaping NetworkRequestCompletionHandler = NOOPNetworkRequestCompletionHandler){
HttpClient.sendRequest(to: resource, method: "PUT" , headers: headers, data: data, completionHandler: completionHandler)
}

/**
Send a DELETE request
- Parameter resource: HttpResource instance describing URI schema, host, port and path
- Parameter headers: Dictionary of Http headers to add to request
- Parameter completionHandler: NetworkRequestCompletionHandler instance
Send a DELETE request

- parameter resource: HttpResource instance describing URI schema, host, port and path
- parameter headers: Dictionary of Http headers to add to request
- parameter completionHandler: NetworkRequestCompletionHandler instance
*/
public class func delete(resource: HttpResource, headers:[String:String]? = nil, completionHandler: @escaping NetworkRequestCompletionHandler = NOOPNetworkRequestCompletionHandler){
HttpClient.sendRequest(to: resource, method: "DELETE" , headers: headers, completionHandler: completionHandler)
}

/**
Send a POST request
- Parameter resource: HttpResource instance describing URI schema, host, port and path
- Parameter headers: Dictionary of Http headers to add to request
- Parameter data: The data to send in request body
- Parameter completionHandler: NetworkRequestCompletionHandler instance
Send a POST request

- parameter resource: HttpResource instance describing URI schema, host, port and path
- parameter headers: Dictionary of Http headers to add to request
- parameter data: The data to send in request body
- parameter completionHandler: NetworkRequestCompletionHandler instance
*/
public class func post(resource: HttpResource, headers:[String:String]? = nil, data:Data? = nil, completionHandler: @escaping NetworkRequestCompletionHandler = NOOPNetworkRequestCompletionHandler){
HttpClient.sendRequest(to: resource, method: "POST" , headers: headers, data: data, completionHandler: completionHandler)
}

/**
Send a HEAD request
- Parameter resource: HttpResource instance describing URI schema, host, port and path
- Parameter headers: Dictionary of Http headers to add to request
- Parameter completionHandler: NetworkRequestCompletionHandler instance
Send a HEAD request

- parameter resource: HttpResource instance describing URI schema, host, port and path
- parameter headers: Dictionary of Http headers to add to request
- parameter completionHandler: NetworkRequestCompletionHandler instance
*/
public class func head(resource: HttpResource, headers:[String:String]? = nil, completionHandler: @escaping NetworkRequestCompletionHandler = NOOPNetworkRequestCompletionHandler){
HttpClient.sendRequest(to: resource, method: "HEAD" , headers: headers, completionHandler: completionHandler)
}

/**
Send a request

- parameter resource: HttpResource instance describing URI schema, host, port and path
- parameter method: The HTTP method to use
- parameter headers: Dictionary of Http headers to add to request
- parameter data: The data to send in request body
- parameter completionHandler: NetworkRequestCompletionHandler instance
*/
public class func sendRequest(to resource: HttpResource, method:String, headers:[String:String]? = nil, data: Data? = nil, completionHandler: @escaping NetworkRequestCompletionHandler = NOOPNetworkRequestCompletionHandler){


var requestOptions = Array<ClientRequest.Options>()

requestOptions.append(.method(method))
requestOptions.append(.schema(resource.schema + "://"))
requestOptions.append(.hostname(resource.host))
requestOptions.append(.path(resource.path))

let request = HTTP.request(requestOptions) { (response) in
handleResponse(response: response, completionHandler: completionHandler)
}

if let headers = headers {
for (name, value) in headers{
request.headers[name] = value
}
}

logger.debug("Sending \(method) request to \(resource.uri)")

if let data = data {
request.end(data)
} else {
request.end()
}
}
}

internal extension HttpClient {

/**
Send a request
- Parameter url: The URL to send request to
- Parameter method: The HTTP method to use
- Parameter contentType: The value of a 'Content-Type' header
- Parameter data: The data to send in request body
- Parameter completionHandler: NetworkRequestCompletionHandler instance
*/
internal class func sendRequest(to resource: HttpResource, method:String, headers:[String:String]? = nil, data: Data? = nil, completionHandler: @escaping NetworkRequestCompletionHandler = NOOPNetworkRequestCompletionHandler){


var requestOptions = Array<ClientRequest.Options>()

requestOptions.append(.method(method))
requestOptions.append(.schema(resource.schema + "://"))
requestOptions.append(.hostname(resource.host))
requestOptions.append(.path(resource.path))

let request = HTTP.request(requestOptions) { (response) in
handleResponse(response: response, completionHandler: completionHandler)
}

if let headers = headers {
for (name, value) in headers{
request.headers[name] = value
}
}

logger.debug("Sending \(method) request to \(resource.uri)")

if let data = data {
request.end(data)
} else {
request.end()
}
}

internal class func handleResponse(response: ClientResponse?, completionHandler: NetworkRequestCompletionHandler){
if let response = response {

Expand Down
14 changes: 7 additions & 7 deletions Sources/HttpResource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ public struct HttpResource{
}

/**
Initialize the HttpResource by specifying all properties
Initialize the HttpResource by specifying all properties

- Parameter schema: Request schema, should be either http or https
- Parameter host: Resource host name, e.g. www.example.com
- Parameter port: Resource port, e.g. 80
- Parameter path: Resource path, e.g. /my/resource/id/123
- parameter schema: Request schema, should be either http or https
- parameter host: Resource host name, e.g. www.example.com
- parameter port: Resource port, e.g. 80
- parameter path: Resource path, e.g. /my/resource/id/123
*/

public init(schema:String, host: String, port: String? = nil, path: String = "") {
Expand All @@ -52,9 +52,9 @@ public struct HttpResource{
}

/**
Create a new HttpResource by adding components to path
Create a new HttpResource by adding components to path

- Parameter pathComponent: components to add
- parameter pathComponent: components to add
*/
public func resourceByAddingPathComponent(pathComponent:String) -> HttpResource {
return HttpResource(schema: self.schema, host: self.host, port: self.port, path: self.path + pathComponent)
Expand Down

0 comments on commit 06cf53a

Please sign in to comment.