Awesome
Gem
A light weight network library with automated model parser for rapid development.
Managing all http request with automated model parser calls in app with closure
Requirements
- iOS 10.0+
- Swift 4.1+
- Xcode 9.2+
Installation
CocoaPods
Task is available through CocoaPods. To install it, simply add the following line to your Podfile:
use_frameworks!
pod "Gem"
or
use_frameworks!
pod 'Gem', git: 'https://github.com/Albinzr/Gem', :tag => '1.0.3'
Carthage
To integrate Task into your Xcode project using Carthage, specify it in your Cartfile:
github "Albinzr/Gem"
Usage & Requirement
- All model class should have Codable protocol
- Naming for variable should be as follow
//json
{
name:"Albin CR",
published_on:123455,
Time:"23.10"
}
// model class
class Details:Codable{
var name:String? // same variable as in json
var publishedOn:Int? // incase of snake casing use camel casing of the same name
var Time:String // same variable as in json
}
Example
Get Request
//model
class User:Codable{
var id:Int?
var name:String?
var username:String?
var email:String?
var address:Address?
var phone:String?
var website:String?
var company:Company?
}
Gem.request(url: "https://jsonplaceholder.typicode.com/uses", method: Methods.get, model:User.self,
Success: { (data, response) in
// success block
print(data,response)
}) { (error, response) in
//error block
print(error,response)
}
response - contain all details related to network call like status code etc..
Post Request
class User:Codable{
var id:Int?
var name:String?
var username:String?
var email:String?
var address:Address?
var phone:String?
var website:String?
var company:Company?
}
let param: [String : Any] = [
"id":12324,
"name":"Albin CR",
"username":"Albi",
"email":"albinzr@gmail.com",
"phone":"8907575123",
"website":"www.albin.in",
"company":[
"name":"Quin",
"catchPhrase":"Time to change",
"bs":"Pika",
]
]
Gem.request(url: "https://jsonplaceholder.typicode.com/posts", method: Methods.post,parameter:param,header:nil, model:User.self, Success: { (data, response) in
print(data,response ?? "")
print("success")
}) { (error, response) in
print(error ?? "",response!.statusCode)
}
ImageUpload
class ImageUpload:Codable{
var link:String?
var width:Float?
var height:Float?
var id:String?
}
let image:UIImage = UIImage(named: "scan") // or image url
let imageData:Data = UIImagePNGRepresentation(image)!
let base64Data = imageData.base64EncodedString()
let param:[String:Any] = [
"image":base64Data
]
let header:[String:String] = [
"Authorization":"Client-ID {{your client key}}",//replace your client key
"Content-type":"multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'"
]
Gem.request(url: "https://api.imgur.com/3/image", method: Methods.post,parameter:param,header:header, model:ImageUpload.self, Success: { (data, response) in
print(data,response ?? "")
print("success")
}) { (error, response) in
print(error ?? "",response!.statusCode)
}