版本 1.0.3 的Gem

Gem 1.0.3

Albin CR 维护。



Gem 1.0.3

  • Albin CR

Gem

一个极轻量级、高性能的系统,可在应用程序中使用闭包自动解析器管理所有HTTP请求

Swift 4.1 Platforms iOS Xcode 9.2 Gemnasium Ratting license

在应用程序中使用闭包自动解析器调用管理所有HTTP请求

要求

  • iOS 10.0+
  • Swift 4.1+
  • Xcode 9.2+

安装

CocoaPods

任务通过 CocoaPods 提供使用。要安装它,只需将以下行添加到您的Podfile中

use_frameworks!

pod "Gem"

use_frameworks!

pod 'Gem', git: 'https://github.com/Albinzr/Gem', :tag => '1.0.3'

Carthage

要使用Carthage在Xcode项目中集成Task,请在Cartfile中指定它

github "Albinzr/Gem"

使用及需求

  1. 所有模型类都应该遵循Codable协议
  2. 变量命名规则如下
           
           //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
           }

示例

GET请求

//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)
            
        }

响应 - 包含有关网络调用(如状态码等)的所有细节

POST请求

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":"[email protected]",
            "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)
            
        }
        

图像上传

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)
            
        }

PR/请求/建议总是受欢迎