App42 Cloud API

iOS cloud SDK

Message

Message API consists of Message and Queue.

Message - Service for sending, pulling and removing Message from a particular queue. Asynchronous messages can be sent to a queue which can be pulled using various receive messages.

Queue - Manages Asynchronous queues. Allows to create, delete, purge messages, view pending messages and get all messages.

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 QueueService, buildQueueService() method needs to be called.

QueueService *queueService = [serviceAPIObj buildQueueService];

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 Queue 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 *queueName =  @"MyQueue";
NSString *queueDescription = @"Hi its testing Time";   
						
Queue *queue = [queueService createPullQueue:queueName description:description];  /* returns the Queue object. */ 
NSLog(@"queueName is = %@",queue.queueName);
NSLog(@"queueType is= %@",queue.queueType);
NSLog(@"queueDescription is = %@",queue.description);
NSString *jsonResponse = [queue toString]; /* returns the response in JSON format. */ 
                            

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

E.g. If App developer is sending a message on the queue which has not been created, the function will throw the App42Exception (as shown below) with message as "Not Found" and the appErrorCode as "2400" and the details as "Queue with name {queueName} not found".

							
NSString *queueName = @"MyQueue";
NSString *msg = @"Its a Real Message";
long exp = 100000;

@try{
    Queue *queue = [queueService sendMessage:queueName message:msg expiryTime:exp];
} @catch(App42Exception *ex) {
	int appErrorCode = ex.appErrorCode;
	int httpErrorCode = ex.httpErrorCode;
	if(appErrorCode == 2400)
{
    // Handle here for Not Found (Queue with the name '<queueName>' not found.)
}
    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": 404,
			"appErrorCode": 2400,
			"message": "Not Found",
			"details": "Queue with name pull not found"
		}
	}				

Below are the HTTP Error Codes and their description, the function under the Message 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 Message API can throw.

/*
2400 - NOT FOUND - Queue with name {queueName} not found 
2401 - NOT FOUND - Queue with name {queueName} does not have any messages 
2402 - NOT FOUND -  Queue with name {queueName} and CorrelationId {correlationId} does not have any messages 
2403 - BAD REQUEST - Queue by the name {queueName} for the App {serviceName} already exists 
2404 - BAD REQUEST - The Request parameters are invalid. This action is only applicable to PULL type Queue 
2405 - NOT FOUND - Queue with name {queueName} does not have any pending messages 
*/
							

Various functions available under Message API has been explained below.

-(Queue*)receiveMessage:(NSString*)queueName receiveTimeOut:(long)receiveTimeOut

Pull all the message from the queue.

Parameters:

queueName - The name of the queue from which messages have to be pulled
receiveTimeOut - Receive time out (in milli seconds)

Returns:

Queue object containing messages which have been pulled

Response: Queue Object

Exception:

This method throws following exception.
/*
2400 - NOT FOUND - Queue with the name '<queueName>' not found.
2401 - NOT FOUND - Queue with the name '<queueName>' does not have any messages.
*/ 		          
For more details click Exception
NSString *queueName = @"MyQueue";
long receiveTimeOut = 10000;				
Queue *queue = [queueService receiveMessage:queueName receiveTimeOut:receiveTimeOut];  /* returns the Queue object. */ 
NSLog(@"queueName is = %@",queue.queueName);
NSLog(@"queueType is= %@",queue.queueType);
NSMutableArray *messageList = [queue messageArray];	
for(Message *message in messageList)
{  
	NSLog(@"correlationId is  = %@",message.correlationId);
	NSLog(@"messageId is = %@",message.messageId);
	NSLog(@"payload is = %@",message.payLoad);
}
NSString *jsonResponse = [queue toString]; /* returns the response in JSON format. (as shown below)*/
                            
{
    "app42": {
        "response": {
            "success": true,
            "queues": {
                "queue": {
                    "queueName": "MyQueue",
                    "queueType": "PULL",
                    "messages": {
                        "message": {
                            "correlationId": "App42_d929f477-3517-4131-a46d-c2826b701f8d",
                            "payLoad": "Message Description",
                            "messageId": "ID:SHEPHERTZ-59712-1336725186689-0:30:24:1:1"
                        }
                    }
                }
            }
        }
    }
}
-(Queue*)receiveMessageByCorrelationId:(NSString*)queueName receiveTimeOut:(long)receiveTimeOut correlationId:(NSString*)correlationId

Pull message based on the correlation id.

Parameters:

queueName - The name of the queue from which the message has to be pulled
receiveTimeOut - Receive time out (in milli seconds)
correlationId - Correlation Id for which message has to be pulled

Returns:

Queue containing message which has pulled based on the correlation id

Response: Queue Object

Exception:

This method throws following exception.
/*
2400 - NOT FOUND - Queue with the name '<queueName>' not found.
2402 - NOT FOUND -  Queue with the name '<queueName>' and correlationId '<correlationId>' does not have any messages.
*/ 		          
For more details click Exception
NSString *queueName = @"MyQueue";
long receiveTimeOut = 10000;
NSString *correlationId = @"App42_d929f477-3517-4131-a46d-c2826b701f8d";                      
Queue *queue = [queueService receiveMessageByCorrelationId:queueName  receiveTimeOut:receiveTimeOut correlationId:correlationId];  /* returns the Queue object. */ 
NSLog(@"queueName is = %@",queue.queueName);
NSLog(@"queueType is= %@",queue.queueType);
NSMutableArray *messageList = [queue messageArray];	
for(Message *message in messageList)
{  
	NSLog(@"correlationId is  = %@",message.correlationId);
	NSLog(@"messageId is = %@",message.messageId);
	NSLog(@"payload is = %@",message.payLoad);
}
NSString *jsonResponse = [queue toString]; /* returns the response in JSON format. (as shown below)*/
                            
{
    "app42": {
        "response": {
            "success": true,
            "queues": {
                "queue": {
                    "queueName": "MyQueue",
                    "queueType": "PULL",
                    "messages": {
                        "message": {
                            "correlationId": "App42_d929f477-3517-4131-a46d-c2826b701f8d",
                            "payLoad": "Description of Messages",
                            "messageId": "ID:SHEPHERTZ-59712-1336725186689-0:30:24:1:1"
                        }
                    }
                }
            }
        }
    }
}
-(App42Response*)removeMessage:(NSString*)queueName messageId:(NSString*)messageId

Remove message from the queue based on the message id.
Note: Once the message is removed it cannot be pulled.

Parameters:

queueName - The name of the queue from which the message has to be removed
messageId - The message id of the message which has to be removed

Returns:

App42Response if removed successfully

Response: App42Response Object

Exception:

This method throws following exception.
/*
2400 - NOT FOUND - Queue with the name '<queueName>' not found.
*/ 		         
For more details click Exception
NSString *queueName = @"MyQueue";
String *messageId = @"ID:SHEPHERTZ-59712-1336725186689-0:30:24:1:1";
						        
App42Response *response = [queueServiceObject removeMessage:queueName messageId:messageId]; /* returns the App42Response object. */
NSString *success = [response isResponseSuccess];
NSString *jsonResponse = [response toString];  /* returns the response in JSON format. (as shown below)*/

                            
{
    "app42": {
        "response": {
            "success": true,
            "queues": {
                "queue": {
                    "messages": {
                        "message": {
                            "queueName": "MyQueue",
                            "messageId": "ID:SHEPHERTZ-59712-1336725186689-0:30:24:1:1"
                        }
                    }
                }
            }
        }
    }
}
-(Queue*)sendMessage:(NSString*)queueName message:(NSString*)msg expiryTime:(long)exp

Send message on the queue with an expiry. The message will expire if it is not pulled/dequeued before the expiry.

Parameters:

queueName - The name of the queue to which he message has to be sent
msg - Message that has to be sent
exp - Message expiry time

Returns:

Queue object containing message which has been sent with its message id and correlation id

Response: Queue Object

Exception:

This method throws following exception.
/*
2400 - NOT FOUND - Queue with the name '<queueName>' not found.
*/ 		  
For more details click Exception
NSString *queueName = @"MyQueue";
NSString *msg = @"Its a Real Message";
long exp = 100000;    
Queue *queue = [queueService sendMessage:queueName message:msg expiryTime:exp];  /* returns the Queue object. */ 
NSLog(@"queueName is = %@",queue.queueName);
NSLog(@"queueType is= %@",queue.queueType);
NSMutableArray *messageList = [queue messageArray];	
for(Message *message in messageList)
{  
	NSLog(@"correlationId is  = %@",message.correlationId);
	NSLog(@"messageId is = %@",message.messageId);
	NSLog(@"payload is = %@",message.payLoad);
}
NSString *jsonResponse = [queue toString]; /* returns the response in JSON format. (as shown below)*/

                            
{
    "app42": {
        "response": {
            "success": true,
            "queues": {
                "queue": {
                    "queueName": "MyQueue",
                    "queueType": "PULL",
                    "messages": {
                        "message": {
                            "correlationId": "App42_d929f477-3517-4131-a46d-c2826b701f8d",
                            "payLoad": "Its a Real Message"
                        }
                    }
                }
            }
        }
    }
}
-(Queue*)createPullQueue:(NSString*)queueName description:(NSString*)queueDescription

Creates a type Pull Queue.

Parameters:

queueName - The name of the queue which has to be created
queueDescription - The description of the queue

Returns:

Queue object containing queue name which has been created

Response: Queue Object

Exception:

This method throws following exception.
/*
2403 - BAD REQUEST - Queue by the name '<queueName>' already exists.
*/ 		        
For more details click Exception
NSString *queueName =  @"MyQueue";
NSString *queueDescription = @"Hi its testing Time";   				
Queue *queue = [queueService createPullQueue:queueName description:description];  /* returns the Queue object. */ 
NSLog(@"queueName is = %@",queue.queueName);
NSLog(@"queueType is= %@",queue.queueType);
NSLog(@"queueDescription is = %@",queue.description);
NSString *jsonResponse = [queue toString]; /* returns the response in JSON format. (as shown below)*/
                            
{
    "app42": {
        "response": {
            "success": true,
            "queues": {
                "queue": {
                    "queueName": "MyQueue",
                    "queueType": "pull",
                    "description": "Hi its testing Time"
                }
            }
        }
    }
}
-(App42Response*)deletePullQueue:(NSString*)queueName

Deletes the Pull type Queue.

Parameters:

queueName - The name of the queue which has to be deleted

Returns:

App42Response if deleted successfully

Response: App42Response Object

Exception:

This method throws following exception.
/*
2400 - NOT FOUND - Queue with the name '<queueName>' not found.
*/ 		 
For more details click Exception
NSString *queueName = @"MyQueue"; 
App42Response *response = [queueServiceObject deletePullQueue:queueName]; /* 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,
            "queues": {
                "queue": {
                    "queueName": "MyQueue",
                    "queueType": "pull",
                    "description": "Queue Description"
                }
            }
        }
    }
}
-(Queue*)getMessages:(NSString*)queueName receiveTimeOut:(long)receiveTimeOut

Messages are retrieved and dequeued from the Queue.

Parameters:

queueName - The name of the queue which have to be retrieved
receiveTimeOut - Receive time out (in milli seconds)

Returns:

Queue object containing messages in the Queue

Response: Queue Object

Exception:

This method throws following exception.
/*
2400 - NOT FOUND - Queue with the name '<queueName>' not found.
*/ 		        
For more details click Exception
NSString *queueName = @"MyQueue";
long receiveTimeOut = 10000;				     
Queue *queue = [queueService getMessages:queueName  receiveTimeOut:receiveTimeOut];  /* returns the Queue object. */ 
NSLog(@"queueName is = %@",queue.queueName);
NSLog(@"queueType is= %@",queue.queueType);
NSMutableArray *messageList = [queue messageArray];	
for(Message *message in messageList)
{  
	NSLog(@"correlationId is  = %@",message.correlationId);
	NSLog(@"messageId is = %@",message.messageId);
	NSLog(@"payload is = %@",message.payLoad);
}
NSString *jsonResponse = [queue toString];  /* returns the response in JSON format. (as shown below)*/
                            
{
    "app42": {
        "response": {
            "success": true,
            "queues": {
                "queue": {
                    "queueName": "Queue_Name_101",
                    "queueType": "PULL",
                    "messages": {
                        "message": {
                            "correlationId": "App42_d929f477-3517-4131-a46d-c2826b701f8d",
                            "payLoad": "Hi its Queue Description",
                            "messageId": "ID:SHEPHERTZ-59712-1336725186689-0:30:24:1:1"
                        }
                    }
                }
            }
        }
    }
}
-(Queue*)pendingMessages:(NSString*)queueName

Messages which are pending to be dequeue.
Note: Calling this method does not dequeue the messages in the Queue. The messages stay in the Queue till they are dequeued

Parameters:

queueName - The name of the queue from which pending messages have to be fetched

Returns:

Queue object containing pending messages in the Queue

Response: Queue Object

Exception:

This method throws following exception.
/*
2400 - NOT FOUND - Queue with the name '<queueName>' not found.
2405 - NOT FOUND - Queue with the name '<queueName>' does not have any pending messages.
*/ 		  
For more details click Exception
NSString *queueName = @"MyQueue";   
NSLog(@"queueName is = %@",queue.queueName);
NSLog(@"queueType is= %@",queue.queueType);
NSMutableArray *messageList = [queue messageArray];	
for(Message *message in messageList)
{  
	NSLog(@"correlationId is  = %@",message.correlationId);
	NSLog(@"messageId is = %@",message.messageId);
	NSLog(@"payload is = %@",message.payLoad);
}
NSString *jsonResponse = [queue toString];  /* returns the response in JSON format. (as shown below)*/
                            
{
    "app42": {
        "response": {
            "success": true,
            "queues": {
                "queue": {
                    "queueName": "MyQueue",
                    "queueType": "PULL",
                    "messages": {
                        "message": {
                            "correlationId": "App42_d929f477-3517-4131-a46d-c2826b701f8d",
                            "payLoad": "DESCRIPTION",
                            "messageId": "ID:SHEPHERTZ-59712-1336725186689-0:30:24:1:1"
                        }
                    }
                }
            }
        }
    }
}
-(App42Response*)purgePullQueue:(NSString*)queueName

Purges message on the Queue.
Note: once the Queue is purged the messages are removed from the Queue and wont be available for dequeuing.

Parameters:

queueName - The name of the queue which has to be purged

Returns:

App42Response object

Response: App42Response Object

For more details click Exception
NSString *queueName = @"MyQueue";
App42Response *response = [queueService purgePullQueue:queueName];  /* returns the Queue object. */ 
BOOL success = response.isResponseSuccess;
NSString *jsonResponse = [response.toString(); /* returns the response in JSON format. (as shown below)*/ 
                            
{
    "app42": {
        "response": {
            "success": true,
            "queues": {
                "queue": {
                    "queueName": "MyQueue",
                    "queueType": null,
                    "description": "Queue Description"
                }
            }
        }
    }
}