Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

The Anatomy of Apps - How iPhone, Android & Facebook Apps Consume APIs

9,209 views

Published on

Building mobile apps is different from building web apps. Developers need to understand the unique demands that mobile apps place on APIs and considerations across different client and back-end platforms. Mobile apps also introduce new issues for security and scalability.

WARNING: These slides contain lots of code.

We Will Discuss »


Why building mobile apps is different
A roundup of different client libraries and frameworks
Considerations for selecting mobile back-end platforms

Published in: Technology

The Anatomy of Apps - How iPhone, Android & Facebook Apps Consume APIs

  1. 1. The Anatomy of AppsHow iPhone, Android & Facebook Apps Consume APIsEd Anuff@edanuffSam Ramji@sramjiBrian Mulloy@landlessness Apigee @apigee
  2. 2. groups.google.com/group/api-craft
  3. 3. youtube.com/apigee
  4. 4. New! IRC Channel #api-craft
  5. 5. App App App World of API Internal App APIUser Store Developer APIs Team Systems
  6. 6. Dogs API/dogs/ownersAuthorizationOAuth 2.0RESTful API Design - Second Editionhttp://www.youtube.com/watch?v=QpAhXa12xvU
  7. 7. Build an iPhone App, an Android App and aFacebook Web App**Ruby on Rails app hosted on Herokuhttp://devcenter.heroku.com/articles/facebook
  8. 8. App App App World of API Internal App APIUser Store Developer APIs Team Systems
  9. 9. Start with a basic HTTP request
  10. 10. AndroidHttpClient client = new DefaultHttpClient();HttpGet httpGet = new HttpGet("http://api.apizoo.com/v1/dogs");HttpResponse response = client.execute(httpGet);iOSNSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://api.apizoo.com/v1/dogs"]];NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];Ruby on Railsrequire net/httpresponse = Net::HTTP.get(‘api.apizoo.com/v1’, ‘/dogs’)
  11. 11. Parse the data
  12. 12. AndroidJSONObject dogs = new JSONObject(response);iOSimport "JSONKit.h"NSDictionary *dogs = [response objectFromData];Ruby on Railsrequire yajlparser = Yajl::Parser.newdogs = parser.parse(response) # returns a hash
  13. 13. Resource Object Mapping
  14. 14. RouteMapUse
  15. 15. Android Spring MobileRouterequestEntity = new HttpEntity<Object>(requestHeaders);ResponseEntity<Dog> responseEntity = restTemplate.exchange("http://api.apizoo.com/v1/dogs/15", HttpMethod.GET, requestEntity, Dog.class);
  16. 16. Android Spring MobileMap// Handled with introspection
  17. 17. Android Spring MobileUseDog dog = responseEntity.getBody()
  18. 18. iOS RestKitRoute#import <RestKit/RestKit.h>RKObjectManager* manager = [RKObjectManager objectManagerWithBaseURL:@"http://api.apizoo.com/v1"];RKDynamicRouter* router = [[RKDynamicRouter new] autorelease];manager.router = router;[router routeClass:[Dog class] toResourcePath:@"/dogs" forMethod:RKRequestMethodPOST];[router routeClass:[Dog class] toResourcePath:@"/dogs/(dogID)"];
  19. 19. iOS RestKitMap@implementation Dog+ (NSDictionary*)elementToPropertyMappings { return [NSDictionary dictionaryWithKeysAndObjects: @"name", @"color", nil];}@end
  20. 20. iOS RestKitUseDog* dog = [Dog object];dog.name = @"Rover";Dog.color = @"red";[[RKObjectManager sharedManager] postObject:dog delegate:self];[[RKObjectManager sharedManager] deleteObject:dog delegate:self];
  21. 21. Ruby on Rails ActiveResourceRouteresource :dogs
  22. 22. Ruby on Rails ActiveResourceMapclass Dog < ActiveResource::Base self.site = "http://api.apizoo.com/v1"end
  23. 23. Ruby on Rails ActiveResourceUsedog = Dog.new name: ‘Rover’, color: ‘red’dog.savedog.destroy
  24. 24. Cache the response in a database
  25. 25. Android Jersey + JacksonUsageRoll your own
  26. 26. iOS RestKit + Core DataUsage#import <RestKit/CoreData/CoreData.h>RKObjectManager* manager = [RKObjectManager objectManagerWithBaseURL:@"http://api.apizoo.com/v1"];manager.objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"DogApp.sqlite"];@implementation Dog + (NSString*)primaryKeyProperty { return @"dogID"; }@end
  27. 27. Ruby on Rails ActiveResource + ActiveRecordUsageclass DogResource < ActiveResource::Base self.site = http://api.apizoo.com/v1endclass Dog < ActiveRecord::Base # the before methods call DogResource methods before_create :create_resource before_update :update_resource before_destroy :destroy_resourceend
  28. 28. Simple to do list
  29. 29. Problem: we want new capabilities in our app notsupported by APIs.
  30. 30. Usergrid - Data & Querieshttp://www.youtube.com/watch?v=zLl56sU5Bt0
  31. 31. Androidusergrid_ sdkiOSUse RESTKitRuby on RailsProbably not applicable
  32. 32. What about offline cases
  33. 33. AndroidRoll your owniOS-(BOOL)reachable {Reachability *r = [Reachability reachabilityWithHostName:@"api.apizoo.com/v1"];NetworkStatus internetStatus = [r currentReachabilityStatus];if(internetStatus == NotReachable) { return NO;}return YES;}Ruby on RailsProbably not applicable
  34. 34. Get authorization out of the wayAssumption: the APIs we consume use OAuth 2
  35. 35. AndroidRoll your own
  36. 36. iOS RestKitRKObjectManager* objectManager = [RKObjectManager sharedManager];objectManager.client.baseURL = @”api.apizoo.com/v1";objectManager.client.OAuth2AccessToken = @"YOUR ACCESS TOKEN";objectManager.client.authenticationType = RKRequestAuthenticationTypeOAuth2;
  37. 37. Ruby on Rails Oauth Gem@consumer=OAuth::Consumer.new("key","secret", site: "api.apizoo.com/v1")@request_token=@consumer.get_request_tokensession[:request_token]=@request_tokenredirect_to @request_token.authorize_url@access_token=@request_token.get_access_token
  38. 38. AndroidBuilt-in JSON, etc.Spring Mobile (bundles Jersey & Jackson)iOSJSONKitRestKitCore DataRuby on RailsYAJLActiveResourceOauth
  39. 39. Coming Up: March Miniseries on Apps & APIs
  40. 40. Questions?
  41. 41. THANK YOUSubscribe to API webinars at:youtube.com/apigee
  42. 42. THANK YOUChat on IRC#api-craft
  43. 43. THANK YOUQuestions and ideas to:groups.google.com/group/api-craft
  44. 44. THANK YOUContact us at:@edanuffed@apigee.com@sramjisramji@apigee.com@landlessnessbrian@apigee.com

×