App42 Cloud API

iOS cloud SDK

Session Management

The SessionManager manages user sessions on the server side. It is a persistent Session Manager. It allows to save attributes in the session and retrieve them. This SessionManager is especially useful for Mobile/Device Apps which want to do session management.

In order to use the various functions available under a particular API, a developer will have to allocate and initialize an instance of ServiceAPI. After initialization, apiKey and secretKey needs to be set which has been received while creating an application under App42.

ServiceAPI *serviceAPIObj = [[ServiceAPI alloc]init];
serviceAPIObj.apiKey = @"<API_KEY>";
serviceAPIObj.secretKey = @"<SECRET_KEY>";	

After initialization, developer needs to call the buildXXXService method on ServiceAPI instance to get the instance of the particular API that you wish to build. For example, To build an instance of SessionService, buildSessionService() method needs to be called.

SessionService *sessionService = [serviceAPIObj buildSessionService];

Once the API instance has been retrieved, You are ready to use the functions available for that particular API.

The methods available under a particular API will return the domain object (like Session in this case) as a response which will have the accessor / mutator to access the available properties for that object. You can get the response in the form of JSON as well. We have provided the JSON response with every function detail which can be retrieved by calling the toString() on the returned object.

NSString *userName = @"Nick";

Session *session = [sessionService getSession:userName];  /* returns the Session object. */  
NSLog(@"UserName is = %@",session.userName);
NSLog(@"Session id is = %@",session.sessionId);
NSLog(@"CreatedOn is = %@",session.createdOn);
NSString *jsonResponse = [session toString]; /* returns the response in JSON format. */
                            

The functions available under Session Management API can throw some exceptions in abnormal conditions. Example of the same has been given below.

E.g. If App developer is trying to invalidate a Session which is already in invalidated, the function will throw the App42Exception (as shown below) with message as "Not Found" and the appErrorCode as "2202" and the details as "Session with the id '<sessionId>' does not exist".

							NSString *sessionId = @"48dacb76-7f5e-4011-ac24-e09623bdc70f";
@try{
	App42Response *response = [sessionService invalidate:sessionId];  /* returns the Session object. */  
} @catch(App42Exception *ex) {
	int appErrorCode = ex.appErrorCode;
	int httpErrorCode = ex.httpErrorCode;
	if(appErrorCode == 2202)
{
    // Handle here for Not Found (Session with the id '<sessionId>' does not exist.)
}
    else if(appErrorCode  == 2203){
    // Handle here for Bad Request (The request parameters are invalid. Session with the Id '<sessionId>' is already invalidated.)
}
    else if(appErrorCode == 1401){
    // handle here for Client is not authorized
}
    else if(appErrorCode == 1500){
    // handle here for Internal Server Error
}	
	NSString *jsonText = ex.reason; /* returns the Exception text in JSON format. (as shown below)*/	
}
                            
	{
		"app42Fault": {
			"httpErrorCode": 400,
			"appErrorCode": 2202,
			"message": "Not Found",
			"details": "Session with the id '48dacb76-7f5e-4011-ac24-e09623bdc70f' does not exist"
		}
	}							

Below are the HTTP Error Codes and their description, the function under the Session Management API can throw.

/*
1400 - BAD REQUEST - The Request parameters are invalid
1401 - UNAUTHORIZED - Client is not authorized
1500 - INTERNAL SERVER ERROR - Internal Server Error. Please try again	
*/						

Below are the Application Error Codes and their description, the function under the Session Management API can throw.

/*
2200 - NOT FOUND - User by the name '<userName>' does not exist.
2201 - NOT FOUND - Session for the user '<userName>' does not exist.
2202 - NOT FOUND - Session with the id '<sessionId>' does not exist.
2203 - BAD REQUEST - The request parameters are invalid. Session with the Id '<sessionId>' is already invalidated.
2204 - NOT FOUND - Attribute with the name '<name>' for session id '<sessionId>' does not exist.
2205 - NOT FOUND - There are no attributes for session id '<sessionId>'.
2206 - NOT FOUND - Attributes for session id '<sessionId>' do not exist.
*/						

Various functions available under Session Management API has been explained below.

-(Session*)getSession:(NSString*)userName

Create Session for the User. If the session does not exist it will create a new session

Parameters:

userName - Username for which the session has to be created

Returns:

Session object containing the session id of the created session. This id has to be used for storing or retrieving attributes.

Response: Session Object

For more details click Exception
NSString *userName = @"Nick";
						
Session *session = [sessionService getSession:userName];  /* returns the Session object. */  
NSLog(@"User Name is = %@",session.userName);
NSLog(@"SessionId is = %@",session.sessionId);
NSLog(@"createdOn is = %@",session.createdOn);
NSString *jsonResponse = [session toString]; /* returns the response in JSON format. (as shown below)*/                            
{
    "app42": {
        "response": {
            "success": true,
            "session": {
                "userName": "Nick",
                "sessionId": "74f9b379-60f7-4d93-89f2-4aca536ba407",
                "createdOn": "2012-05-09T05:34:55.000Z"
            }
        }
    }
}
-(Session*)getSession:(NSString*)userName isCreate:(BOOL)isCreate

