Latest 0.2
Homepage https://github.com/Noobish1/KeyedAPIParameters
License MIT
Platforms ios 8.0, requires ARC
Authors

Build Status codebeat badge codecov

A concept for API parameters in Swift.

Requirements

  • Xcode 8+
  • Swift 3+
  • iOS 8+

This framework is based around a few main components which I will outline below.

APIParamConvertible

APIParamConvertible is a protocol which defines an object that can safely be turned into a value for a given HTTP method. The reason we pass in the HTTP method is that different HTTP methods may require different output. For example GET requests require `String`s whereas POST requests can use anything can be encoded to JSON.

“`swift
public protocol APIParamConvertible {
func value(forHTTPMethod method: HTTPMethod) -> Any
}
“`

By looking in `APIParamConvertible.swift` you can see the built-in convertible types.

APIParamValue

APIParamValue is a wrapper for all the types that we allow in API parameters, more can be added over time, this is just a decent start.

“`swift
public enum APIParamValue: APIParamConvertible {
case convertible(APIParamConvertible)
case optionalConvertible(APIParamConvertible?)
case arrayConvertible([APIParamConvertible])
case null
case timestampInMillis(Date)
}
“`

APIParameters

`APIParameters` is the first level of protocols which you can make your parameters conform to. The reason `APIParameters` exists is it lets you have `String` keys without having to make an enum, which some may prefer.

“`swift
public protocol APIParameters: APIParamConvertible {
func toParamDictionary() -> [String : APIParamValue]
}
“`

A basic example would be:
“`swift
import KeyedAPIParameters

struct Object {
let stringProperty: String
}

extension Object: APIParameters {
func toParamDictionary() -> [String : APIParamValue] {
return [“stringProperty” : .convertible(stringProperty)]
}
}
“`

KeyedAPIParameters

`KeyedAPIParameters` is the highest level of protocols you can make your parameters conform to. The protocol forces you to define an enum for the parameter keys.

“`swift
public protocol KeyedAPIParameters: APIParameters {
associatedtype Key: ParamJSONKey

func toKeyedDictionary() -> [Key: APIParamValue]
}
“`

A basic example would be:
“`swift
import KeyedAPIParameters

struct Object {
let stringProperty: String
}

extension Object: KeyedAPIParameters {
enum Key: String, ParamJSONKey {
case stringProperty
}

func toKeyedDictionary() -> [Key : APIParamValue] {
return [.stringProperty : .convertible(stringProperty)]
}
}
“`

Full example

“`swift
import KeyedAPIParameters

struct InnerObject {
let innerStringProperty: String
}

extension InnerObject: KeyedAPIParameters {
enum Key: String, ParamJSONKey {
case innerStringProperty
}

func toKeyedDictionary() -> [Key : APIParamValue] {
return [.innerStringProperty : .convertible(innerStringProperty)]
}
}

struct Object {
let stringProperty: String
let intProperty: Int
let floatProperty: Float
let doubleProperty: Double
let boolProperty: Bool
let optionalProperty: String?
let arrayProperty: [String]
let nestedProperty: InnerObject
}

extension Object: KeyedAPIParameters {
enum Key: String, ParamJSONKey {
case stringProperty
case intProperty
case floatProperty
case doubleProperty
case boolProperty
case optionalProperty
case arrayProperty
case nestedProperty
}

func toKeyedDictionary() -> [Key : APIParamValue] {
return [
.stringProperty: .convertible(stringProperty),
.intProperty: .convertible(intProperty),
.floatProperty: .convertible(intProperty),
.doubleProperty: .convertible(doubleProperty),
.boolProperty: .convertible(boolProperty),
.optionalProperty: .optionalConvertible(optionalProperty),
.arrayProperty: .arrayConvertible(arrayProperty),
.nestedProperty: .convertible(nestedProperty)
]
}
}
“`

Latest podspec

{
    "name": "KeyedAPIParameters",
    "version": "0.2",
    "summary": "A concept for API parameters in Swift",
    "homepage": "https://github.com/Noobish1/KeyedAPIParameters",
    "license": "MIT",
    "authors": {
        "Blair McArthur": "[email protected]"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source": {
        "git": "https://github.com/Noobish1/KeyedAPIParameters.git",
        "tag": "0.2"
    },
    "requires_arc": true,
    "source_files": "KeyedAPIParameters/**/*.swift",
    "module_name": "KeyedAPIParameters",
    "pushed_with_swift_version": "4.0"
}

Pin It on Pinterest

Share This