SOA Server is a great way to build your Web Services, it's built on top of Silex and Doctrine 2, but using iOS as a client you couldn't get the same experience, until now. After using SOA Server for a long time I decided to use the same mindset with Objective-C and the result is this library.
##Getting SOA iOS SDK
###Cocoapods
pod 'SOA-iOS-SDK', '~> 0.1.3'
##Configure it In order to get SOA SDK working, you must configure the API URL and shared headers.
- apiURL for the REST Calls
- rpcURL for the RPC Calls
[SOAManager defaultManager].apiURL = [NSURL URLWithString:@"http://serveraddress.com/api/v1"];
[SOAManager defaultManager].rpcURL = [NSURL URLWithString:@"http://serveraddress.com/rpc/v1"];
[[SOAManager defaultManager] setDefaultHeaders:@{@"Authorization" : @"_authorization_token_"}];
Including shared headers for each HTTP Method available at REST API.
SOAServiceMethodGET,
SOAServiceMethodPOST,
SOAServiceMethodPUT,
SOAServiceMethodDELETE,
SOAServiceMethodALL
[[SOAManager defaultManager] setDefaultHeaders:@{@"Content-Type" : @"application/x-www-form-urlencoded"} forMethod:SOAServiceMethodPOST];
[[SOAManager defaultManager] setDefaultHeaders:@{@"Content-Type" : @"application/x-www-form-urlencoded"} forMethod:SOAServiceMethodPUT];
###Miscelaneous Right now, you can configure the queue concurrent requests limits and you can also delegate to SOA SDK the responsability to display or hide the Network Activity Status.
[SOAManager defaultManager].maxConcurrentRequests = 4;
[SOAManager defaultManager].controlNetworkActivity = YES;
##The SOA Object
SOAObject is a NSObject subclass built to storage entity's data. SOAObject has only one mandatory property, which is Entity Name, but you can set the entity id. Both properties are intended to identify the object.
Beyond it, you can store any kind of data (pointers) using setValue:forKey: and you can also retrieve any data using valueForKey: method.
###Creating New
SOAObject *hotel = [[SOAObject alloc] initWithEntityName:@"hotel"];
SOAObject *knownHotel = [[SOAObject alloc] initWithEntityName:@"hotel" entityId:12];
###Dealing with data
[hotel setValue:@"Ibis" forKey:@"name"];
[hotel setValue:@"R. Nove de Março" forKey:@"street"];
NSString *name = [hotel valueForKey:@"name"];
###Fetching an Object
[SOAObject getWithEntityName:@"hotel"
entityId:12
completionBlock:^(id result, NSError *error) {
SOAObject *object = (SOAObject *)result;
}];
###Saving an Object #####Static Way
[SOAObject saveEntity:@"hotel"
parameters:@{
@"id" : @(10),
@"name" : @"Bourbon",
@"street" : @"R. Visconde de Taunay"
}
completionBlock:^(id result, NSError *error) {
}];
#####Instance Way
[hotel saveWithCompletionBlock:^(id result, NSError *error) {
}];
###Deleting an Object #####Static Way
[SOAObject getWithEntityName:@"hotel"
entityId:12
completionBlock:^(id result, NSError *error) {
SOAObject *object = (SOAObject *)result;
}];
#####Instance Way
[hotel deleteWithCompletionBlock:^(id result, NSError *error) {
}];
##The SOA Query ###Building a query
SOAQuery *query = [[SOAQuery alloc] initWithEntityName:@"hotel"];
query.offset = 0;
query.limit = 100;
query.fields = @[@"name", @"id"];
###SOA Filter
SOAFilter *filter = [SOAFilter where:@"id" equalTo:@26];
[query addFilter:filter];
###SOA Join
SOAFilter *joinFilter = [SOAFilter where:@"id" equalTo:@10];
SOAJoin *join = [SOAJoin joinField:@"user" withFilter:joinFilter];
[query addJoin:join];
###Perform Query
[query performQueryWithCompletionBlock:^(id result, NSError *error) {
NSLog(@"%@", result);
}];
##The SOA RPC Call
SOARPCCall *rpc = [[SOARPCCall alloc] init];
[rpc callProcedure:@"/authentication/login"
parameters:@{@"email" : @"user@email.com", @"password" : @"passwd"}
headers:nil
completionBlock:^(id result, NSError *error) {
NSLog(@"%@", result);
NSLog(@"%@", error);
}];
##Dealing with authorization errors From version 0.1.3, you can observe authorization failures using NSNotificationCenter, the notification name is SOAServiceAuthErrorNotification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selector:) name:SOAServiceAuthErrorNotification object:nil];