Create User Session based on the isCreate boolean parameter. If isCreate is true and there is an existing session for the user, the existing session is returned. If there is no existing session for the user a new one is created. If isCreate is false and there is an existing session, the existing session is returned if there is no existing session new one is not created.

Parameters:

userName - Username for which the session has to be created
isCreate - A boolean value for specifying if an existing session is not there, should a new one is to be created or not

Returns:

Session object containing the session id of the created session. This id has to be used for storing or retrieving attributes.

Response: Session Object

Exception:

This method throws following exception.
/*
2201 - NOT FOUND - Session for the user '<userName>' does not exist.
*/        
For more details click Exception
NSString *userName = @"Nick";
BOOL  isCreate = true;

Session *session = [sessionService getSession:userName isCreate:isCreate];  /* returns the Session object. */  
NSLog(@"User Name is = %@",session.userName);
NSLog(@"SessionId is = %@",session.sessionId);
NSLog(@"createdOn is = %@",session.createdOn);
NSString *jsonResponse = [session toString]; /* returns the response in JSON format. (as shown below)*/                           
								
{
    "app42": {
        "response": {
            "success": true,
            "session": {
                "userName": "Nick",
                "sessionId": "48dacb76-7f5e-4011-ac24-e09623bdc70f",
                "createdOn": "2012-05-09T05:38:43.000Z"
            }
        }
    }
}
-(App42Response*)invalidate:(NSString*)sessionId

Invalidate a session based on the session id. All the attributes store in the session will be lost.

Parameters:

sessionId - The session id for which the session has to be invalidated

Returns:

App42Response if invalidated successfully

Response: App42Response Object

Exception:

This method throws following exception.
/*
2202 - NOT FOUND - Session with the id '<sessionId>' does not exist.
2203 - BAD REQUEST - The request parameters are invalid. Session with the Id '<sessionId>' is already invalidated.
*/ 	        
For more details click Exception
 
NSString *sessionId = @"48dacb76-7f5e-4011-ac24-e09623bdc70f";

App42Response *response = [sessionService invalidate:sessionId]; /* returns the App42Response object. */
BOOL  success = response.isResponseSuccess;
NSString *jsonResponse = [session toString]; /* returns the response in JSON format. (as shown below)*/                            
{
    "app42": {
        "response": {
            "success": true,
            "session": {
                "userName": "Nick",
                "sessionId": "48dacb76-7f5e-4011-ac24-e09623bdc70f",
                "createdOn": "2012-05-09T05:34:55.000Z",
                "invalidatedOn": "2012-05-09T05:38:42.000Z"
            }
        }
    }
}
-(Session*)setAttribute:(NSString *)sessionId attributeName:(NSString *)attributeName attributeValue:(NSString*) attributeValue

Sets attribute in a session whose session id is provided. Attributes are stored in a key value pair.

Parameters:

sessionId - Session id for which the attribute has to be saved
attributeName - The attribute key that needs to be stored
attributeValue - The attribute value that needs to be stored

Returns:

Session object containing the attribute and value which is stored

Response: Session Object

Exception:

This method throws following exception.
/*
2202 - NOT FOUND - Session with the id '<sessionId>' does not exist.
2203 - BAD REQUEST - The request parameters are invalid. Session with the Id '<sessionId>' is already invalidated.
*/ 		        
For more details click Exception
NSString *sessionId = @"48dacb76-7f5e-4011-ac24-e09623bdc70f";
NSString *attributeName = @"attributeName01";
NSString *attributeValue =	@"attributeValue01";				

Session *session = [sessionService setAttribute:sessionId attributeName:attributeName attributeValue:attributeValue];  /* returns the Session object. */  
NSLog(@"sessionId is = %@",session.sessionId);
NSMutableArray *attributeList =  session.attributeArray; 
for(Attribute *attribute in attributeList)
{    
	NSLog(@"Name is = %@",attribute.name);
	NSLog(@"Value is = %@",attribute.value);
}
NSString *jsonResponse = [session toString]; /* returns the response in JSON format. (as shown below)*/         
{
    "app42": {
        "response": {
            "success": true,
            "session": {
                "sessionId": "48dacb76-7f5e-4011-ac24-e09623bdc70f",
                "attributes": {
                    "attribute": {
                        "name": "attributeName01",
                        "value": "attributeValue01"
                    }
                }
            }
        }
    }
}
-(Session*)getAttribute:(NSString *)sessionId attributeName:(NSString *)attributeName

Gets the attribute value in a session whose session id is provided.

Parameters:

sessionId - The session id for which the attribute has to be fetched
attributeName - The attribute key that has to be fetched

Returns:

Session object containing the attribute and value which is stored for the session id and attribute name

Response: Session Object

Exception:

This method throws following exception.
/*
2202 - NOT FOUND - Session with the id '<sessionId>' does not exist.
2203 - BAD REQUEST - The request parameters are invalid. Session with the Id '<sessionId>' is already invalidated.
2204 - NOT FOUND - Attribute with the name '<name>' for session id '<sessionId>' does not exist.
*/ 	        
For more details click Exception
NSString *sessionId = @"48dacb76-7f5e-4011-ac24-e09623bdc70f";
NSString *attributeName = @"attributeName01";
						
