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
@interface BUYClient (Customers)
#pragma mark - Getting -
/**
* GET /api/customers/:customer_id
* Gets an existing customer
......@@ -78,6 +80,8 @@ typedef void (^BUYDataOrdersBlock)(NSArray <BUYOrder*> * _Nullable orders, NSErr
*/
- (BUYRequestOperation *)getCustomerWithID:(NSString *)customerID callback:(BUYDataCustomerBlock)block;
#pragma mark - Customer -
/**
* POST /api/customers
* Creates a new customer
......@@ -93,39 +97,44 @@ typedef void (^BUYDataOrdersBlock)(NSArray <BUYOrder*> * _Nullable orders, NSErr
- (BUYRequestOperation *)createCustomerWithCredentials:(BUYAccountCredentials *)credentials callback:(BUYDataCustomerTokenBlock)block;
/**
* POST /api/customers/customer_token
* Logs in an existing customer
* Expects email and password
* PUT /api/customers/:customer_id/activate
* Activates an unactivated customer
*
* @param credentials Credentials object containing items for required fields
* @param block (BUYCustomer *customer, NSString *token, NSError *error)
* @param credentials Credentials containing a password and password confirmation
* @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
*/
- (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
* Logs out an existing customer
* Expects a customerID string
* PUT /api/customers/:customer_id
* Update customer credentials represented by BUYAccountCredentials object
*
* @param customer A customerID represented by a string
* @param block (BUYStatus status, NSError *error)
* @param credentials Credentials containing a password and password confirmation
* @param customerID ID of customer being activated
* @param block (BUYCustomer *customer, NSError *error)
*
* @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
* Sends email for password recovery to an existing customer
* PUT /api/customers/:customer_id/reset
* Resets an existing customer's password
*
* @param email Email to send the password reset to
* @param block (BUYStatus status, NSError *error)
* @param credentials Credentials containing a password and password confirmation
* @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
......@@ -138,43 +147,44 @@ typedef void (^BUYDataOrdersBlock)(NSArray <BUYOrder*> * _Nullable orders, NSErr
*/
- (BUYRequestOperation *)renewCustomerTokenWithID:(NSString *)customerID callback:(BUYDataTokenBlock)block;
#pragma mark - Login -
/**
* PUT /api/customers/:customer_id/activate
* Activates an unactivated customer
* POST /api/customers/customer_token
* Logs in an existing customer
* Expects email and password
*
* @param credentials Credentials containing a password and password confirmation
* @param customerID ID of customer being activated
* @param token Token contained in activation URL
* @param block (BUYCustomer *customer, NSString *token, NSError *error)
* @param credentials Credentials object containing items for required fields
* @param block (BUYCustomer *customer, NSString *token, NSError *error)
*
* @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
* Update customer credentials represented by BUYAccountCredentials object
* DELETE /api/customers/:customer_id/customer_token
* Logs out an existing customer
* Expects a customerID string
*
* @param credentials Credentials containing a password and password confirmation
* @param customerID ID of customer being activated
* @param block (BUYCustomer *customer, NSError *error)
* @param customer A customerID represented by a string
* @param block (BUYStatus status, NSError *error)
*
* @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
* Resets an existing customer's password
* POST /api/customers/recover
* Sends email for password recovery to an existing customer
*
* @param credentials Credentials containing a password and password confirmation
* @param customerID ID of customer resetting password
* @param token Token contained in reset URL
* @param block (BUYCustomer *customer, NSString *token, NSError *error)
* @param email Email to send the password reset to
* @param block (BUYStatus status, 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
......
......@@ -36,7 +36,7 @@
@implementation BUYClient (Customers)
#pragma mark - Customer
#pragma mark - Getting -
- (BUYRequestOperation *)getCustomerWithID:(NSString *)customerID callback:(BUYDataCustomerBlock)block
{
......@@ -50,6 +50,8 @@
}];
}
#pragma mark - Customer -
- (BUYRequestOperation *)createCustomerWithCredentials:(BUYAccountCredentials *)credentials callback:(BUYDataCustomerTokenBlock)block
{
NSURL *route = [self urlForCustomers];
......@@ -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];
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);
}
NSURL *route = [self urlForCustomersActivationWithID:customerID parameters:@{ @"token": token }];
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);
......@@ -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];
return [self deleteRequestForURL:route completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
block(response.statusCode, error);
NSURL *route = [self urlForCustomersWithID:customerID];
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 *)loginCustomerWithCredentials:(BUYAccountCredentials *)credentials callback:(BUYDataCustomerTokenBlock)block
{
return [self createTokenForCustomerWithCredentials:credentials customerJSON:nil callback:block];
}
- (BUYRequestOperation *)recoverPasswordForCustomer:(NSString *)email callback:(BUYDataStatusBlock)block
- (BUYRequestOperation *)resetPasswordWithCredentials:(BUYAccountCredentials *)credentials customerID:(NSString *)customerID token:(NSString *)token callback:(BUYDataCustomerTokenBlock)block
{
NSURL *route = [self urlForCustomersPasswordRecovery];
return [self postRequestForURL:route object:@{@"email": email} completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
block(response.statusCode, error);
NSURL *route = [self urlForCustomersPasswordResetWithID:customerID parameters:@{ @"token": token }];
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
{
if (self.customerToken) {
......@@ -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 }];
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);
}
NSURL *route = [self urlForCustomersTokenWithID:customerID];
return [self deleteRequestForURL:route completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
block(response.statusCode, 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 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);
}];
return [self createTokenForCustomerWithCredentials:credentials customerJSON:nil callback:block];
}
- (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 }];
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);
}
NSURL *route = [self urlForCustomersPasswordRecovery];
return [self postRequestForURL:route object:@{@"email": email} completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
block(response.statusCode, error);
}];
}
#pragma mark - Orders -
- (BUYRequestOperation *)getOrdersForCustomerWithCallback:(BUYDataOrdersBlock)block
{
NSURL *route = [self urlForCustomersOrders];
......@@ -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
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