Howto requests nested resources using RESTkit for Objective-C

I have a pure RESTful API that provides me with the following endpoints

/ suppliers
/ manufacturers /: / country ID
/ vendors /: id / countries /: id / cities

I am inexperienced when it comes to Objective-C and RESTkit. Just at the moment when I am looking for a way to map objects on the server side with 3 classes on the client side: vendor, country, city.

So I would expect declarations for every class that

a) defines the endpoint at which JSON objects can be retrieved
b) defines a 1: n relationship from supplier to country and from country to city.

After that, I expect that I can do something like this [pseudocode]:

vendors = Vendors.all //retrieve all vendors and construct objects countries = vendors[0].countries //retrieve all countries of the first vendor city = countries.last.cities //retrieve the cities of the last countries 

Unfortunately, I do not see something like this in RESTkit. To be able to create relationships between objects, the API will have to supply nested resources! For example, a country endpoint call would have to provide the associated provider object directly in JSON countries?!?

This is something that I do not understand at all. In this case, I would use all kinds of legacy protocol and should not use the RESTful API.

Am I missing something? Can someone help with this topic or provide a link to a resource explaining RESTkit in more detail than documents?

+4
source share
2 answers

There is a section in the RestKit documentation: KVC-free mapping that covers this.

RKPathMatcher: The path connector evaluates URL patterns to create matches for patterns, such as "/ articles /: articleID", which matches either '/ articles / 1234' or '/ articles / some-great-article'.

https://github.com/RestKit/RestKit/wiki/Object-mapping#mapping-without-kvc

Note. I have not tried this yet, but the document seems to be updated for the latest version of RestKit (0.20.0)

0
source

Answering your question, yes, you can define relationships using RestKit, and the JSON representation must be nested, read ahead to see an example of this and how to map it to your objects.

You must follow these steps:

  • Create your objects using the properties you get from the API.

  • You need to establish the mappings associated with each of your objects and the JSON / XML that you get from the API.

  • Define mappings between your objects if the property of the object is another object.

From the Object Mapping Documentation :

You need to parse the following JSON:

 { "articles": [ { "title": "RestKit Object Mapping Intro", "body": "This article details how to use RestKit object mapping...", "author": { "name": "Blake Watters", "email": " blake@restkit.org " }, "publication_date": "7/4/2011" }] } 

Define your objects in Objective-c:

 //author.h @interface Author : NSObject @property (nonatomic, retain) NSString* name; @property (nonatomic, retain) NSString* email; @end //article.h @interface Article : NSObject @property (nonatomic, retain) NSString* title; @property (nonatomic, retain) NSString* body; @property (nonatomic, retain) Author* author; //Here we use the author object! @property (nonatomic, retain) NSDate* publicationDate; @end 

Define mappings:

 // Create our new Author mapping RKObjectMapping* authorMapping = [RKObjectMapping mappingForClass:[Author class]]; // NOTE: When your source and destination key paths are symmetrical, you can use mapAttributes: as a shortcut [authorMapping mapAttributes:@"name", @"email", nil]; // Now configure the Article mapping RKObjectMapping* articleMapping = [RKObjectMapping mappingForClass:[Article class]]; [articleMapping mapKeyPath:@"title" toAttribute:@"title"]; [articleMapping mapKeyPath:@"body" toAttribute:@"body"]; [articleMapping mapKeyPath:@"author" toAttribute:@"author"]; [articleMapping mapKeyPath:@"publication_date" toAttribute:@"publicationDate"]; // Define the relationship mapping [articleMapping mapKeyPath:@"author" toRelationship:@"author" withMapping:authorMapping]; [[RKObjectManager sharedManager].mappingProvider setMapping:articleMapping forKeyPath:@"articles"]; 

I hope this can be useful for you!

-2
source

Source: https://habr.com/ru/post/1393945/


All Articles