Skip to content

Commit

Permalink
Merge pull request #64 from nemesis/v3
Browse files Browse the repository at this point in the history
Update SDK to support Spectre API V3
  • Loading branch information
deltadd committed Mar 31, 2016
2 parents 038bb4e + 57f34db commit dafd331
Show file tree
Hide file tree
Showing 89 changed files with 893 additions and 277 deletions.
43 changes: 37 additions & 6 deletions Classes/API/SEAPIRequestManager.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// SEAPIRequestManager.h
//
// Copyright (c) 2015 Salt Edge. https://saltedge.com
// Copyright (c) 2016 Salt Edge. https://saltedge.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand All @@ -23,7 +23,7 @@

#import <Foundation/Foundation.h>

@class SEProvider, SELogin, SEError;
@class SEProvider, SELogin, SEError, SELoginAttempt;

@protocol SELoginFetchingDelegate;

Expand All @@ -47,6 +47,15 @@ typedef void (^SEAPIRequestFailureBlock)(SEError* error);
*/
+ (void)linkClientId:(NSString*)clientId appSecret:(NSString*)appSecret;


/**
Links your Customer Secret to the request manager. All outgoing requests related to logins will have the proper customer-related HTTP headers set by default.
@param customerSecret The string identifying the customer.
@see https://docs.saltedge.com/reference/#customers-create
*/
+ (void)linkCustomerSecret:(NSString*)customerSecret;

/**
Creates a new customer or returns an error if such customer exists.
Expand All @@ -55,6 +64,7 @@ typedef void (^SEAPIRequestFailureBlock)(SEError* error);
@param failure The callback block if the request fails.
@warning identifier cannot be nil.
@see https://docs.saltedge.com/reference/#customers-create
*/
- (void)createCustomerWithIdentifier:(NSString*)identifier
success:(void (^)(NSDictionary* responseObject))success
Expand All @@ -73,7 +83,6 @@ typedef void (^SEAPIRequestFailureBlock)(SEError* error);
@code
// parameters example, listing only the required fields
{
"customer_id": "AtQX6Q8vRyMrPjUVtW7J_O1n06qYQ25bvUJ8CIC80-8",
"country_code": "XF",
"provider_code": "fakebank_simple_xf",
"credentials": {
Expand Down Expand Up @@ -103,7 +112,6 @@ typedef void (^SEAPIRequestFailureBlock)(SEError* error);
@code
// parameters example, listing only the required fields
{
"customer_id": "AtQX6Q8vRyMrPjUVtW7J_O1n06qYQ25bvUJ8CIC80-8",
"country_code": "XO",
"provider_code": "paypal_xo",
"return_to": "your-app-url://home.local"
Expand Down Expand Up @@ -282,6 +290,30 @@ typedef void (^SEAPIRequestFailureBlock)(SEError* error);
success:(void (^)(SELogin* login))success
failure:(SEAPIRequestFailureBlock)failure;

/**
Fetches a certain attempt for a login.
@param attemptId The id of the attempt that is going to be fetched.
@param loginSecret The secret of the login whose attempt is going to be fetched.
@param success The callback block if the request succeeds.
@param failure The callback block if the request fails.
*/
- (void)fetchAttemptWithId:(NSNumber*)attemptId
forLoginWithSecret:(NSString*)loginSecret
success:(void (^)(SELoginAttempt*))success
failure:(SEAPIRequestFailureBlock)failure;

/**
Fetches all attempts of a login.
@param loginSecret The secret of the login whose attempts will be requested.
@param success The callback block if the request succeeds.
@param failure The callback block if the request fails.
*/
- (void)fetchAttemptsForLoginWithSecret:(NSString*)loginSecret
success:(void (^)(NSArray* attempts))success
failure:(SEAPIRequestFailureBlock)failure;

/**
Provides an interactive login with a set of credentials.
Expand Down Expand Up @@ -406,14 +438,13 @@ typedef void (^SEAPIRequestFailureBlock)(SEError* error);
@param success The callback block if the request succeeds.
@param failure The callback block if the request fails.
@warning parameters cannot be nil. Required fields are: "country_code", "provider_code", "customer_id", "return_to"
@warning parameters cannot be nil. Required fields are: "country_code", "provider_code", "return_to"
@code
// parameters example, listing only the required fields
{
"country_code": "XO"
"provider_code": "paypal_xo",
"customer_id": "customer id string",
"return_to": "http://example.com"
}
@endcode
Expand Down
116 changes: 97 additions & 19 deletions Classes/API/SEAPIRequestManager.m

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Classes/API/SEAPIRequestManager_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Salt Edge API Demo
//
// Created by nemesis on 10/29/14.
// Copyright (c) 2015 Salt Edge. All rights reserved.
// Copyright (c) 2016 Salt Edge. All rights reserved.
//

@interface SEAPIRequestManager()
Expand Down
2 changes: 1 addition & 1 deletion Classes/API/SELoginFetchingDelegate.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// SELoginFetchingDelegate.h
//
// Copyright (c) 2015 Salt Edge. https://saltedge.com
// Copyright (c) 2016 Salt Edge. https://saltedge.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion Classes/API/SERequestHandler.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// SERequestHandler.h
//
// Copyright (c) 2015 Salt Edge. https://saltedge.com
// Copyright (c) 2016 Salt Edge. https://saltedge.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
99 changes: 44 additions & 55 deletions Classes/API/SERequestHandler.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// SERequestHandler.m
//
// Copyright (c) 2015 Salt Edge. https://saltedge.com
// Copyright (c) 2016 Salt Edge. https://saltedge.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -47,12 +47,12 @@ typedef NS_ENUM(NSInteger, SERequestMethod) {
SERequestMethodPUT
};

@interface SERequestHandler (/* Private */) <NSURLConnectionDataDelegate>
static NSURLSession* _requestHandlerURLSession;

@property (nonatomic, strong) NSMutableData* responseData;
@property (nonatomic, copy) SERequestHandlerFailureBlock successBlock;
@property (nonatomic, copy) SERequestHandlerFailureBlock failureBlock;
@property (nonatomic) NSInteger responseStatusCode;
@interface SERequestHandler (/* Private */)

@property (nonatomic, copy) SERequestHandlerFailureBlock successBlock;
@property (nonatomic, copy) SERequestHandlerFailureBlock failureBlock;

@end

Expand All @@ -61,6 +61,16 @@ @implementation SERequestHandler
#pragma mark -
#pragma mark - Public API

+ (void)initialize
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSURLSessionConfiguration* sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfig.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
_requestHandlerURLSession = [NSURLSession sessionWithConfiguration:sessionConfig];
});
}

