Simple lightweight HTTP Networking Library written in Swift based on UrlSession
- Swift 5.0+
- iOS 13+
SimpleAPI is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod'SimpleAPI','~> 2.0.7'import Foundation
import SimpleAPI
structPost:Model{
//API => Model protocol will add 3 static properties to communicate with API
staticvarendpoint:String!="https://jsonplaceholder.typicode.com/posts"staticvarparams:Params?staticvarheaders:Headers?=["Content-type":"application/json"]
//Properties
vartitle:Stringvarbody:String}Quicker Version => return value directly
API<Post>.object{ post in // do whatever you want with "post" object ... }
API<Post>.list{ posts in // do whatever you want with "posts" array ... }
Longer Version => handle success & failure cases
API<Post>.objectResult{ result inswitch result {case.success(let post): // do whatever you want with "post" object ... case .failure(let error):print(error)}}
API<Post>.listResult{ result inswitch result {case.success(let posts): // do whatever you want with "posts" array ... case .failure(let error):print(error)}}
| Parameters | Value | Notes |
|---|---|---|
| HTTPMethod [enum] | .get() [default], .put(), .delete(), .post() | you could pass an endpoint extension for specfic request through http methods |
| encoding [enum] | .json [default], .url | to change body parameters encoding => json encoded or url encoded |
| decoding - [Bool] | true [default], false | if your API response is empty or not return object from the same type , you need to set decoding to false |
you could pass an endpoint extension ( custom paths/queries) for specfic request through http methods
// Post.endpoint = "https://jsonplaceholder.typicode.com/posts" [original]
// .get("1") => https://jsonplaceholder.typicode.com/posts/1 🆕
// .get("?page=1&search=movie") => https://jsonplaceholder.typicode.com/posts?page=1&search=movie 🆕API<Post>.object{ post inself.label.text = post.title }API<Post>.object(.get("1")){ post inself.label.text = post.title }API<Post>.object(.put("1")){ post in
// ...
}API<Post>.object(.delete("1"), decoding:false){ post in
// ...
}API<Token>.object(.post, encoding:.url){ token in
// ...
}- SimpleAPI run on the main thread, so you could work with your UI directly
- Endpoints support Arabic characters and spacing with percent-encoding by default
BelalSamy, belalsamy10@gmail.com
SimpleAPI is available under the MIT license. See the LICENSE file for more info.
