Latest 1.0.0
Homepage https://github.com/paleksandrs/APNetworking
License MIT
Platforms ios 9.0

license MIT

A convenient way to make REST API calls and parse data from JSON responses.

Requirements

  • Xcode 8
  • Swift 3.0
  • iOS 9.0+

Installation

CocoaPods. Please use the latest CocoaPods as this framework is using Swift 3

use_frameworks!

pod 'APNetworking'

Usage

Use JsonRequestto create post, get, put or delete request. Create your own parser confirming to GenericResponseParser, set it for the request using angle brackets and get fully parsed object back.

Example:

Say you have an API call that returns JSON of photos:

[
  {
    "albumId": 1,
    "id": 1,
    "title": "accusamus beatae ad facilis cum similique qui sunt",
    "url": "http://placehold.it/600/92c952",
    "thumbnailUrl": "http://placehold.it/150/92c952"
  },
  {
    "albumId": 1,
    "id": 2,
    "title": "reprehenderit est deserunt velit ipsam",
    "url": "http://placehold.it/600/771796",
    "thumbnailUrl": "http://placehold.it/150/771796"
  }
]

1) Create an object that represents a single photo:

struct Photo {
    let albumId: Int
    let id: Int
    let title: String
    let url: String
    let thumbnailUrl: String
}

2) Create a parser that will be responsible of parsing the response:

struct GetPhotosParser: GenericResponseParser {

    typealias GenericReponseEntity = [Photo]

    func parse(json: Json?) -> [Photo]? {

        guard  let posts = json as? [[String : Any]] else {
            return nil
        }

        return posts.flatMap { return parsePhoto($0)}
    }

   private func parsePhoto(_ node: [String: Any]) -> Photo? {

        guard let albumId = node["albumId"] as? Int,
            let id = node["id"] as? Int,
            let title = node["title"] as? String,
            let url = node["url"] as? String,
            let thumbnailUrl = node["thumbnailUrl"] as? String
            else {

            return nil
        }

        return Photo(albumId: albumId, id: id, title: title, url: url, thumbnailUrl: thumbnailUrl)
    }
}

3) Make the call:

let url = URL(string: "https://jsonplaceholder.typicode.com/photos")!
let request = JsonRequest<GetPhotosParser>()

request.get(url: url, success: { photos in    
    for photo in photos {
        print(photo.title)
    }       
}) { (error) in     
    print(error)
}
Same way you can make post, get, put or delete requests. You can also pass custom headers to the call.

License

APNetworking is released under an MIT License. See LICENSE for details.

Latest podspec

{
    "name": "APNetworking",
    "version": "1.0.0",
    "summary": "A convenient way to make Rest API calls.",
    "description": "A convenient way to make Rest API calls and parse data from JSON responses.",
    "homepage": "https://github.com/paleksandrs/APNetworking",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": "Aleksandrs Proskurins",
    "platforms": {
        "ios": "9.0"
    },
    "source": {
        "git": "https://github.com/paleksandrs/APNetworking.git",
        "tag": "1.0.0"
    },
    "source_files": "APNetworking/*.swift",
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This