Session *session = [sessionService getAttribute:sessionId attributeName:attributeName];  /* returns the Session object. */  
NSLog(@"sessionId is = %@",session.sessionId);
NSMutableArray *attributeList =  session.attributeArray; 
for(Attribute *attribute in attributeList)
{    
	NSLog(@"Name is = %@",attribute.name);
	NSLog(@"Value is = %@",attribute.value);
}
NSString *jsonResponse = [session toString]; /* returns the response in JSON format. (as shown below)*/   
{
    "app42": {
        "response": {
            "success": true,
            "session": {
                "sessionId": "48dacb76-7f5e-4011-ac24-e09623bdc70f",
                "attributes": {
                    "attribute": {
                        "name": "attributeName01",
                        "value": "attributeValue01"
                    }
                }
            }
        }
    }
}
-(Session*)getAllAttributes:(NSString *)sessionId

Gets all the attributes for a given session id.

Parameters:

sessionId - The session id for which the attribute has to be fetched

Returns:

Session object containing all the attributes and values which are stored for the session id

Response: Session Object

Exception:

This method throws following exception.
/*
2202 - NOT FOUND - Session with the id '<sessionId>' does not exist.
2203 - BAD REQUEST - The request parameters are invalid. Session with the Id '<sessionId>' is already invalidated.
2205 - NOT FOUND - There are no attributes for session id '<sessionId>'.
*/ 	        
For more details click Exception
String sessionId = "48dacb76-7f5e-4011-ac24-e09623bdc70f";
						
Session *session = [sessionService getAllAttributes:sessionId];  /* returns the Session object. */  
NSLog(@"sessionId is = %@",session.sessionId);
NSMutableArray *attributeList =  session.attributeArray; 
for(Attribute *attribute in attributeList)
{    
	NSLog(@"Name is = %@",attribute.name);
	NSLog(@"Value is = %@",attribute.value);
}
NSString *jsonResponse = [session toString]; /* returns the response in JSON format. (as shown below)*/            
{
    "app42": {
        "response": {
            "success": true,
            "session": {
                "sessionId": "48dacb76-7f5e-4011-ac24-e09623bdc70f",
                "attributes": {
                    "attribute": [
                        {
                            "name": "attributeName01",
                            "value": "attributeValue01"
                        },
                        {
                            "name": "attributeName02",
                            "value": "attributeValue02"
                        }
                    ]
                }
            }
        }
    }
}
-(App42Response*)removeAttribute:(NSString*)sessionId attributeName:(NSString*)attributeName

Removes the attribute from a session whose session id is provided.

Parameters:

sessionId - The session id for which the attribute has to be removed
attributeName - The attribute key that has to be removed

Returns:

App42Response if removed successfully

Response: App42Response Object

Exception:

This method throws following exception.
/*
2202 - NOT FOUND - Session with the id '<sessionId>' does not exist.
2203 - BAD REQUEST - The request parameters are invalid. Session with the Id '<sessionId>' is already invalidated.
*/ 	        
NSString *sessionId = @"48dacb76-7f5e-4011-ac24-e09623bdc70f";
NSString *attributeName = @"attributeName01";
						
App42Response *response = [sessionService removeAttribute:sessionId attributeName:attributeName]; /* returns the App42Response object. */
BOOL  success = response.isResponseSuccess;
NSString *jsonResponse = [response toString]; /* returns the response in JSON format. (as shown below)*/                 
For more details click Exception
{
    "app42": {
        "response": {
            "success": true,
            "session": {
                "sessionId": "48dacb76-7f5e-4011-ac24-e09623bdc70f",
                "attributes": {
                    "attribute": {
                        "name": "attributeName01"
                    }
                }
            }
        }
    }
}
-(App42Response*)removeAllAttributes:(NSString*)sessionId

Removes all the attributes for a given session id

Parameters:

sessionId - The session id for which the attributes has to be removed

Returns:

App42Response if removed successfully

Response: App42Response Object

Exception:

This method throws following exception.
/*
2202 - NOT FOUND - Session with the id '<sessionId>' does not exist.
2203 - BAD REQUEST - The request parameters are invalid. Session with the Id '<sessionId>' is already invalidated.
2206 - NOT FOUND - Attributes for session id '<sessionId>' do not exist.
*/ 		        
For more details click Exception
NSString *sessionId = @"48dacb76-7f5e-4011-ac24-e09623bdc70f";
						
App42Response *response = [sessionService removeAllAttributes:sessionId]; /* returns the App42Response object. */
BOOL  success = response.isResponseSuccess;
NSString *jsonResponse = [response toString]; /* returns the response in JSON format. (as shown below)*/        
{
    "app42": {
        "response": {
            "success": true,
            "session": {
                "sessionId": "48dacb76-7f5e-4011-ac24-e09623bdc70f",
                "attributes": {
                    "attribute": [
                        {
                            "name": "attributeName01"
                        },
                        {
                            "name": "attributeName02"
                        }
                    ]
                }
            }
        }
    }
}