CocoaPods trunk is moving to be read-only. Read more on the blog, there are 18 months to go.

JCSwiftRestful 1.0.4

JCSwiftRestful 1.0.4

Maintained by James.



JCSwiftRestful

CI Status Version License Platform

Introduction

Here are three frameworks for junior developers. They can help you increase development efficiency and write more standardized, maintainable code.:

JCSwiftCommon: for extensions, some common function, and a lightweight local storage tool based on file system IO.

JCSwiftRestful: for Restful APIs. It helps you focus more on handling object-oriented and structured data. To use this framework, you will have to write code using more standard RESTful semantics, both on iOS and server sides. Otherwise, the automatic serialization and deserialization functions within this framework will not work.

JCSwiftUIWedgets: contains some custom components. Since many native SwiftUI methods do not support iOS 13 or 14, I have written some components to support these versions. And all components support "theme mode", which meaning you only need to modify one config, and the appearance will change everywhere.

Example

To use JCSwiftRestful, the HTTP response MUST adhere to standard RESTful formats. Which means: when the status code is 200, the responseData must follow a single data format, and 5XX codes should be used to indicate parameter errors or other issues. List of HTTP status codes.

For example, if an API returns:

{statusCode: 200, responseData: [Person]}

And in any other case with different parameters sent from client, this API SHOULD NOT returns like:

{statusCode: 200, responseData: Person}

{statusCode: 200, responseData: { errorMsg: "Parameter is incorrect" }}

###3 steps to get results from a RESTful API:

*Step 1: Have a default implementation for JCRequestData, which is a protocol

extension JCRequestData {
 var method: JCHttpMethod {
   return .get
 }

 var parameter: Codable? {
   return nil
 }

 var header: [String: String] {
   var header = [String: String]()
   header["Accept"] = "application/json, text/plain, */*"
   header["Accept-Language"] = "en-US,en;q=0.9"
   header["Content-Type"] = "application/json"
   header["source"] = "iOS"
//    if let token = UserManager.shared.userToken, token.count != 0 {
//      header["Authorization"] = "userToken"
//    }
   return header
 }
}

*Step 2: Looking at the json string in response and translate to a Swift Struct/Class

$ curl http://ip.jsontest.com
{"ip": "24.84.236.255"}

Translate to (Here I name it as IpTestRequestData, and it's' response only have one property is "ip" as a String):

private struct IpTestRequestData: JCRequestData {
  struct Response: Codable {
    var ip: String
  }

  var apiPath: String {
    "http://ip.jsontest.com"
  }
}

*Step 3: Send request and get result

Task {
    let result = try? await JCRequestCenter.shared.sendRequest(IpTestRequestData(), decodeType: IpTestRequestData.Response.self)
    print(result?.ip ?? "Error")
}

Requirements

iOS Deployment Target >= 13.0

Installation

JCSwiftRestful is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'JCSwiftRestful'

Author

James, [email protected], LinkedIn

Fanny, [email protected]

License

JCSwiftRestful is available under the MIT license. See the LICENSE file for more info.