fernyb / Sequel-J

A MySQL Client written in Cappuccino (work in progress)
31 stars 1 forks source link

SJAPIRequest class proposal #12

Closed joehoyle closed 13 years ago

joehoyle commented 13 years ago

I think we should write a apiRequest class to pass all requests to the server through, this would give us some abstraction from the endpoints and the ugly CPURLConnection API.

On another project I work on, I use a closure functions to handle the callbacks, as I find having to send a request in one method, then pick it up in another method to continue the process is a bit clunky. This way we would be able to do the following:

var options = [[CPDictionary alloc] initWithValues:["root", "root"] forKeys:["username", "password"]];

[[SJAPIRequest sendRequestToConnectWithOptions: options callback: function( jsonData ) {
    //continue doing whatever, you have access to local variables etc etc
}];

Other methods would be sendRequestToQueryWithOptions:, sendRequestToDatabasesWithOptions: etc (for all endpoints).

This would also reduce the code needed, the same with the current way of doing things:

  var request = [SJHTTPRequest requestWithURL:SERVER_BASE + "/connect"];
  [request setObject:username forKey:@"username"];
  [request setObject:password forKey:@"password"];

  [[SJDataManager sharedInstance] setCredentials:[request params]];

  httpConnection = [CPURLConnection connectionWithRequest:[request toRequest] delegate:self];

- (void)connectionDidFinishLoading:(CPURLConnection)connection
{
  var json = JSON.parse([responseData componentsJoinedByString:@""]);
  response = nil;
  [responseData removeAllObjects];

  //do stuff
}

This would also make it easier to support different server-sides in the future as we would not be tied to the end url scheme.

What do you think about this?

fernyb commented 13 years ago

Sounds like a great idea. Go for it!

joehoyle commented 13 years ago

Closing as completed