An open API service indexing awesome lists of open source software.

https://github.com/hlts2/httpswift

Simple http client library written swift
https://github.com/hlts2/httpswift

hlts2 http-client library networking rest simple swift swift-library

Last synced: about 1 month ago
JSON representation

Simple http client library written swift

Awesome Lists containing this project

README

          

# HttpSwift
HttpSwift is simple http client library for swift

## How to use
Fist, you need to import this library

```swift
import HttpSwift
```

## Basic example

### GET

The most basic request.

```swift

HTTP.Get(url: "https://www.google.co.jp/")
.do() { result in
switch result {
case .success(let value):
print(value)
case .failure(let error):
print(error)
}
}

```

We can also add parameters.

```swift

HTTP.Get(url: "https://www.google.co.jp/")
.setQuery(params: ["a": "1", "b": "2"])
.do() { result in
switch result {
case .success(let value):
print(value)
case .failure(let error):
print(error)
}
}

```

We can also add content-type. returned object is json selialized object.

```swift

HTTP.Get(url: "https://www.google.co.jp/")
.setQuery(params: ["a": "1", "b": "2"])
.setContentsType(.json)
.do() { result in
switch result {
case .success(let value):
print(value) //Json Serialized Object
case .failure(let error):
print(error)
}
}

```

We can also add User-Agent.

```swift

HTTP.Get(url: "https://www.google.co.jp/")
.setUserAgent(agent: "Agent_Name")
.do() { result in
switch result {
case .success(let value):
print(value)
case .failure(let error):
print(error)
}
}

```

We can also add Authorization.

```swift

HTTP.Get(url: "https://www.google.co.jp/")
.setAuth(token: "token string")
.do()
}

```

We can also add Basic Authorization.

```swift

HTTP.Get(url: "https://www.google.co.jp/")
.basicAuth(id: "id", pw: "pass word")
.do()

```

We can also add Proxy.

```swift

HTTP.Get(url: "https://www.google.co.jp/")
.setProxy(host: "192.168.3.30", port: "3030")
.do()
```

when reusing request. HTTP instance is singleton object.

```swift

let http = HTTP.instance

```

We can also switch handler type

```swift

//no handler
.do()

//with handler
.do({ result in

})
```

## Request type
All the common HTTTP Method is available

- GET

- POST

- PUT

- DELETE

- PATCH

### Request Cancel
You want to cancel the request. You call cancel method.

```swift
let http = HTTP.instance

http.cancel()
```

### Custom Header
You want to add the custom request header.

```swift
let http = HTTP.instance

http.setHeader(headers: ["test1": "1", "test2": "2"])
```

## Requirements
Swift3.0 or latter.

## Installation

HttpSwift is available through [Carthage](https://github.com/Carthage/Carthage) or
[Swift Package Manager](https://github.com/apple/swift-package-manager).

### Carthage

```
github "hlts2/HttpSwift"
```

for detail, please follow the [Carthage Instruction](https://github.com/Carthage/Carthage#if-youre-building-for-ios-tvos-or-watchos)

### Swift Package Manager

```
dependencies: [
.Package(url: "https://github.com/hlts2/HttpSwift.git", majorVersion: 1)
]
```

for detail, please follow the [Swift Package Manager Instruction](https://github.com/apple/swift-package-manager/blob/master/Documentation/Usage.md)