FMPUtils.RequestResponseParser

Description

The FMPUtils.RequestResponseParser class is used to handle JSON responses as a result of requests to server. It inherits from the FMPUtils.Parser class.

class RequestResponseParser : FMPUtils.Parser

Methods

Method Description
getRawError() Get error from the raw field in JSON structure. It returns request error.
func getRawError() -> FMPError?
handleResponse() Handle the current JSON structure. It returns the tuple containing server response (success, error, result).
func handleResponse() -> FMPRequestResponse

Examples

Get error from the raw field:

let json: NSDictionary = ["status" : "error", "result" : ["raw" : ["code" : 503, "description": "Service Unavailable"]]]
  
// Build an instance of the FMPUtils.RequestResponseParser class. For details see examples for FMPUtils.RequestResponseParser.Builder.
let requestResponseParser: FMPUtils.RequestResponseParser = FMPUtils().requestResponse
    .json(json)
    .build()
  
// Get error
let error: FMPError? = requestResponseParser.getRawError() // FMPError.error(code: "503", description: "Service Unavailable")

Handle server json response:

let json: NSDictionary = ["status" : "ok", "result" : ["key" : "value"]]
  
// Build an instance of the FMPUtils.RequestResponseParser class. For details see examples for FMPUtils.RequestResponseParser.Builder.
let requestResponseParser: FMPUtils.RequestResponseParser = FMPUtils().requestResponse
    .json(json)
    .build()
  
// Get handled server response
let result: FMPRequestResponse? = requestResponseParser.handleResponse() // (success: true, error: nil, result: ["key" : "value"])

See also:

FMPWrapper Framework | Classes