Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/meniny/ghost
👻 A versatile HTTP(s) networking framework written in Swift.
https://github.com/meniny/ghost
codable curl download ios json macos multipart-form networking stream swift tls tvos upload urlsession watchos
Last synced: 2 months ago
JSON representation
👻 A versatile HTTP(s) networking framework written in Swift.
- Host: GitHub
- URL: https://github.com/meniny/ghost
- Owner: Meniny
- License: mit
- Created: 2018-01-25T01:30:14.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-05-19T10:42:06.000Z (over 6 years ago)
- Last Synced: 2024-10-10T15:48:33.596Z (2 months ago)
- Topics: codable, curl, download, ios, json, macos, multipart-form, networking, stream, swift, tls, tvos, upload, urlsession, watchos
- Language: Swift
- Homepage: https://meniny.cn/Ghost/
- Size: 2.08 MB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Introduction
**Ghost** is a versatile HTTP(s) networking framework written in Swift.
## 🌟 Features
- [x] Chainable Request / Response Methods
- [x] Asynchronous & synchronous task execution
- [x] Basic, Bearer & Custom Authorization Handling
- [x] `URL` / `JSON` / `Property List` Parameter Encoding
- [x] Upload File / `Data` / `Stream` / `Multipart Form Data`
- [x] Download File using Request / Resume Data
- [x] Authentication with `URLCredential`
- [x] Custom Cache Controls
- [x] Custom Content Types
- [x] Upload & Download Progress Closures
- [x] `cURL` Command Debug Output
- [x] Request & Response Interceptors
- [x] Inference of response object type
- [x] Network reachability
- [x] `TLS Certificate` & `Public Key Pinning`
- [x] Retry requests
- [x] `Codable` protocols compatible (`JSON` / `Property List`)
- [x] `watchOS` Compatible
- [x] `tvOS` Compatible
- [x] `macOS` Compatible## 📋 Requirements
- iOS 8.0+
- macOS 10.9+
- tvOS 9.0+
- watchOS 2.0+
- Xcode 9.0+ with Swift 4.0+## 📲 Installation
Ghost is available on [CocoaPods](https://cocoapods.org):
```ruby
use_frameworks!
pod 'Ghost'
```## 🔧 Usage
### Build a GhostRequest
```swift
import Ghostdo {
let request = try GhostRequest.builder("YOUR_URL")!
.setAccept(.json)
.setCache(.reloadIgnoringLocalCacheData)
.setMethod(.PATCH)
.setTimeout(20)
.setJSONBody(["foo", "bar"])
.setContentType(.json)
.setServiceType(.background)
.setCacheControls([.maxAge(500)])
.setURLParameters(["foo": "bar"])
.setAcceptEncodings([.gzip, .deflate])
.setBasicAuthorization(user: "user", password: "password")
.setHeaders(["foo": "bar"])
.build()
} catch {
print("Request error: \(error)")
}
```### Request asynchronously
```swift
import Ghostlet ghost = GhostURLSession()
ghost.data(URL(string: "YOUR_URL")!).async { (response, error) in
do {
if let object: [AnyHashable: Any] = try response?.object() {
print("Response dictionary: \(object)")
} else if let error = error {
print("Net error: \(error)")
}
} catch {
print("Parse error: \(error)")
}
}
```### Request synchronously
```swift
import Ghostlet ghost = GhostURLSession()
do {
let object: [AnyHashable: Any] = try ghost.data("YOUR_URL").sync().object()
print("Response dictionary: \(object)")
} catch {
print("Error: \(error)")
}
```### Request from cache
```swift
import Ghostlet ghost = GhostURLSession()
do {
let object: [AnyHashable: Any] = try ghost.data("YOUR_URL").cached().object()
print("Response dictionary: \(object)")
} catch {
print("Error: \(error)")
}
```### Track progress
```swift
import Ghostlet ghost = GhostURLSession()
do {
let task = try ghost.data("YOUR_URL").progress({ progress in
print(progress)
}).sync()
} catch {
print("Error: \(error)")
}
```### Add interceptors for all requests
```swift
import Ghostlet ghost = GhostURLSession()
ghost.addRequestInterceptor { request in
request.addHeader("foo", value: "bar")
request.setBearerAuthorization(token: "token")
return request
}
```### Retry requests
```swift
import Ghostlet ghost = GhostURLSession()
ghost.retryClosure = { response, _, _ in response?.statusCode == XXX }
do {
let task = try ghost.data("YOUR_URL").retry({ response, error, retryCount in
return retryCount < 2
}).sync()
} catch {
print("Error: \(error)")
}
```## 🧙♂️ Codable
### Encodable
```swift
import Ghostlet request = GhostRequest.builder("YOUR_URL")!
.setJSONObject(Encodable())
.build()
```### Decodable
```swift
import Ghostlet ghost = URLSession()
do {
let object: Decodable = try ghost.data("YOUR_URL").sync().decode()
print("Response object: \(object)")
} catch {
print("Error: \(error)")
}
```## 🌙 GhostHunter
```swift
let url = URL.init(string: "YOUR_URL")!
do {
try GhostHunter.async(.GET, url: url, parameters: ["name": "elias"], headers: ["Content-Type": "text/json"], progress: { (pregress) in
print(pregress)
}, completion: { (response, error) in
do {
if let result: SomeCodableType = try response?.decode() {
print("GhostHunter Asynchronous: \(result)")
} else if let error = error {
print("GhostHunter Asynchronous: Ghost error: \(error)")
}
} catch {
print("GhostHunter: Parse error: \(error)")
}
}
} catch {
print("GhostHunter: Request error: \(error)")
}
```## ❤️ Contribution
You are welcome to fork and submit pull requests.
## 🔖 License
`Ghost` is open-sourced software, licensed under the `MIT` license.