2

I need to perform a GET request to a url, with optional URL parameters.

The basic URL looks something like http://host/user/explore, with the option of querying http://host/user/explore?who=VALUE&category=VALUE&service=VALUE for more accurate results.

For the first case the response comes with keys at the root, such as:

{
  "professionals": [
    {
        "_id": "59e8576cb524cf44a435844b"
    }
   ],
 "salons": [
    {
        "_id": "59e857bbb524cf44a4358454"
    }
   ]
}

And I am able of successfully mapping the response by setting up my response descriptor such as:

RKResponseDescriptor *response = [RKResponseDescriptor
                                  responseDescriptorWithMapping:[Explore map1]
                                  method:RKRequestMethodGET
                                  pathPattern:@"user/explore"
                                  keyPath:nil                    
       statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
return response;

(note that the [Explore map1] accounts for mapping an NSArray of "Professional" and another one made up of "Salon" NSObjects)

However, when I query http://host/user/explore?who=VALUE&category=VALUE&service=VALUE, my response JSON becomes:

{
  "_id": "59f6f9fc36e51720da2e85f4"
},
{
  "_id": "92x2f9fc36e51720da2e66d5"
}

Where the actual returned objects are all of the same type (and no longer have a key at the root).

So now obviously, my RKResponseDescriptor and RKObjectMapping are failing to properly handle this case.

I've looked into using RKDynamicMapping or RKRoute and wasn't able of really getting a handle on how they work or if they're even applicable to my case...

Priest
  • 242
  • 4
  • 11

1 Answers1

0

you url is: http://host/user/explore?who=VALUE&category=VALUE&service=VALUE so just do as this:

    let postMapping: RKObjectMapping = RKObjectMapping(for: GetFoo.self)
    postMapping.addAttributeMappings(from:["who","category","service"])

    // Define response decriptor

    let statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClass.successful)
    let resDescriptor = RKResponseDescriptor(mapping: postMapping, method: RKRequestMethod.GET, pathPattern: "user/explore", keyPath: nil, statusCodes: statusCodes)

    // Create object manager

    let url = URL(string: "http://host")

    let jsonPlaceholderManager = RKObjectManager(baseURL: url)
    jsonPlaceholderManager?.addResponseDescriptor(resDescriptor)
    RKObjectManager.setShared(jsonPlaceholderManager)

    RKObjectManager.shared().getObjectsAtPath("/user/explore?who=VALUE&category=VALUE&service=VALUE", parameters: nil, success: { (operation, mappingResult) -> Void in

        let dataResponse = operation?.httpRequestOperation.responseData
        let jsonData = try? JSONSerialization.jsonObject(with: dataResponse!, options: .mutableContainers) as? NSMutableDictionary
        print(dataResponse)