Commit cf1c4b2c by Dima Bart

Organize BUYClient+Customer.m (reorder methods, add pragma marks).

parent 3bc35a19
...@@ -67,6 +67,8 @@ typedef void (^BUYDataOrdersBlock)(NSArray <BUYOrder*> * _Nullable orders, NSErr ...@@ -67,6 +67,8 @@ typedef void (^BUYDataOrdersBlock)(NSArray <BUYOrder*> * _Nullable orders, NSErr
@interface BUYClient (Customers) @interface BUYClient (Customers)
#pragma mark - Getting -
/** /**
* GET /api/customers/:customer_id * GET /api/customers/:customer_id
* Gets an existing customer * Gets an existing customer
...@@ -78,6 +80,8 @@ typedef void (^BUYDataOrdersBlock)(NSArray <BUYOrder*> * _Nullable orders, NSErr ...@@ -78,6 +80,8 @@ typedef void (^BUYDataOrdersBlock)(NSArray <BUYOrder*> * _Nullable orders, NSErr
*/ */
- (BUYRequestOperation *)getCustomerWithID:(NSString *)customerID callback:(BUYDataCustomerBlock)block; - (BUYRequestOperation *)getCustomerWithID:(NSString *)customerID callback:(BUYDataCustomerBlock)block;
#pragma mark - Customer -
/** /**
* POST /api/customers * POST /api/customers
* Creates a new customer * Creates a new customer
...@@ -93,39 +97,44 @@ typedef void (^BUYDataOrdersBlock)(NSArray <BUYOrder*> * _Nullable orders, NSErr ...@@ -93,39 +97,44 @@ typedef void (^BUYDataOrdersBlock)(NSArray <BUYOrder*> * _Nullable orders, NSErr
- (BUYRequestOperation *)createCustomerWithCredentials:(BUYAccountCredentials *)credentials callback:(BUYDataCustomerTokenBlock)block; - (BUYRequestOperation *)createCustomerWithCredentials:(BUYAccountCredentials *)credentials callback:(BUYDataCustomerTokenBlock)block;
/** /**
* POST /api/customers/customer_token * PUT /api/customers/:customer_id/activate
* Logs in an existing customer * Activates an unactivated customer
* Expects email and password
* *
* @param credentials Credentials object containing items for required fields * @param credentials Credentials containing a password and password confirmation
* @param block (BUYCustomer *customer, NSString *token, NSError *error) * @param customerID ID of customer being activated
* @param token Token contained in activation URL
* @param block (BUYCustomer *customer, NSString *token, NSError *error)
* *
* @return The associated BUYRequestOperation * @return The associated BUYRequestOperation
*/ */
- (BUYRequestOperation *)loginCustomerWithCredentials:(BUYAccountCredentials *)credentials callback:(BUYDataCustomerTokenBlock)block; - (BUYRequestOperation *)activateCustomerWithCredentials:(BUYAccountCredentials *)credentials customerID:(NSString *)customerID token:(NSString *)token callback:(BUYDataCustomerTokenBlock)block;
/** /**
* DELETE /api/customers/:customer_id/customer_token * PUT /api/customers/:customer_id
* Logs out an existing customer * Update customer credentials represented by BUYAccountCredentials object
* Expects a customerID string
* *
* @param customer A customerID represented by a string * @param credentials Credentials containing a password and password confirmation
* @param block (BUYStatus status, NSError *error) * @param customerID ID of customer being activated
* @param block (BUYCustomer *customer, NSError *error)
* *
* @return The associated BUYRequestOperation * @return The associated BUYRequestOperation
*/ */
- (BUYRequestOperation *)logoutCustomerID:(NSString *)customerID callback:(BUYDataStatusBlock)block; - (BUYRequestOperation *)updateCustomerWithCredentials:(BUYAccountCredentials *)credentials customerID:(NSString *)customerID callback:(BUYDataCustomerBlock)block;
/** /**
* POST /api/customers/recover * PUT /api/customers/:customer_id/reset
* Sends email for password recovery to an existing customer * Resets an existing customer's password
* *
* @param email Email to send the password reset to * @param credentials Credentials containing a password and password confirmation
* @param block (BUYStatus status, NSError *error) * @param customerID ID of customer resetting password
* @param token Token contained in reset URL
* @param block (BUYCustomer *customer, NSString *token, NSError *error)
* *
* @return the associated BUYRequestOperation * @return The associated BUYRequestOperation
*/ */
- (BUYRequestOperation *)recoverPasswordForCustomer:(NSString *)email callback:(BUYDataStatusBlock)block; - (BUYRequestOperation *)resetPasswordWithCredentials:(BUYAccountCredentials *)credentials customerID:(NSString *)customerID token:(NSString *)token callback:(BUYDataCustomerTokenBlock)block;
#pragma mark - Token -
/** /**
* PUT /api/customers/:customer_id/customer_token/renew * PUT /api/customers/:customer_id/customer_token/renew
...@@ -138,43 +147,44 @@ typedef void (^BUYDataOrdersBlock)(NSArray <BUYOrder*> * _Nullable orders, NSErr ...@@ -138,43 +147,44 @@ typedef void (^BUYDataOrdersBlock)(NSArray <BUYOrder*> * _Nullable orders, NSErr
*/ */
- (BUYRequestOperation *)renewCustomerTokenWithID:(NSString *)customerID callback:(BUYDataTokenBlock)block; - (BUYRequestOperation *)renewCustomerTokenWithID:(NSString *)customerID callback:(BUYDataTokenBlock)block;
#pragma mark - Login -
/** /**
* PUT /api/customers/:customer_id/activate * POST /api/customers/customer_token
* Activates an unactivated customer * Logs in an existing customer
* Expects email and password
* *
* @param credentials Credentials containing a password and password confirmation * @param credentials Credentials object containing items for required fields
* @param customerID ID of customer being activated * @param block (BUYCustomer *customer, NSString *token, NSError *error)
* @param token Token contained in activation URL
* @param block (BUYCustomer *customer, NSString *token, NSError *error)
* *
* @return The associated BUYRequestOperation * @return The associated BUYRequestOperation
*/ */
- (BUYRequestOperation *)activateCustomerWithCredentials:(BUYAccountCredentials *)credentials customerID:(NSString *)customerID token:(NSString *)token callback:(BUYDataCustomerTokenBlock)block; - (BUYRequestOperation *)loginCustomerWithCredentials:(BUYAccountCredentials *)credentials callback:(BUYDataCustomerTokenBlock)block;
/** /**
* PUT /api/customers/:customer_id * DELETE /api/customers/:customer_id/customer_token
* Update customer credentials represented by BUYAccountCredentials object * Logs out an existing customer
* Expects a customerID string
* *
* @param credentials Credentials containing a password and password confirmation * @param customer A customerID represented by a string
* @param customerID ID of customer being activated * @param block (BUYStatus status, NSError *error)
* @param block (BUYCustomer *customer, NSError *error)
* *
* @return The associated BUYRequestOperation * @return The associated BUYRequestOperation
*/ */
- (BUYRequestOperation *)updateCustomerWithCredentials:(BUYAccountCredentials *)credentials customerID:(NSString *)customerID callback:(BUYDataCustomerBlock)block; - (BUYRequestOperation *)logoutCustomerID:(NSString *)customerID callback:(BUYDataStatusBlock)block;
/** /**
* PUT /api/customers/:customer_id/reset * POST /api/customers/recover
* Resets an existing customer's password * Sends email for password recovery to an existing customer
* *
* @param credentials Credentials containing a password and password confirmation * @param email Email to send the password reset to
* @param customerID ID of customer resetting password * @param block (BUYStatus status, NSError *error)
* @param token Token contained in reset URL
* @param block (BUYCustomer *customer, NSString *token, NSError *error)
* *
* @return The associated BUYRequestOperation * @return the associated BUYRequestOperation
*/ */
- (BUYRequestOperation *)resetPasswordWithCredentials:(BUYAccountCredentials *)credentials customerID:(NSString *)customerID token:(NSString *)token callback:(BUYDataCustomerTokenBlock)block; - (BUYRequestOperation *)recoverPasswordForCustomer:(NSString *)email callback:(BUYDataStatusBlock)block;
#pragma mark - Orders -
/** /**
* GET /api/customers/:customer_id/orders * GET /api/customers/:customer_id/orders
......
...@@ -36,7 +36,7 @@ ...@@ -36,7 +36,7 @@
@implementation BUYClient (Customers) @implementation BUYClient (Customers)
#pragma mark - Customer #pragma mark - Getting -
- (BUYRequestOperation *)getCustomerWithID:(NSString *)customerID callback:(BUYDataCustomerBlock)block - (BUYRequestOperation *)getCustomerWithID:(NSString *)customerID callback:(BUYDataCustomerBlock)block
{ {
...@@ -50,6 +50,8 @@ ...@@ -50,6 +50,8 @@
}]; }];
} }
#pragma mark - Customer -
- (BUYRequestOperation *)createCustomerWithCredentials:(BUYAccountCredentials *)credentials callback:(BUYDataCustomerTokenBlock)block - (BUYRequestOperation *)createCustomerWithCredentials:(BUYAccountCredentials *)credentials callback:(BUYDataCustomerTokenBlock)block
{ {
NSURL *route = [self urlForCustomers]; NSURL *route = [self urlForCustomers];
...@@ -63,23 +65,15 @@ ...@@ -63,23 +65,15 @@
}]; }];
} }
- (BUYRequestOperation *)createTokenForCustomerWithCredentials:(BUYAccountCredentials *)credentials customerJSON:(NSDictionary *)customerJSON callback:(BUYDataCustomerTokenBlock)block - (BUYRequestOperation *)activateCustomerWithCredentials:(BUYAccountCredentials *)credentials customerID:(NSString *)customerID token:(NSString *)token callback:(BUYDataCustomerTokenBlock)block
{ {
NSURL *route = [self urlForCustomersToken]; NSURL *route = [self urlForCustomersActivationWithID:customerID parameters:@{ @"token": token }];
return [self postRequestForURL:route object:credentials.JSONRepresentation completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
if (json && !error) { return [self putRequestForURL:route object:credentials.JSONRepresentation completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
BUYAuthenticatedResponse *authenticatedResponse = [BUYAuthenticatedResponse responseWithJSON:json]; NSString *email = json[@"customer"][@"email"];
self.customerToken = authenticatedResponse.accessToken; if (email && !error) {
BUYAccountCredentialItem *emailItem = [BUYAccountCredentialItem itemWithEmail:email];
if (!customerJSON) { [self loginCustomerWithCredentials:[credentials credentialsByAddingItems:@[emailItem]] callback:block];
[self getCustomerWithID:authenticatedResponse.customerID callback:^(BUYCustomer *customer, NSError *error) {
block(customer, self.customerToken, error);
}];
}
else {
BUYCustomer *customer = [self.modelManager customerWithJSONDictionary:json[@"customer"]];
block(customer, self.customerToken, error);
}
} }
else { else {
block(nil, nil, error); block(nil, nil, error);
...@@ -87,27 +81,36 @@ ...@@ -87,27 +81,36 @@
}]; }];
} }
- (BUYRequestOperation *)logoutCustomerID:(NSString *)customerID callback:(BUYDataStatusBlock)block - (BUYRequestOperation *)updateCustomerWithCredentials:(BUYAccountCredentials *)credentials customerID:(NSString *)customerID callback:(BUYDataCustomerBlock)block
{ {
NSURL *route = [self urlForCustomersTokenWithID:customerID]; NSURL *route = [self urlForCustomersWithID:customerID];
return [self deleteRequestForURL:route completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) { return [self putRequestForURL:route object:credentials.JSONRepresentation completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
block(response.statusCode, error); BUYCustomer *customer = nil;
if (json && !error) {
customer = [self.modelManager customerWithJSONDictionary:json];
}
block(customer, error);
}]; }];
} }
- (BUYRequestOperation *)loginCustomerWithCredentials:(BUYAccountCredentials *)credentials callback:(BUYDataCustomerTokenBlock)block - (BUYRequestOperation *)resetPasswordWithCredentials:(BUYAccountCredentials *)credentials customerID:(NSString *)customerID token:(NSString *)token callback:(BUYDataCustomerTokenBlock)block
{
return [self createTokenForCustomerWithCredentials:credentials customerJSON:nil callback:block];
}
- (BUYRequestOperation *)recoverPasswordForCustomer:(NSString *)email callback:(BUYDataStatusBlock)block
{ {
NSURL *route = [self urlForCustomersPasswordRecovery]; NSURL *route = [self urlForCustomersPasswordResetWithID:customerID parameters:@{ @"token": token }];
return [self postRequestForURL:route object:@{@"email": email} completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
block(response.statusCode, error); return [self putRequestForURL:route object:credentials.JSONRepresentation completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
NSString *email = json[@"customer"][@"email"];
if (email && !error) {
BUYAccountCredentialItem *emailItem = [BUYAccountCredentialItem itemWithEmail:email];
[self loginCustomerWithCredentials:[credentials credentialsByAddingItems:@[emailItem]] callback:block];
}
else {
block(nil, nil, error);
}
}]; }];
} }
#pragma mark - Token -
- (BUYRequestOperation *)renewCustomerTokenWithID:(NSString *)customerID callback:(BUYDataTokenBlock)block - (BUYRequestOperation *)renewCustomerTokenWithID:(NSString *)customerID callback:(BUYDataTokenBlock)block
{ {
if (self.customerToken) { if (self.customerToken) {
...@@ -129,50 +132,31 @@ ...@@ -129,50 +132,31 @@
} }
} }
- (BUYRequestOperation *)activateCustomerWithCredentials:(BUYAccountCredentials *)credentials customerID:(NSString *)customerID token:(NSString *)token callback:(BUYDataCustomerTokenBlock)block #pragma mark - Login -
- (BUYRequestOperation *)logoutCustomerID:(NSString *)customerID callback:(BUYDataStatusBlock)block
{ {
NSURL *route = [self urlForCustomersActivationWithID:customerID parameters:@{ @"token": token }]; NSURL *route = [self urlForCustomersTokenWithID:customerID];
return [self deleteRequestForURL:route completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
return [self putRequestForURL:route object:credentials.JSONRepresentation completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) { block(response.statusCode, error);
NSString *email = json[@"customer"][@"email"];
if (email && !error) {
BUYAccountCredentialItem *emailItem = [BUYAccountCredentialItem itemWithEmail:email];
[self loginCustomerWithCredentials:[credentials credentialsByAddingItems:@[emailItem]] callback:block];
}
else {
block(nil, nil, error);
}
}]; }];
} }
- (BUYRequestOperation *)updateCustomerWithCredentials:(BUYAccountCredentials *)credentials customerID:(NSString *)customerID callback:(BUYDataCustomerBlock)block - (BUYRequestOperation *)loginCustomerWithCredentials:(BUYAccountCredentials *)credentials callback:(BUYDataCustomerTokenBlock)block
{ {
NSURL *route = [self urlForCustomersWithID:customerID]; return [self createTokenForCustomerWithCredentials:credentials customerJSON:nil callback:block];
return [self putRequestForURL:route object:credentials.JSONRepresentation completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
BUYCustomer *customer = nil;
if (json && !error) {
customer = [self.modelManager customerWithJSONDictionary:json];
}
block(customer, error);
}];
} }
- (BUYRequestOperation *)resetPasswordWithCredentials:(BUYAccountCredentials *)credentials customerID:(NSString *)customerID token:(NSString *)token callback:(BUYDataCustomerTokenBlock)block - (BUYRequestOperation *)recoverPasswordForCustomer:(NSString *)email callback:(BUYDataStatusBlock)block
{ {
NSURL *route = [self urlForCustomersPasswordResetWithID:customerID parameters:@{ @"token": token }]; NSURL *route = [self urlForCustomersPasswordRecovery];
return [self postRequestForURL:route object:@{@"email": email} completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
return [self putRequestForURL:route object:credentials.JSONRepresentation completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) { block(response.statusCode, error);
NSString *email = json[@"customer"][@"email"];
if (email && !error) {
BUYAccountCredentialItem *emailItem = [BUYAccountCredentialItem itemWithEmail:email];
[self loginCustomerWithCredentials:[credentials credentialsByAddingItems:@[emailItem]] callback:block];
}
else {
block(nil, nil, error);
}
}]; }];
} }
#pragma mark - Orders -
- (BUYRequestOperation *)getOrdersForCustomerWithCallback:(BUYDataOrdersBlock)block - (BUYRequestOperation *)getOrdersForCustomerWithCallback:(BUYDataOrdersBlock)block
{ {
NSURL *route = [self urlForCustomersOrders]; NSURL *route = [self urlForCustomersOrders];
...@@ -186,4 +170,30 @@ ...@@ -186,4 +170,30 @@
}]; }];
} }
#pragma mark - Helpers -
- (BUYRequestOperation *)createTokenForCustomerWithCredentials:(BUYAccountCredentials *)credentials customerJSON:(NSDictionary *)customerJSON callback:(BUYDataCustomerTokenBlock)block
{
NSURL *route = [self urlForCustomersToken];
return [self postRequestForURL:route object:credentials.JSONRepresentation completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
if (json && !error) {
BUYAuthenticatedResponse *authenticatedResponse = [BUYAuthenticatedResponse responseWithJSON:json];
self.customerToken = authenticatedResponse.accessToken;
if (!customerJSON) {
[self getCustomerWithID:authenticatedResponse.customerID callback:^(BUYCustomer *customer, NSError *error) {
block(customer, self.customerToken, error);
}];
}
else {
BUYCustomer *customer = [self.modelManager customerWithJSONDictionary:json[@"customer"]];
block(customer, self.customerToken, error);
}
}
else {
block(nil, nil, error);
}
}];
}
@end @end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment