Code Monkey home page Code Monkey logo

networkmapper's Introduction

NetworkMapper

A framework to map JSON responses to swift objects, based on Alamofire and Unbox.

## Installation

Cocoapods

pod 'NetworkMapper', '~> 0.1.23'

Usage

NetworkMapper makes use of protocols to implement functionality. The protocols only require you to define instance variables for the method, url and parameters of the request.

Basic Request

You can make basic requests that don't map the response by conforming to NetworkRequest. You can use responseJSON or responseData to retrieve the response from the server.

struct ExampleRequest: NetworkRequest {
  let method: HTTPMethod = .get
  let url: URL = URL(string: "https://example.org/example/user")
  let parameters: [String:Any]? = nil
}

ExampleRequest().responseJSON { response in
  switch response.result {
  case .failure(let error):
    // process error
  case .success(let json):
    // process response
  }
}

Mapped Request

A mapped request is slightly more complicated. Mapped requests require you to create an object that conforms to NetworkObjectRequest and NetworkObjectResponse.

The response protocol requires you to conform to the Unboxable protocol, see it's documentation for more information. The response object is treated as the root object of the JSON response. Currently arrays as a root object is not supported.

The request protocol adds on to NetworkRequest by adding an associatedtype, which specifies the type of response object. On completion of the request, the JSON object will be mapped to the response object. There is one other function, responseDecoded which can be optionally implemented, the default implementation of which does nothing.

struct ExampleObjectRequest: NetworkObjectRequest {
  typealias ResponseType = ExampleObjectResponse

  let method: HTTPMethod = .get
  let url: URL = URL(string: "https://example.org/example/users")
  let parameters: [String:Any]? = nil
}

struct ExampleObjectResponse: NetworkObjectResponse {
  let users: [User]
  let pageNumber: Int

  init(unboxer: Unboxable) throws {
    self.users = try unboxer.unbox(key: "users")
    self.pageNumber = try unboxer.unbox(keyPath: "pagination.page_number")
  }
}

struct User: Unboxable {
  let name: String
  let age: Int

  init(unboxer: Unboxable) throws {
    self.name = try unboxer.unbox(key: "name")
    self.name = try unboxer.unbox(key: "age")
  }
}

ExampleObjectRequest().responseObject { response in
  switch response.result {
  case .failure(let error):
    // process error
  case .success(let object):
    // process response
  }
}

networkmapper's People

Contributors

adamdebono avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.