Created
August 21, 2013 13:24
-
-
Save RajaveluC/6294414 to your computer and use it in GitHub Desktop.
These set of files are intended to demonstrate a typical MVC architecture and how data flows between the different layers. Code snippet for Service class goes here.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ProfileService.h | |
// Project A | |
// | |
// Created by Company A on 7/25/13. | |
// Copyright (c) 2013 Company A. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
#import "UserProfile.h" | |
#import "ASIHTTPRequest.h" | |
@protocol UserProfileServiceDelegate <NSObject> | |
@optional | |
-(void)didReceiveCreateProfileResponseWithCode:(int)code error:(NSString*)error; | |
-(void)didReceiveGetProfileResponseWithCode:(UserProfile*)profile error:(NSString*)error; | |
-(void)didReceiveUpdateProfileResponseWithCode:(UserProfile*)profile error:(NSString*)error; | |
@end | |
@interface UserProfileService : NSObject | |
{ | |
ASIHTTPRequest *asiHTTPRequest; | |
UserProfile *userProfile; | |
} | |
@property (nonatomic, strong) id<UserProfileServiceDelegate>delegate; | |
// method to create a new user profile | |
-(void)createProfile:(UserProfile*)profile; | |
// method to retreive an existing user profile with userId | |
-(void)getProfileForId:(int)userId; | |
// method to update the user profile | |
-(void)updateProfile:(UserProfile*)profile; | |
@end | |
// | |
// ProfileService.m | |
// Project A | |
// | |
// Created by Company A on 7/25/13. | |
// Copyright (c) 2013 Company A. All rights reserved. | |
// | |
#import "UserProfileService.h" | |
@implementation UserProfileService | |
@synthesize delegate; | |
- (id)init { | |
self = [super init]; | |
return self; | |
} | |
-(void)buildRequestWithURL:(NSURL*)url Tag:(int)tag andMethod:(NSString*)methodType payload:(NSString*)payload | |
{ | |
asiHTTPRequest = [ASIHTTPRequest requestWithURL:url]; | |
[asiHTTPRequest setDelegate:self]; | |
[asiHTTPRequest setRequestMethod:methodType]; | |
[asiHTTPRequest setValidatesSecureCertificate:NO]; | |
[asiHTTPRequest setTimeOutSeconds:40.0]; | |
[asiHTTPRequest setTag:tag]; | |
[asiHTTPRequest addRequestHeader:@"Content-Type" value:@"application/json"]; | |
if (payload) { | |
NSData *postBody=[payload dataUsingEncoding:NSUTF8StringEncoding]; | |
[asiHTTPRequest setPostBody:[NSMutableData dataWithData:postBody]]; | |
} | |
[asiHTTPRequest startAsynchronous]; | |
} | |
#pragma mark - ASIHTTP callbacks | |
- (void)requestFinished:(ASIHTTPRequest *)request | |
{ | |
if (request.tag == 1) { | |
[self handleCreateProfileResponse:request]; | |
} | |
if (request.tag == 2) { | |
[self handleGetProfileResponse:request]; | |
} | |
if (request.tag == 3) { | |
[self handleUpdateProfileResponse:request]; | |
} | |
} | |
- (void)requestFailed:(ASIHTTPRequest *)request | |
{ | |
// Handle the service error | |
NSError *error = [request error]; | |
NSLog(@"Service Error : %@",error); | |
if (request.tag == 1) { | |
if ([delegate respondsToSelector:@selector(didReceiveCreateProfileResponseWithCode:error:)]) { | |
[delegate didReceiveCreateProfileResponseWithCode:0 error:error.localizedDescription]; | |
} | |
} | |
if (request.tag == 2) { | |
if ([delegate respondsToSelector:@selector(didReceiveGetProfileResponseWithCode:error:)]) { | |
[delegate didReceiveGetProfileResponseWithCode:0 error:error.localizedDescription]; | |
} | |
} | |
if (request.tag == 3) { | |
if ([delegate respondsToSelector:@selector(didReceiveUpdateProfileResponseWithCode:error:)]) { | |
[delegate didReceiveUpdateProfileResponseWithCode:0 error:error.localizedDescription]; | |
} | |
} | |
} | |
#pragma mark - Create Profile | |
// Provide the following details in UserProfile Obj | |
// 1. emailId | |
// 2. username | |
// 3. password | |
-(void)createProfile:(UserProfile*)profile | |
{ | |
if ([CommonUtilities isConnectedToInternet]) { | |
userProfile = [[UserProfile alloc]init]; | |
userProfile = profile; | |
NSString *urlString = @"Service URL to create a profile"; | |
NSURL *url = [NSURL URLWithString:urlString]; | |
NSString *payload=[NSString stringWithFormat:@"{\"email_id\": %@," | |
@"\"password\": %@," | |
@"\"user_name\":%@}", | |
profile.email_id, | |
[CommonUtilities getMD5:profile.password], | |
profile.user_name]; | |
[self buildRequestWithURL:url Tag:1 andMethod:@"PUT" payload:payload]; | |
} | |
else | |
{ | |
// Handle offline use case | |
NSLog(@"No internet connection"); | |
if ([delegate respondsToSelector:@selector(didReceiveCreateProfileResponseWithCode:error:)]) { | |
[delegate didReceiveCreateProfileResponseWithCode:0 error:CONST_CHECK_INTERNET]; | |
} | |
} | |
} | |
-(void)handleCreateProfileResponse:(ASIHTTPRequest*)request | |
{ | |
// Handle the response | |
NSData *response = [request responseData]; | |
int reponseStatus = [request responseStatusCode]; | |
// pasring the JSON response | |
NSError *error; | |
NSMutableDictionary *resultDictionary = [NSJSONSerialization JSONObjectWithData:response options:kNilOptions error:&error]; | |
if (reponseStatus == 200) { | |
// success | |
UserProfile *userprofile = [[UserProfile alloc]initWithDictionary:resultDictionary]; | |
if ([delegate respondsToSelector:@selector(didReceiveCreateProfileResponseWithCode:error:)]) { | |
[delegate didReceiveCreateProfileResponseWithCode:200 error:nil]; | |
} | |
} | |
else | |
{ | |
// failure | |
NSString *description = [resultDictionary objectForKey:@"status_description"]; | |
if ([delegate respondsToSelector:@selector(didReceiveCreateProfileResponseWithCode:error:)]) { | |
[delegate didReceiveCreateProfileResponseWithCode:0 error:description]; | |
} | |
} | |
} | |
#pragma mark - Get Profile | |
-(void)getProfileForId:(int)userId | |
{ | |
if ([CommonUtilities isConnectedToInternet]) { | |
NSString *urlString = @"Service URL to get a profile"; | |
NSURL *url = [NSURL URLWithString:urlString]; | |
[self buildRequestWithURL:url Tag:2 andMethod:@"GET" payload:nil]; | |
} | |
else | |
{ | |
// Handle offline use case | |
NSLog(@"No internet connection"); | |
if ([delegate respondsToSelector:@selector(didReceiveGetProfileResponseWithCode:error:)]) { | |
[delegate didReceiveGetProfileResponseWithCode:0 error:CONST_CHECK_INTERNET]; | |
} | |
} | |
} | |
-(void)handleGetProfileResponse:(ASIHTTPRequest*)request | |
{ | |
// Handle the response | |
NSData *response = [request responseData]; | |
int reponseStatus = [request responseStatusCode]; | |
// pasring the JSON response | |
NSError *error; | |
NSMutableDictionary *resultDictionary = [NSJSONSerialization JSONObjectWithData:response options:kNilOptions error:&error]; | |
if (reponseStatus == 200) { | |
// success | |
UserProfile *userprofile = [[UserProfile alloc]initWithDictionary:resultDictionary]; | |
if ([delegate respondsToSelector:@selector(didReceiveGetProfileResponseWithCode:error:)]) { | |
[delegate didReceiveGetProfileResponseWithCode:userprofile error:nil]; | |
} | |
} | |
else | |
{ | |
// failure | |
NSString *description = [resultDictionary objectForKey:@"status_description"]; | |
if ([delegate respondsToSelector:@selector(didReceiveGetProfileResponseWithCode:error:)]) { | |
[delegate didReceiveGetProfileResponseWithCode:0 error:description]; | |
} | |
} | |
} | |
#pragma mark - Update Profile | |
// Provide the updated details in UserProfile Obj | |
-(void)updateProfile:(UserProfile*)profile | |
{ | |
if ([CommonUtilities isConnectedToInternet]) { | |
userProfile = [[UserProfile alloc]init]; | |
userProfile = profile; | |
NSString *urlString = @"Service URL to update a profile"; | |
NSURL *url = [NSURL URLWithString:urlString]; | |
NSString *payload=[NSString stringWithFormat:@"{\"abt_me\": %@," | |
@"\"email_id\": %@," | |
@"\"id\": %@," | |
@"\"location\": %@," | |
@"\"image\": %@," | |
@"\"password\": %@," | |
@"\"user_name\":%@}", | |
profile.abt_me, | |
profile.email_id, | |
profile.id, | |
profile.location, | |
profile.image, | |
profile.password, | |
profile.user_name]; | |
[self buildRequestWithURL:url Tag:3 andMethod:@"POST" payload:payload]; | |
} | |
else | |
{ | |
// Handle offline use case | |
NSLog(@"No internet connection"); | |
if ([delegate respondsToSelector:@selector(didReceiveUpdateProfileResponseWithCode:error:)]) { | |
[delegate didReceiveUpdateProfileResponseWithCode:0 error:CONST_CHECK_INTERNET]; | |
} | |
} | |
} | |
-(void)handleUpdateProfileResponse:(ASIHTTPRequest*)request | |
{ | |
// Handle the response | |
NSData *response = [request responseData]; | |
int reponseStatus = [request responseStatusCode]; | |
// pasring the JSON response | |
NSError *error; | |
NSMutableDictionary *resultDictionary = [NSJSONSerialization JSONObjectWithData:response options:kNilOptions error:&error]; | |
if (reponseStatus == 200) { | |
// success | |
UserProfile *userprofile = [[UserProfile alloc]initWithDictionary:resultDictionary]; | |
if ([delegate respondsToSelector:@selector(didReceiveUpdateProfileResponseWithCode:error:)]) { | |
[delegate didReceiveUpdateProfileResponseWithCode:userprofile error:nil]; | |
} | |
} | |
else | |
{ | |
// failure | |
NSString *description = [resultDictionary objectForKey:@"status_description"]; | |
if ([delegate respondsToSelector:@selector(didReceiveUpdateProfileResponseWithCode:error:)]) { | |
[delegate didReceiveUpdateProfileResponseWithCode:0 error:description]; | |
} | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment