I'm building an iOS app that uses web services extensively. I've built classes that handle the requests. However since there are a lot of web service request. I need to find out how to detect which web service to run, based on the action - registration, login, image upload, etc.
This is just a small snippet of a very large method I came up with:
- (BOOL) wsExecution:(int) wsID wsParameters: (NSDictionary*) parameters
{
switch (wsID)
{
case WEB_SERVICE_ID
{
ClassWS* ws = [[ClassWS alloc] initWithURL:WEB_SERVICE_ID Url:WEB_SERVICE_URL];
break;
}
case AAWEB_SERVICE_ID
{
ClassWS* ws = [[ClassWS alloc] initWithURL:AAWEB_SERVICE_ID Url:AAWEB_SERVICE_URL];
break;
}
The web service IDs are defined in a file called constants. I then map a URL to an ID in the method above. There are another 30 switch cases in this method. The Web Service IDs are just ints that have no relevance to the actual web service. It was just a way to check which service, based on a switch statement.
It makes it hard to read and I think it can be done better. Is there a way to do with and remove the notion of a web service ID completely?
To be a little more clear on where I am going with this:
Here is another example of the switch case (I have counted 200 cases):
switch (wsID)
{
case RUN_ORDER_WS_ID:
{
NSDictionary* postData = [NSDictionary dictionaryWithObject:[parameters objectForKey:@"OrderResponse"] forKey:@"OrderResponse"];
NSMutableDictionary* newParams = [NSMutableDictionary dictionaryWithDictionary:parameters];
OrderWS* ws = [[OrderWS alloc] initWithURL:RUN_ORDER_WS_ID Url:RUN_ORDER_WS_ID_WS_URL];
self.placeClassifiedResponseWS = ws;
ws.delegate = self;
[newParams removeObjectForKey:@"OrderResponse"];
[ws fetch:newParams PostDataValuesAndKeys:postData];
[ws release];
break;
}
case GET_LIST_OF_USERS_ID:
{
NSDictionary* postData = [NSDictionary dictionaryWithObject:[parameters objectForKey:@"userList"] forKey:@"userList"];
NSMutableDictionary* newParams = [NSMutableDictionary dictionaryWithDictionary:parameters];
UsersWS* ws = [[UsersWS alloc] initWithURL:GET_LIST_OF_USERS_ID Url:GET_LIST_OF_USERS_URL];
self.userListWS = ws;
ws.delegate = self;
[newParams removeObjectForKey:@"userList"];
[ws fetch:newParams PostDataValuesAndKeys:postData];
[ws release];
break;
}
case GET_USER_REVIEW_FROM_ITEMS_WS_ID:
{
NSString* itemId = [parameters objectForKey:@"itemId"];
NSMutableDictionary* newParams = [NSMutableDictionary dictionaryWithDictionary:parameters];
NSString* newURL = [NSString stringWithFormat:GET_USER_REVIEW_FROM_ITEMS_WS_URL, itemId];
DataStoreWS = [[DataStoreWS alloc] initForComponents: [NSArray arrayWithObjects:REVIEW_COMPONENT, nil] WebserviceID: wsID Url:newURL];
self.reviewOfUserWS = ws;
ws.delegate = self;
[newParams removeObjectForKey:@"itemId"];
[ws fetch:newParams];
[ws release];
break;
}
initWithURL:(not a URL!) Url:(wait, here's the URL?)\$\endgroup\$@[...]for NSArray or@"..."for NSString. \$\endgroup\$