Simple HTTP Networking in Swift based on UrlSession β‘
SimpleAPI is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'SimpleAPI'import Foundation
let BASE_URL = "https://jsonplaceholder.typicode.com"
struct Endpoints {
static let posts = "\(BASE_URL)/posts"
}Model protocol will add 3 static properties to your struct
- endpoint
- params
- headers
π‘ Tip: add setParams() function so you don't have to specify params keys inside your viewController
import Foundation
import SimpleAPI
struct Post: Model {
//API
static var endpoint: String! = Endpoints.posts
static var params: Params?
static var headers: Headers? = ["Content-type": "application/json"]
static func setParams(title: String, body: String) {
self.params = ["title": title, "body": body]
}
//Properties
var title: String
var body: String
private enum CodingKeys : String, CodingKey {
case title = "title"
case body = "body"
}
}- just type
APIand specify the type of your<object>that you want to return - you have 2 functions :
objectto return one object,listto return list of objects -
objectfunction takes 2 parameters the first one ishttp methodenum like.get(id),.post - the second parameter is
decodeboolean itstrueby default - but if you didn't need to decode the response to consider it as a success make it
false
π‘ Tip: .get(id) it will add /id at the end of your end point automatically
//MARK: - get list of posts
func getPosts() {
API<Post>.list { result in
switch result {
case .success(let posts):
posts.forEach { post in
print(post!.title)
}
case .failure(let error):
print(error)
}
}
} //MARK: - get post with id
func getPost() {
API<Post>.object(.getWithoutID) { result in
switch result {
case .success(let post):
print(post!.title)
case .failure(let error):
print(error)
}
}
} //MARK: - get post with id
func getPost(id: String) {
API<Post>.object(.get(id)) { result in
switch result {
case .success(let post):
print(post!.title)
case .failure(let error):
print(error)
}
}
} //MARK: - set post
func setPost(title: String, body: String) {
Post.setParams(title: title, body: body)
API<Post>.object(.post) { result in
switch result {
case .success(let post):
print(post!.title)
case .failure(let error):
print(error)
}
}
} //MARK: - update post with id
func updatePost(id: String, title: String, body: String) {
Post.setParams(title: title, body: body)
API<Post>.object(.put(id)) { result in
switch result {
case .success(let post):
print(post!.body)
case .failure(let error):
print(error)
}
}
} //MARK: - delete post with id
func deletePost(id: String) {
API<Post>.object(.delete(id), decode: false) { result in
switch result {
case .success(_):
print("Deleted Successfully !")
case .failure(let error):
print(error)
}
}
} override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
getPosts()
}π‘ tip #1: you can customize your endpoint from your function as you want, like this
func getVideos(page: Int, genreID: String) {
Video.endpoint = "\(BASE_URL)\(CategoryName.movies)/genre/\(genreID)/\(page)" // << endpoint
API<Video>.list { [weak self] result in
// .
// .
// .π‘ tip #2: there're quicker versions of our 2 main funtions => quickObject & quickList
-
object&listcomes with success & failure callbacks - but
quickObject&quickListjust return the value directly - if quick functions fails it'll only print the error without customizations
API<Post>.quickObject(.get("5")) { [weak self] post in
self?.label.text = post?.title
}API<Post>.quickList() { posts in
posts.forEach { post in
print(post!.title)
}
}BelalSamy, belalsamy10@gmail.com
SimpleAPI is available under the MIT license. See the LICENSE file for more info.