+ (void)sendDELETERequestWithURL:(NSString*)url
parameters:(NSDictionary*)parameters
headers:(NSDictionary*)headers
Expand Down Expand Up @@ -148,7 +158,34 @@ - (void)sendRequest:(SERequestMethod)method
}
}

[NSURLConnection connectionWithRequest:request delegate:self];
NSURLSessionDataTask* task = [_requestHandlerURLSession dataTaskWithRequest:request completionHandler:^(NSData* responseData, NSURLResponse* response, NSError* responseError) {
dispatch_async(dispatch_get_main_queue(), ^{
if (responseError) {
self.failureBlock(@{ kErrorClassKey : responseError.domain,
kMessageKey : responseError.localizedDescription,
kRequestKey : request
});
return;
}

NSInteger responseStatusCode = [(NSHTTPURLResponse*)response statusCode];
NSError* error;
NSDictionary* data = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
if (error) {
self.failureBlock(@{ kErrorClassKey : error.domain,
kMessageKey : error.localizedDescription,
kRequestKey : request });
} else {
if ((responseStatusCode >= 200 && responseStatusCode < 300)) {
self.successBlock(data);
} else {
self.failureBlock(data);
}
}
});
}];

[task resume];
}

- (NSString*)stringForMethod:(SERequestMethod)method
Expand Down Expand Up @@ -204,52 +241,4 @@ - (NSArray*)HTTPMethodsWithoutBody
return @[kRequestMethodGET, kRequestMethodDELETE];
}

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response
{
self.responseData = [[NSMutableData alloc] init];
self.responseStatusCode = [(NSHTTPURLResponse*)response statusCode];
}

- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
[self.responseData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection*)connection
{
NSError* error;
NSDictionary* data = [NSJSONSerialization JSONObjectWithData:self.responseData options:0 error:&error];
if (error) {
self.failureBlock(@{ kErrorClassKey : error.domain,
kMessageKey : error.localizedDescription,
kRequestKey : connection.currentRequest });
} else {
if ((self.responseStatusCode >= 200 && self.responseStatusCode < 300)) {
self.successBlock(data);
} else {
self.failureBlock(data);
}
}
}

- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
if (self.failureBlock) {
self.failureBlock(@{ kErrorClassKey : error.domain,
kMessageKey : error.localizedDescription,
kRequestKey : connection.currentRequest
});
}
}

- (NSCachedURLResponse*)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
return nil;
}

@end
2 changes: 1 addition & 1 deletion Classes/Categories/NSString+SEModelsSerializingAdditions.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// NSString+SEModelsSerializingAdditions.h
//
// Copyright (c) 2015 Salt Edge. https://saltedge.com
// Copyright (c) 2016 Salt Edge. https://saltedge.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion Classes/Categories/NSString+SEModelsSerializingAdditions.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// NSString+SEModelsSerializingAdditions.m
//
// Copyright (c) 2015 Salt Edge. https://saltedge.com
// Copyright (c) 2016 Salt Edge. https://saltedge.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion Classes/Categories/NSURL+SEQueryArgmuentsAdditions.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// NSURL+SEQueryArgmuentsAdditions.h
//
// Copyright (c) 2015 Salt Edge. https://saltedge.com
// Copyright (c) 2016 Salt Edge. https://saltedge.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion Classes/Categories/NSURL+SEQueryArgmuentsAdditions.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// NSURL+SEQueryArgmuentsAdditions.m
//
// Copyright (c) 2015 Salt Edge. https://saltedge.com
// Copyright (c) 2016 Salt Edge. https://saltedge.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// SEAPIRequestManager+SEOAuthLoginHandlingAdditions.h
//
// Copyright (c) 2015 Salt Edge. https://saltedge.com
// Copyright (c) 2016 Salt Edge. https://saltedge.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// SEAPIRequestManager+SEOAuthLoginHandlingAdditions.m
//
// Copyright (c) 2015 Salt Edge. https://saltedge.com
// Copyright (c) 2016 Salt Edge. https://saltedge.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion Classes/Models/SEAccount.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// SEAccount.h
//
// Copyright (c) 2015 Salt Edge. https://saltedge.com
// Copyright (c) 2016 Salt Edge. https://saltedge.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion Classes/Models/SEAccount.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// SEAccount.m
//
// Copyright (c) 2015 Salt Edge. https://saltedge.com
// Copyright (c) 2016 Salt Edge. https://saltedge.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion Classes/Models/SEBaseModel.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// SEBaseModel.h
//
// Copyright (c) 2015 Salt Edge. https://saltedge.com
// Copyright (c) 2016 Salt Edge. https://saltedge.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 2 additions & 2 deletions Classes/Models/SEBaseModel.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// SEBaseModel.m
//
// Copyright (c) 2015 Salt Edge. https://saltedge.com
// Copyright (c) 2016 Salt Edge. https://saltedge.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -31,7 +31,7 @@ @implementation SEBaseModel

+ (void)initialize
{
dateTimePropertiesNames = @[@"createdAt", @"updatedAt", @"deletedAt", @"lastFailAt", @"lastSuccessAt", @"lastRequestAt"];
dateTimePropertiesNames = @[@"createdAt", @"updatedAt", @"deletedAt", @"lastFailAt", @"lastSuccessAt", @"lastRequestAt", @"nextRefreshPossibleAt", @"successAt", @"failAt"];
datePropertyNames = @[@"madeOn"];
}

Expand Down
4 changes: 2 additions & 2 deletions Classes/Models/SEError.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// SEError.h
//
// Copyright (c) 2015 Salt Edge. https://saltedge.com
// Copyright (c) 2016 Salt Edge. https://saltedge.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -31,7 +31,7 @@
@interface SEError : SEBaseModel

@property (nonatomic, strong) NSString* errorClass;
@property (nonatomic, strong) NSString* message;
@property (nonatomic, strong) NSString* errorMessage;
@property (nonatomic, strong) NSDictionary* request;

@end
4 changes: 2 additions & 2 deletions Classes/Models/SEError.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// SEError.m
//
// Copyright (c) 2015 Salt Edge. https://saltedge.com
// Copyright (c) 2016 Salt Edge. https://saltedge.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand All @@ -27,7 +27,7 @@ @implementation SEError

- (NSString*)description
{
return [NSString stringWithFormat:@"*** ERROR: %@ -> %@. REQUEST: %@", self.errorClass, self.message, self.request];
return [NSString stringWithFormat:@"*** ERROR: %@ -> %@. REQUEST: %@", self.errorClass, self.errorMessage, self.request];
}

@end
Loading

0 comments on commit dafd331

Please sign in to comment.