- ADD OAuth2 Manager
This commit is contained in:
1 parent
515d75c316
commit
31a1afdc98
65 files changed
+4110
-2732
No files matched your search
@@ -1,60 +0,0 @@
|
||||
//
|
||||
// PNAccessToken.h
|
||||
// Pods
|
||||
//
|
||||
// Created by Giuseppe Nucifora on 27/01/16.
|
||||
//
|
||||
//
|
||||
|
||||
#import "PNObject.h"
|
||||
|
||||
@interface PNAccessToken : PNObject
|
||||
|
||||
typedef NS_ENUM(NSInteger, TokenType) {
|
||||
TokenTypeBasic = 1,
|
||||
TokenTypeBearer = 2
|
||||
};
|
||||
|
||||
/**
|
||||
* gets singleton object of current user session.
|
||||
*
|
||||
* @return singleton
|
||||
*/
|
||||
+ (instancetype _Nonnull) currentAccessToken;
|
||||
|
||||
|
||||
/**
|
||||
* gets singleton object of current user session.
|
||||
*
|
||||
* @return singleton
|
||||
*/
|
||||
+ (instancetype _Nonnull) currentAccessTokenWithJSON:(NSDictionary *)JSON;
|
||||
|
||||
///--------------------------------------
|
||||
#pragma mark - PNAccessToken Properties
|
||||
///--------------------------------------
|
||||
|
||||
|
||||
/**
|
||||
* <#Description#>
|
||||
*/
|
||||
@property (nonatomic, strong, nullable) NSString *accessToken;
|
||||
/**
|
||||
* <#Description#>
|
||||
*/
|
||||
@property (nonatomic, strong, nullable) NSDate *expirationDate;
|
||||
|
||||
@property (nonatomic, strong, nullable) NSNumber *expiresIn;
|
||||
/**
|
||||
* <#Description#>
|
||||
*/
|
||||
@property (nonatomic) TokenType tokenType;
|
||||
|
||||
@property (nonatomic, strong, nullable) NSString *tokenTypeString;
|
||||
|
||||
@property (nonatomic, strong, nullable) NSString *scope;
|
||||
|
||||
@property (nonatomic, strong, nullable) NSString *refreshToken;
|
||||
|
||||
|
||||
@end
|
||||
@@ -1,127 +0,0 @@
|
||||
//
|
||||
// PNAccessToken.m
|
||||
// Pods
|
||||
//
|
||||
// Created by Giuseppe Nucifora on 27/01/16.
|
||||
//
|
||||
//
|
||||
|
||||
#import "PNAccessToken.h"
|
||||
#import <NSDate_Utils/NSDate+NSDate_Util.h>
|
||||
|
||||
@interface PNAccessToken() <PNObjectSubclassing>
|
||||
|
||||
@end
|
||||
|
||||
@implementation PNAccessToken
|
||||
|
||||
static PNAccessToken *SINGLETON = nil;
|
||||
|
||||
static bool isFirstAccess = YES;
|
||||
|
||||
#pragma mark - Public Method
|
||||
|
||||
+ (instancetype) currentAccessToken {
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
isFirstAccess = NO;
|
||||
|
||||
SINGLETON = [[super allocWithZone:NULL] initForCurrentAccessTokenWithJSON:nil];
|
||||
});
|
||||
|
||||
return SINGLETON;
|
||||
}
|
||||
|
||||
+ (instancetype _Nonnull) currentAccessTokenWithJSON:(NSDictionary *)JSON {
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
isFirstAccess = NO;
|
||||
|
||||
SINGLETON = [[super allocWithZone:NULL] initForCurrentAccessTokenWithJSON:JSON];
|
||||
});
|
||||
|
||||
return SINGLETON;
|
||||
}
|
||||
|
||||
|
||||
- (instancetype) initForCurrentAccessTokenWithJSON:(NSDictionary *)JSON {
|
||||
if(SINGLETON){
|
||||
return SINGLETON;
|
||||
}
|
||||
if (isFirstAccess) {
|
||||
[self doesNotRecognizeSelector:_cmd];
|
||||
}
|
||||
NSDictionary *savedAccessToken;
|
||||
|
||||
if (JSON) {
|
||||
savedAccessToken = JSON;
|
||||
}
|
||||
else {
|
||||
savedAccessToken = [[PNObjectModel sharedInstance] fetchObjectsWithClass:[self class]];
|
||||
}
|
||||
|
||||
if (savedAccessToken) {
|
||||
self = [super initWithJSON:savedAccessToken];
|
||||
}
|
||||
else {
|
||||
self = [super init];
|
||||
}
|
||||
|
||||
if (self) {
|
||||
if (_tokenTypeString) {
|
||||
((void (^)())@{
|
||||
@"beaer" : ^{
|
||||
_tokenType = TokenTypeBearer;
|
||||
},
|
||||
@"basic" : ^{
|
||||
_tokenType = TokenTypeBasic;
|
||||
}
|
||||
}[_tokenTypeString] ?: ^{
|
||||
|
||||
})();
|
||||
}
|
||||
|
||||
if (_expiresIn) {
|
||||
NSLog(@"%ld",[_expiresIn integerValue]);
|
||||
NSLog(@"%@",[NSDate date]);
|
||||
NSLog(@"%@",[[NSDate date] dateByAddingMinutes:[_expiresIn integerValue]]);
|
||||
NSLog(@"%@",[[NSDate date] dateByAddingMinutes:10000]);
|
||||
_expirationDate = [[NSDate date] dateByAddingMinutes:[_expiresIn integerValue]];
|
||||
}
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
|
||||
#pragma mark PNObjectSubclassing Protocol
|
||||
|
||||
+ (NSDictionary *)objcetMapping {
|
||||
|
||||
NSDictionary *mapping = @{
|
||||
@"accessToken":@"access_token",
|
||||
@"expiresIn":@"expires_in",
|
||||
@"tokenTypeString":@"token_type",
|
||||
@"tokenType":@"scope",
|
||||
@"refreshToken":@"refresh_token",
|
||||
};
|
||||
return mapping;
|
||||
}
|
||||
|
||||
- (instancetype) initWithJSON:(NSDictionary *)JSON {
|
||||
return [[self class] currentAccessTokenWithJSON:JSON];
|
||||
}
|
||||
|
||||
+ (NSString *)objectClassName {
|
||||
return @"AccessToken";
|
||||
}
|
||||
|
||||
+ (BOOL) singleInstance {
|
||||
return YES;
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
|
||||
|
||||
|
||||
@end
|
||||
@@ -14,6 +14,10 @@
|
||||
return @"Address";
|
||||
}
|
||||
|
||||
+ (NSString *)objectEndPoint {
|
||||
return @"Address";
|
||||
}
|
||||
|
||||
+ (NSDictionary *) objcetMapping {
|
||||
NSDictionary *mapping = @{@"country":@"country",
|
||||
@"province":@"province",
|
||||
|
||||
@@ -26,25 +26,29 @@ static bool isFirstAccess = YES;
|
||||
#pragma mark PNObjectSubclassing Protocol
|
||||
|
||||
+ (NSDictionary *)objcetMapping {
|
||||
|
||||
NSDictionary *mapping = @{
|
||||
@"deviceType":@"deviceType",
|
||||
@"deviceModel":@"deviceModel",
|
||||
@"deviceName":@"deviceName",
|
||||
@"osVersion":@"osVersion",
|
||||
@"deviceToken":@"deviceToken",
|
||||
@"badge":@"badge",
|
||||
@"localeIdentifier":@"localeIdentifier",
|
||||
};
|
||||
return mapping;
|
||||
|
||||
NSDictionary *mapping = @{
|
||||
@"deviceType":@"deviceType",
|
||||
@"deviceModel":@"deviceModel",
|
||||
@"deviceName":@"deviceName",
|
||||
@"osVersion":@"osVersion",
|
||||
@"deviceToken":@"deviceToken",
|
||||
@"badge":@"badge",
|
||||
@"localeIdentifier":@"localeIdentifier",
|
||||
};
|
||||
return mapping;
|
||||
}
|
||||
|
||||
+ (NSString *)objectClassName {
|
||||
return @"AccessToken";
|
||||
return @"Installation";
|
||||
}
|
||||
|
||||
+ (NSString *)objectEndPoint {
|
||||
return @"Installation";
|
||||
}
|
||||
|
||||
+ (BOOL) singleInstance {
|
||||
return YES;
|
||||
return YES;
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
@@ -53,26 +57,26 @@ static bool isFirstAccess = YES;
|
||||
|
||||
|
||||
+ (instancetype) currentInstallation {
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
isFirstAccess = NO;
|
||||
|
||||
SINGLETON = [[super allocWithZone:NULL] init];
|
||||
});
|
||||
|
||||
return SINGLETON;
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
isFirstAccess = NO;
|
||||
|
||||
SINGLETON = [[super allocWithZone:NULL] init];
|
||||
});
|
||||
|
||||
return SINGLETON;
|
||||
}
|
||||
|
||||
- (void)setDeviceTokenFromData:(NSData *)deviceTokenData {
|
||||
|
||||
_deviceTokenData = deviceTokenData;
|
||||
|
||||
NSString *ptoken = [[[[deviceTokenData description]
|
||||
stringByReplacingOccurrencesOfString:@"<"withString:@""]
|
||||
stringByReplacingOccurrencesOfString:@">" withString:@""]
|
||||
stringByReplacingOccurrencesOfString: @" " withString: @""];
|
||||
|
||||
_deviceToken = ptoken;
|
||||
|
||||
_deviceTokenData = deviceTokenData;
|
||||
|
||||
NSString *ptoken = [[[[deviceTokenData description]
|
||||
stringByReplacingOccurrencesOfString:@"<"withString:@""]
|
||||
stringByReplacingOccurrencesOfString:@">" withString:@""]
|
||||
stringByReplacingOccurrencesOfString: @" " withString: @""];
|
||||
|
||||
_deviceToken = ptoken;
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
@@ -81,31 +85,31 @@ static bool isFirstAccess = YES;
|
||||
|
||||
- (id) init
|
||||
{
|
||||
if(SINGLETON){
|
||||
return SINGLETON;
|
||||
}
|
||||
if (isFirstAccess) {
|
||||
[self doesNotRecognizeSelector:_cmd];
|
||||
}
|
||||
|
||||
NSDictionary *savedInstallation = [[PNObjectModel sharedInstance] fetchObjectsWithClass:[self class]];
|
||||
|
||||
if (savedInstallation) {
|
||||
self = [super initWithJSON:savedInstallation];
|
||||
}
|
||||
else {
|
||||
self = [super init];
|
||||
}
|
||||
|
||||
if (self) {
|
||||
|
||||
_deviceType = @"iOS";
|
||||
_deviceModel = [[UIDevice currentDevice] model];
|
||||
_osVersion = [[UIDevice currentDevice] systemVersion];
|
||||
_deviceName = [[UIDevice currentDevice] name];
|
||||
|
||||
}
|
||||
return self;
|
||||
if(SINGLETON){
|
||||
return SINGLETON;
|
||||
}
|
||||
if (isFirstAccess) {
|
||||
[self doesNotRecognizeSelector:_cmd];
|
||||
}
|
||||
|
||||
NSDictionary *savedInstallation = [[PNObjectModel sharedInstance] fetchObjectsWithClass:[self class]];
|
||||
|
||||
if (savedInstallation) {
|
||||
self = [super initWithJSON:savedInstallation];
|
||||
}
|
||||
else {
|
||||
self = [super init];
|
||||
}
|
||||
|
||||
if (self) {
|
||||
|
||||
_deviceType = @"iOS";
|
||||
_deviceModel = [[UIDevice currentDevice] model];
|
||||
_osVersion = [[UIDevice currentDevice] systemVersion];
|
||||
_deviceName = [[UIDevice currentDevice] name];
|
||||
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
|
||||
@@ -14,6 +14,9 @@
|
||||
return @"Location";
|
||||
}
|
||||
|
||||
+(NSString *)objectEndPoint {
|
||||
return @"Location";
|
||||
}
|
||||
|
||||
+ (NSDictionary *) objcetMapping {
|
||||
NSDictionary *mapping = @{@"lat":@"lat",
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
//
|
||||
|
||||
#import "PNObject.h"
|
||||
#import "PNAccessToken.h"
|
||||
|
||||
@interface PNUser : PNObject
|
||||
|
||||
|
||||
@@ -27,119 +27,118 @@ static bool isFirstAccess = YES;
|
||||
#pragma mark - Public Method
|
||||
|
||||
+ (instancetype) currentUser {
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
isFirstAccess = NO;
|
||||
|
||||
SINGLETON = [[super allocWithZone:NULL] initForCurrentUser];
|
||||
});
|
||||
|
||||
return SINGLETON;
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
isFirstAccess = NO;
|
||||
|
||||
SINGLETON = [[super allocWithZone:NULL] initForCurrentUser];
|
||||
});
|
||||
|
||||
return SINGLETON;
|
||||
}
|
||||
|
||||
#pragma mark - Life Cycle
|
||||
|
||||
- (instancetype)copy
|
||||
{
|
||||
return [[PNUser alloc] init];
|
||||
return [[PNUser alloc] init];
|
||||
}
|
||||
|
||||
- (instancetype)mutableCopy
|
||||
{
|
||||
return [[PNUser alloc] init];
|
||||
return [[PNUser alloc] init];
|
||||
}
|
||||
|
||||
- (instancetype) initForCurrentUser
|
||||
{
|
||||
if(SINGLETON){
|
||||
return SINGLETON;
|
||||
}
|
||||
if (isFirstAccess) {
|
||||
[self doesNotRecognizeSelector:_cmd];
|
||||
}
|
||||
|
||||
NSDictionary *savedUser = [[PNObjectModel sharedInstance] fetchObjectsWithClass:[self class]];
|
||||
|
||||
if (savedUser) {
|
||||
self = [super initWithJSON:savedUser];
|
||||
}
|
||||
else {
|
||||
self = [super init];
|
||||
}
|
||||
|
||||
if (self) {
|
||||
}
|
||||
return self;
|
||||
if(SINGLETON){
|
||||
return SINGLETON;
|
||||
}
|
||||
if (isFirstAccess) {
|
||||
[self doesNotRecognizeSelector:_cmd];
|
||||
}
|
||||
|
||||
NSDictionary *savedUser = [[PNObjectModel sharedInstance] fetchObjectsWithClass:[self class]];
|
||||
|
||||
if (savedUser) {
|
||||
self = [super initWithJSON:savedUser];
|
||||
}
|
||||
else {
|
||||
self = [super init];
|
||||
}
|
||||
|
||||
if (self) {
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void) setEmail:(NSString *)email {
|
||||
if ([email isValidEmail]) {
|
||||
_email = email;
|
||||
}
|
||||
else {
|
||||
NSLogDebug(@"insertedEmail is not valid.");
|
||||
}
|
||||
if ([email isValidEmail]) {
|
||||
_email = email;
|
||||
}
|
||||
else {
|
||||
NSLogDebug(@"insertedEmail is not valid.");
|
||||
}
|
||||
}
|
||||
|
||||
- (void) setPassword:(NSString *)password {
|
||||
if ([self isValidPassword:password]) {
|
||||
_password = password;
|
||||
}
|
||||
else {
|
||||
NSLogDebug(@"Inserted Passord is not valid.Lenght must be >= %ld",(long)[[PNObjectConfig sharedInstance] minPasswordLenght]);
|
||||
}
|
||||
if ([self isValidPassword:password]) {
|
||||
_password = password;
|
||||
}
|
||||
else {
|
||||
NSLogDebug(@"Inserted Passord is not valid.Lenght must be >= %ld",(long)[[PNObjectConfig sharedInstance] minPasswordLenght]);
|
||||
}
|
||||
}
|
||||
|
||||
- (BOOL) isValidPassword:(NSString* _Nonnull) password {
|
||||
if ([password length] >= [[PNObjectConfig sharedInstance] minPasswordLenght]) {
|
||||
return YES;
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
|
||||
|
||||
- (NSString *) password {
|
||||
return @"password is not readble";
|
||||
if ([password length] >= [[PNObjectConfig sharedInstance] minPasswordLenght]) {
|
||||
return YES;
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (void)logout {
|
||||
[self autoRemoveLocally];
|
||||
[self resetObject];
|
||||
[self autoRemoveLocally];
|
||||
[self resetObject];
|
||||
}
|
||||
|
||||
#pragma mark PNObjectSubclassing Protocol
|
||||
|
||||
+ (NSDictionary *)objcetMapping {
|
||||
|
||||
NSDictionary *mapping = @{
|
||||
@"userId":@"id",
|
||||
@"firstName":@"firstName",
|
||||
@"lastName":@"lastName",
|
||||
@"profileImage":@"profileImage",
|
||||
@"sex":@"sex",
|
||||
@"birthDate":@"birthDate",
|
||||
@"phone":@"phone",
|
||||
@"password":@"password",
|
||||
@"hasAcceptedPrivacy":@"hasAcceptedPrivacy",
|
||||
@"hasAcceptedNewsletter":@"hasAcceptedNewsletter",
|
||||
@"hasVerifiedEmail":@"hasVerifiedEmail",
|
||||
@"emailVerifiedDate":@"emailVerifiedDate",
|
||||
@"email":@"email",
|
||||
@"username":@"username",
|
||||
@"publicProfile":@"public_profile",
|
||||
@"loginCount":@"login_count",
|
||||
@"facebookId":@"facebookId",
|
||||
@"facebookAccessToken":@"facebookAccessToken",
|
||||
};
|
||||
return mapping;
|
||||
|
||||
NSDictionary *mapping = @{
|
||||
@"userId":@"id",
|
||||
@"firstName":@"firstName",
|
||||
@"lastName":@"lastName",
|
||||
@"profileImage":@"profileImage",
|
||||
@"sex":@"sex",
|
||||
@"birthDate":@"birthDate",
|
||||
@"phone":@"phone",
|
||||
@"password":@"password",
|
||||
@"hasAcceptedPrivacy":@"hasAcceptedPrivacy",
|
||||
@"hasAcceptedNewsletter":@"hasAcceptedNewsletter",
|
||||
@"hasVerifiedEmail":@"hasVerifiedEmail",
|
||||
@"emailVerifiedDate":@"emailVerifiedDate",
|
||||
@"email":@"email",
|
||||
@"username":@"username",
|
||||
@"publicProfile":@"public_profile",
|
||||
@"loginCount":@"login_count",
|
||||
@"facebookId":@"facebookId",
|
||||
@"facebookAccessToken":@"facebookAccessToken",
|
||||
};
|
||||
return mapping;
|
||||
}
|
||||
|
||||
+ (NSString *)objectClassName {
|
||||
return @"User";
|
||||
return @"User";
|
||||
}
|
||||
|
||||
+ (NSString *) objectEndPoint {
|
||||
return @"User";
|
||||
}
|
||||
|
||||
+ (BOOL) singleInstance {
|
||||
return YES;
|
||||
return YES;
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
|
||||
@@ -10,8 +10,8 @@
|
||||
|
||||
@interface PNObject (PNObjectConnection)
|
||||
|
||||
- (NSURLSessionDataTask * _Nonnull)GETWithProgress:(void (^ _Nullable)(NSProgress * _Nonnull))downloadProgress
|
||||
success:(void (^ _Nullable)(NSURLSessionDataTask * _Nonnull, id _Nullable))success
|
||||
failure:(void (^ _Nullable)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull))failure;
|
||||
+ (NSURLSessionDataTask * _Nonnull) GETWithProgress:(nullable void (^)(NSProgress * _Nonnull downloadProgress)) downloadProgress
|
||||
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, PNObject * _Nullable responseObject))success
|
||||
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure;
|
||||
|
||||
@end
|
||||
@@ -16,82 +16,64 @@
|
||||
|
||||
@implementation PNObject (PNObjectConnection)
|
||||
|
||||
|
||||
|
||||
- (NSURLSessionDataTask * _Nonnull)GETWithProgress:(void (^ _Nullable)(NSProgress * _Nonnull))downloadProgress
|
||||
success:(void (^ _Nullable)(NSURLSessionDataTask * _Nonnull, id _Nullable))success
|
||||
failure:(void (^ _Nullable)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull))failure {
|
||||
|
||||
return [[[PNObjectConfig sharedInstance] manager] GET:[[[PNObjectConfig sharedInstance] PNObjEndpoint] stringByAppendingFormat:@"%@",[[self class] objectClassName]] parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
|
||||
|
||||
|
||||
NSLogDebug(@"JSON: %@", responseObject);
|
||||
NSLogDebug(@"JSON: %@", [responseObject class]);
|
||||
|
||||
self.JSON = [[NSDictionary alloc] initWithDictionary:responseObject];
|
||||
|
||||
[self populateObjectFromJSON:responseObject];
|
||||
|
||||
|
||||
} failure:^(NSURLSessionTask *operation, NSError *error) {
|
||||
|
||||
NSLogDebug(@"Error: %@", error);
|
||||
|
||||
}];
|
||||
+ (NSURLSessionDataTask * _Nonnull) GETWithProgress:(nullable void (^)(NSProgress * _Nonnull downloadProgress)) downloadProgress
|
||||
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, PNObject * _Nullable responseObject))success
|
||||
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure {
|
||||
|
||||
return [[[PNObjectConfig sharedInstance] manager] GET:[[[PNObjectConfig sharedInstance] baseUrl] stringByAppendingFormat:@"%@",[[self class] objectEndPoint]] parameters:nil progress:downloadProgress success:^(NSURLSessionDataTask *task, id responseObject) {
|
||||
|
||||
id PNObjectResponse = [[[self class] alloc] initWithJSON:[responseObject copy]];
|
||||
|
||||
if (success) {
|
||||
success(task,PNObjectResponse);
|
||||
}
|
||||
|
||||
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
|
||||
if (failure) {
|
||||
failure(task,error);
|
||||
}
|
||||
}];
|
||||
}
|
||||
|
||||
|
||||
- (NSURLSessionDataTask *)POSTWithProgress:(nullable void (^)(NSProgress * _Nonnull)) uploadProgress
|
||||
success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
|
||||
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure {
|
||||
|
||||
return [[[PNObjectConfig sharedInstance] manager] POST:[[[PNObjectConfig sharedInstance] PNObjEndpoint] stringByAppendingFormat:@"%@",[[self class] objectClassName]]
|
||||
parameters:[self JSONObject] constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
|
||||
|
||||
} progress:^(NSProgress * _Nonnull _uploadProgress) {
|
||||
if (uploadProgress) {
|
||||
uploadProgress(_uploadProgress);
|
||||
}
|
||||
} success:^(NSURLSessionDataTask * _Nonnull _task, id _Nullable _responseObject) {
|
||||
if (success) {
|
||||
success(_task,_responseObject);
|
||||
}
|
||||
} failure:^(NSURLSessionDataTask * _Nullable _task, NSError * _Nonnull _error) {
|
||||
if (failure) {
|
||||
failure(_task,_error);
|
||||
}
|
||||
}];
|
||||
}
|
||||
/*+ (nullable NSURLSessionDataTask *) POSTConstructingBodyWithBlock:(nullable void (^)(id <AFMultipartFormData> _Nonnull formData))block
|
||||
progress:(nullable void (^)(NSProgress * _Nonnull uploadProgress)) uploadProgress
|
||||
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, id _Nullable responseObject))success
|
||||
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure {
|
||||
|
||||
NSDictionary *parameters = [NSDictionary dictionary];
|
||||
|
||||
[[[PNObjectConfig sharedInstance] manager] POST:[[[PNObjectConfig sharedInstance] baseUrl] stringByAppendingFormat:@"%@",[[self class] objectEndPoint]]
|
||||
parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
|
||||
|
||||
} progress:^(NSProgress * _Nonnull uploadProgress) {
|
||||
|
||||
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
|
||||
|
||||
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
|
||||
|
||||
}];
|
||||
}
|
||||
|
||||
- (NSURLSessionDataTask * _Nonnull) GETWithProgress:(nullable void (^)(NSProgress * _Nonnull downloadProgress)) downloadProgress
|
||||
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, PNObject * _Nullable responseObject))success
|
||||
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure {
|
||||
|
||||
return [[[PNObjectConfig sharedInstance] manager] GET:[[[PNObjectConfig sharedInstance] baseUrl] stringByAppendingFormat:@"%@",[[self class] objectEndPoint]] parameters:nil progress:downloadProgress success:^(NSURLSessionDataTask *task, id responseObject) {
|
||||
|
||||
[self resetObject];
|
||||
[self populateObjectFromJSON:responseObject];
|
||||
|
||||
if (success) {
|
||||
success(task,self);
|
||||
}
|
||||
|
||||
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
|
||||
if (failure) {
|
||||
failure(task,error);
|
||||
}
|
||||
}];
|
||||
}*/
|
||||
|
||||
- (NSURLSessionDataTask *)PUTWithSuccess:(void (^)(NSURLSessionDataTask *task, id responseObject))success
|
||||
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure {
|
||||
|
||||
return [[[PNObjectConfig sharedInstance] manager] PUT:[[[PNObjectConfig sharedInstance] PNObjEndpoint] stringByAppendingFormat:@"%@",[[self class] objectClassName]]
|
||||
parameters:[self JSONObject]
|
||||
success:^(NSURLSessionDataTask * _Nonnull _task, id _Nullable _responseObject) {
|
||||
if (success) {
|
||||
success(_task,_responseObject);
|
||||
}
|
||||
} failure:^(NSURLSessionDataTask * _Nullable _task, NSError * _Nonnull _error) {
|
||||
if (failure) {
|
||||
failure(_task,_error);
|
||||
}
|
||||
}];
|
||||
}
|
||||
|
||||
- (NSURLSessionDataTask *)DELETEWithSuccess:(void (^)(NSURLSessionDataTask *task, id responseObject))success
|
||||
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure {
|
||||
return [[[PNObjectConfig sharedInstance] manager] DELETE:[[[PNObjectConfig sharedInstance] PNObjEndpoint] stringByAppendingFormat:@"%@",[[self class] objectClassName]]
|
||||
parameters:[self JSONObject]
|
||||
success:^(NSURLSessionDataTask * _Nonnull _task, id _Nullable _responseObject) {
|
||||
if (success) {
|
||||
success(_task,_responseObject);
|
||||
}
|
||||
} failure:^(NSURLSessionDataTask * _Nullable _task, NSError * _Nonnull _error) {
|
||||
if (failure) {
|
||||
failure(_task,_error);
|
||||
}
|
||||
}];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -10,7 +10,6 @@
|
||||
#import "PNObjectConfig.h"
|
||||
#import "PNObjectModel.h"
|
||||
|
||||
|
||||
@protocol PNObjectSubclassing <NSObject>
|
||||
|
||||
@required
|
||||
@@ -19,6 +18,8 @@
|
||||
|
||||
+ (BOOL) singleInstance;
|
||||
|
||||
+ (NSString * _Nonnull) objectEndPoint;
|
||||
|
||||
@optional
|
||||
|
||||
+ (NSString * _Nonnull ) objectClassName;
|
||||
@@ -29,6 +30,8 @@
|
||||
|
||||
+ (NSString * _Nonnull) PNObjClassName;
|
||||
|
||||
+ (NSString * _Nonnull) PNObjEndPoint;
|
||||
|
||||
- (_Nullable instancetype) initWithJSON:( NSDictionary * _Nonnull) JSON;
|
||||
|
||||
- (id _Nonnull) saveLocally;
|
||||
@@ -52,5 +55,4 @@
|
||||
*/
|
||||
@property (nonatomic, strong, nonnull) NSDictionary * JSONObject;
|
||||
|
||||
|
||||
@end
|
||||
@end
|
||||
+16
-1
@@ -13,6 +13,7 @@
|
||||
#import "PNObjectConstants.h"
|
||||
#import "PNObject+Protected.h"
|
||||
#import "objc/runtime.h"
|
||||
#import "PNObject+PNObjectConnection.m"
|
||||
|
||||
#define PNOBJECT_DIR @"PNObjects"
|
||||
|
||||
@@ -40,6 +41,10 @@
|
||||
return mapping;
|
||||
}
|
||||
|
||||
+ (NSString *)objectEndPoint {
|
||||
return NSStringFromClass([self class]);
|
||||
}
|
||||
|
||||
+ (BOOL) singleInstance {
|
||||
return NO;
|
||||
}
|
||||
@@ -59,6 +64,16 @@
|
||||
}
|
||||
}
|
||||
|
||||
+ (NSString * _Nonnull) PNObjEndPoint {
|
||||
if ([[self class] resolveClassMethod:@selector(objectEndPoint)]) {
|
||||
return [[self class] objectEndPoint];
|
||||
}
|
||||
else {
|
||||
return [[super class] objectEndPoint];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
- (_Nullable instancetype) init {
|
||||
self = [super init];
|
||||
|
||||
@@ -92,7 +107,7 @@
|
||||
self = [super init];
|
||||
if (self) {
|
||||
if ([[self class] isSubclassOfClass:[PNObject class]]) {
|
||||
NSAssert([[self class] conformsToProtocol:@protocol(PNObjectSubclassing)], @"Subclass object must conform to PNObjectSubclassing");
|
||||
NSAssert([[self class] conformsToProtocol:@protocol(PNObjectSubclassing)], @"Subclass object must conform to PNObjectSubclassing Protocol");
|
||||
|
||||
_objID = [[NSProcessInfo processInfo] globallyUniqueString];
|
||||
|
||||
|
||||
@@ -9,16 +9,32 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <AFNetworking/AFNetworking.h>
|
||||
|
||||
#import <AFOAuth2Manager/AFOAuth2Manager.h>
|
||||
#import <AFOAuth2Manager/AFHTTPRequestSerializer+OAuth2.h>
|
||||
#import <AFOAuth2Manager/AFOAuthCredential.h>
|
||||
|
||||
typedef NS_ENUM(NSInteger, Environment) {
|
||||
Development,
|
||||
Stage,
|
||||
Production
|
||||
Development = 0,
|
||||
Stage = 1,
|
||||
Production = 2
|
||||
};
|
||||
|
||||
extern NSString* const EnvironmentProduction;
|
||||
extern NSString* const EnvironmentStage;
|
||||
extern NSString* const EnvironmentDevelopment;
|
||||
|
||||
#pragma mark LocalNotification Keys
|
||||
|
||||
extern NSString* _Nonnull const PNObjectLocalNotificationRefreshTokenClientCredentialSuccess;
|
||||
extern NSString* _Nonnull const PNObjectLocalNotificationRefreshTokenClientCredentialFail;
|
||||
extern NSString* _Nonnull const PNObjectLocalNotificationRefreshTokenUserSuccess;
|
||||
extern NSString* _Nonnull const PNObjectLocalNotificationRefreshTokenUserFail;
|
||||
|
||||
#pragma mark -
|
||||
|
||||
extern NSString* _Nonnull const EnvironmentProduction;
|
||||
extern NSString* _Nonnull const EnvironmentStage;
|
||||
extern NSString* _Nonnull const EnvironmentDevelopment;
|
||||
|
||||
extern NSString* _Nonnull const Client_ID;
|
||||
extern NSString* _Nonnull const Client_Secret;
|
||||
|
||||
@interface PNObjectConfig : NSObject
|
||||
|
||||
@@ -26,10 +42,10 @@ extern NSString* const EnvironmentDevelopment;
|
||||
* gets singleton object.
|
||||
* @return singleton
|
||||
*/
|
||||
+ (instancetype) sharedInstance;
|
||||
+ (instancetype _Nonnull) sharedInstance;
|
||||
|
||||
/**
|
||||
*
|
||||
* Oauth is NO by default
|
||||
*
|
||||
* @param clientIdsForEnvironments
|
||||
* For example,
|
||||
@@ -40,7 +56,22 @@ extern NSString* const EnvironmentDevelopment;
|
||||
*
|
||||
* @return singleton
|
||||
*/
|
||||
+ (instancetype) initSharedInstanceForEnvironments:(NSDictionary *) endpointUrlsForEnvironments;
|
||||
+ (instancetype _Nonnull) initSharedInstanceForEnvironments:(NSDictionary * _Nonnull) endpointUrlsForEnvironments;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param clientIdsForEnvironments
|
||||
* For example,
|
||||
* @{ PNObjectConfigDevelopment : @"https://development.it/api/v1",
|
||||
* PNObjectConfigEnvStage : @"ttps://stage.it/api/v1",
|
||||
* PNObjectConfigEnvProduction : @"ttps://production.it/api/v1"
|
||||
* }
|
||||
* @param oauthEnabled <#oauthEnabled description#>
|
||||
*
|
||||
* @return singleton
|
||||
*/
|
||||
+ (instancetype _Nonnull) initSharedInstanceForEnvironments:(NSDictionary * _Nonnull) endpointUrlsForEnvironments withOauth:(BOOL) oauthEnabled;
|
||||
|
||||
/**
|
||||
* <#Description#>
|
||||
@@ -49,12 +80,30 @@ extern NSString* const EnvironmentDevelopment;
|
||||
*/
|
||||
- (void) setEnvironment:(Environment) env;
|
||||
|
||||
/**
|
||||
* <#Description#>
|
||||
*
|
||||
* @param value <#value description#>
|
||||
* @param key <#key description#>
|
||||
*/
|
||||
- (void) setHTTPHeaderValue:(NSString * _Nonnull)value forKey:(NSString * _Nonnull) key;
|
||||
|
||||
/**
|
||||
* <#Description#>
|
||||
*
|
||||
* @param value <#value description#>
|
||||
* @param key <#key description#>
|
||||
*/
|
||||
- (void) removeHTTPHeaderValueForKey:(NSString * _Nonnull) key;
|
||||
|
||||
- (void) setClientID:(NSString * _Nonnull) clientID clientSecret:(NSString* _Nonnull) clientSecret forEnv:(Environment) environment;
|
||||
|
||||
/**
|
||||
* <#Description#>
|
||||
*
|
||||
* @return <#return value description#>
|
||||
*/
|
||||
- (NSString *) PNObjEndpoint;
|
||||
- (NSString * _Nonnull) baseUrl;
|
||||
|
||||
/**
|
||||
* <#Description#>
|
||||
@@ -70,7 +119,8 @@ extern NSString* const EnvironmentDevelopment;
|
||||
/**
|
||||
* <#Description#>
|
||||
*/
|
||||
@property (nonatomic, strong) AFHTTPSessionManager *manager;
|
||||
@property (nonatomic, strong) AFOAuth2Manager *manager;
|
||||
|
||||
/**
|
||||
* <#Description#>
|
||||
*/
|
||||
|
||||
+207
-105
@@ -1,29 +1,49 @@
|
||||
//
|
||||
// PNObjectConfig.m
|
||||
// Pods
|
||||
//
|
||||
// Created by Giuseppe Nucifora on 08/01/16.
|
||||
//
|
||||
//
|
||||
//
|
||||
// PNObjectConfig.m
|
||||
// Pods
|
||||
//
|
||||
// Created by Giuseppe Nucifora on 08/01/16.
|
||||
//
|
||||
//
|
||||
|
||||
#import "PNObjectConfig.h"
|
||||
#import "PNObjectConstants.h"
|
||||
#import "PNUser.h"
|
||||
|
||||
|
||||
NSString * const PNObjectLocalNotificationRefreshTokenClientCredentialSuccess = @"PNObjectLocalNotificationRefreshTokenClientCredentialSuccess";
|
||||
NSString * const PNObjectLocalNotificationRefreshTokenClientCredentialFail = @"PNObjectLocalNotificationRefreshTokenClientCredentialFail";
|
||||
|
||||
|
||||
NSString * const PNObjectLocalNotificationRefreshTokenUserSuccess = @"PNObjectLocalNotificationRefreshTokenUserSuccess";
|
||||
NSString * const PNObjectLocalNotificationRefreshTokenUserFail = @"PNObjectLocalNotificationRefreshTokenUserFail";
|
||||
|
||||
|
||||
NSInteger const minPassLenght = 4;
|
||||
|
||||
NSString * const PNObjectServiceCredentialIdentifier = @"PNObjectServiceCredentialIdentifier";
|
||||
|
||||
NSString* const EnvironmentProduction = @"PNObjectConfigEnvProduction";
|
||||
NSString* const EnvironmentStage = @"PNObjectConfigEnvStage";
|
||||
NSString* const EnvironmentDevelopment = @"PNObjectConfigDevelopment";
|
||||
|
||||
NSString* const BaseUrl = @"base_url";
|
||||
NSString* const Client_ID = @"client_id";
|
||||
NSString* const Client_Secret = @"client_secret";
|
||||
|
||||
@interface PNObjectConfig()
|
||||
|
||||
@property (nonatomic, strong) NSMutableDictionary *configuration;
|
||||
@property (nonatomic, strong) NSString *currentEnvironment;
|
||||
@property (nonatomic) BOOL devEnabled;
|
||||
@property (nonatomic) BOOL stageEnabled;
|
||||
@property (nonatomic) BOOL productionEnabled;
|
||||
@property (nonatomic) BOOL oauthEnabled;
|
||||
|
||||
@property (nonatomic, strong) AFOAuthCredential *currentOauthCredential;
|
||||
|
||||
@property (nonatomic, strong) NSMutableDictionary *configuration;
|
||||
@property (nonatomic, strong) NSMutableDictionary *headerFields;
|
||||
@property (nonatomic) Environment currentEnv;
|
||||
@property (nonatomic, strong) NSString *currentEndPointBaseUrl;
|
||||
@property (nonatomic, strong) NSString *currentOAuthClientID;
|
||||
@property (nonatomic, strong) NSString *currentOAuthClientSecret;
|
||||
@property (nonatomic, strong) NSMutableArray *environments;
|
||||
|
||||
@end
|
||||
|
||||
@@ -39,136 +59,218 @@ static bool isFirstAccess = YES;
|
||||
|
||||
+ (instancetype)sharedInstance
|
||||
{
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
isFirstAccess = NO;
|
||||
SINGLETON = [[super allocWithZone:NULL] init];
|
||||
});
|
||||
|
||||
return SINGLETON;
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
isFirstAccess = NO;
|
||||
SINGLETON = [[super allocWithZone:NULL] init];
|
||||
});
|
||||
|
||||
return SINGLETON;
|
||||
}
|
||||
|
||||
#pragma mark - Life Cycle
|
||||
|
||||
+ (instancetype) initSharedInstanceForEnvironments:(NSDictionary *) endpointUrlsForEnvironments {
|
||||
SINGLETON = [self sharedInstance];
|
||||
|
||||
if (SINGLETON) {
|
||||
for (NSString *key in [endpointUrlsForEnvironments allKeys]) {
|
||||
|
||||
((void (^)())@{
|
||||
EnvironmentDevelopment : ^{
|
||||
NSURL * endpointUrl = [NSURL URLWithString:[endpointUrlsForEnvironments objectForKey:key]];
|
||||
if (endpointUrl) {
|
||||
[SINGLETON.configuration setValue:[endpointUrl absoluteString] forKey:key];
|
||||
SINGLETON.devEnabled = YES;
|
||||
}
|
||||
|
||||
},
|
||||
EnvironmentStage : ^{
|
||||
NSURL * endpointUrl = [NSURL URLWithString:[endpointUrlsForEnvironments objectForKey:key]];
|
||||
if (endpointUrl) {
|
||||
[SINGLETON.configuration setValue:[endpointUrl absoluteString] forKey:key];
|
||||
SINGLETON.stageEnabled = YES;
|
||||
}
|
||||
},
|
||||
EnvironmentProduction : ^{
|
||||
NSURL * endpointUrl = [NSURL URLWithString:[endpointUrlsForEnvironments objectForKey:key]];
|
||||
if (endpointUrl) {
|
||||
[SINGLETON.configuration setValue:[endpointUrl absoluteString] forKey:key];
|
||||
SINGLETON.productionEnabled = YES;
|
||||
}
|
||||
}
|
||||
}[key] ?: ^{
|
||||
|
||||
})();
|
||||
}
|
||||
NSAssert(SINGLETON.productionEnabled, @"EnvironmentProduction must be valid endpoint url");
|
||||
SINGLETON.currentEnvironment = [[SINGLETON configuration] objectForKey:EnvironmentProduction];
|
||||
|
||||
}
|
||||
return SINGLETON;
|
||||
return [self initSharedInstanceForEnvironments:endpointUrlsForEnvironments withOauth:NO];
|
||||
}
|
||||
|
||||
+ (instancetype) initSharedInstanceForEnvironments:(NSDictionary *) endpointUrlsForEnvironments withOauth:(BOOL) oauthEnabled {
|
||||
SINGLETON = [self sharedInstance];
|
||||
|
||||
if (SINGLETON) {
|
||||
SINGLETON.oauthEnabled = oauthEnabled;
|
||||
for (NSString *key in [endpointUrlsForEnvironments allKeys]) {
|
||||
|
||||
if ([SINGLETON.environments containsObject:key]) {
|
||||
|
||||
NSURL * endpointUrl = [NSURL URLWithString:[endpointUrlsForEnvironments objectForKey:key]];
|
||||
if (endpointUrl) {
|
||||
[SINGLETON.configuration setValue:[NSDictionary dictionaryWithObjectsAndKeys:[endpointUrl absoluteString],BaseUrl, nil] forKey:key];
|
||||
}
|
||||
}
|
||||
}
|
||||
NSAssert([SINGLETON.configuration objectForKey:EnvironmentProduction], @"EnvironmentProduction must be valid endpoint url");
|
||||
[SINGLETON setEnvironment:Production];
|
||||
|
||||
|
||||
|
||||
}
|
||||
return SINGLETON;
|
||||
}
|
||||
|
||||
+ (id) allocWithZone:(NSZone *)zone
|
||||
{
|
||||
return [self sharedInstance];
|
||||
return [self sharedInstance];
|
||||
}
|
||||
|
||||
+ (id)copyWithZone:(struct _NSZone *)zone
|
||||
{
|
||||
return [self sharedInstance];
|
||||
return [self sharedInstance];
|
||||
}
|
||||
|
||||
+ (id)mutableCopyWithZone:(struct _NSZone *)zone
|
||||
{
|
||||
return [self sharedInstance];
|
||||
return [self sharedInstance];
|
||||
}
|
||||
|
||||
- (id)copy
|
||||
{
|
||||
return [[PNObjectConfig alloc] init];
|
||||
return [[PNObjectConfig alloc] init];
|
||||
}
|
||||
|
||||
- (id)mutableCopy
|
||||
{
|
||||
return [[PNObjectConfig alloc] init];
|
||||
return [[PNObjectConfig alloc] init];
|
||||
}
|
||||
|
||||
- (id) init
|
||||
{
|
||||
if(SINGLETON){
|
||||
return SINGLETON;
|
||||
}
|
||||
if (isFirstAccess) {
|
||||
[self doesNotRecognizeSelector:_cmd];
|
||||
}
|
||||
self = [super init];
|
||||
|
||||
if (self) {
|
||||
_configuration = [[NSMutableDictionary alloc] init];
|
||||
_minPasswordLenght = minPassLenght;
|
||||
|
||||
_manager = [AFHTTPSessionManager manager];
|
||||
}
|
||||
return self;
|
||||
if(SINGLETON){
|
||||
return SINGLETON;
|
||||
}
|
||||
if (isFirstAccess) {
|
||||
[self doesNotRecognizeSelector:_cmd];
|
||||
}
|
||||
self = [super init];
|
||||
|
||||
if (self) {
|
||||
if (_oauthEnabled) {
|
||||
_currentOauthCredential = [AFOAuthCredential retrieveCredentialWithIdentifier:PNObjectServiceCredentialIdentifier];
|
||||
}
|
||||
|
||||
_environments = [[NSMutableArray alloc] initWithArray:@[EnvironmentDevelopment,EnvironmentStage,EnvironmentProduction]];
|
||||
|
||||
_configuration = [[NSMutableDictionary alloc] init];
|
||||
_minPasswordLenght = minPassLenght;
|
||||
|
||||
_headerFields = [[NSMutableDictionary alloc] init];
|
||||
|
||||
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void) setEnvironment:(Environment) env {
|
||||
|
||||
_currentEnvironment = nil;
|
||||
|
||||
switch (env) {
|
||||
case Development:{
|
||||
if (_devEnabled) {
|
||||
_currentEnvironment = [_configuration objectForKey:EnvironmentDevelopment];
|
||||
}
|
||||
}
|
||||
break;
|
||||
case Stage:{
|
||||
if (_stageEnabled) {
|
||||
_currentEnvironment = [_configuration objectForKey:EnvironmentStage];
|
||||
}
|
||||
}
|
||||
break;
|
||||
case Production:
|
||||
default:
|
||||
if (_productionEnabled) {
|
||||
_currentEnvironment = [_configuration objectForKey:EnvironmentProduction];
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
NSAssert(_currentEnvironment,@"Selected environment generate error. Please check configuration");
|
||||
|
||||
|
||||
_currentEnv = env;
|
||||
_currentEndPointBaseUrl = nil;
|
||||
_currentOAuthClientID = nil;
|
||||
_currentOAuthClientSecret = nil;
|
||||
|
||||
if (env < [_environments count]) {
|
||||
if ([_configuration objectForKey:[_environments objectAtIndex:env]]) {
|
||||
_currentEndPointBaseUrl = [[_configuration objectForKey:[_environments objectAtIndex:env]] objectForKey:BaseUrl];
|
||||
_currentOAuthClientID = [[_configuration objectForKey:[_environments objectAtIndex:env]] objectForKey:Client_ID];
|
||||
_currentOAuthClientSecret = [[_configuration objectForKey:[_environments objectAtIndex:env]] objectForKey:Client_Secret];
|
||||
}
|
||||
}
|
||||
else {
|
||||
_currentEndPointBaseUrl = [[_configuration objectForKey:EnvironmentProduction] objectForKey:BaseUrl];
|
||||
if ([[_configuration objectForKey:EnvironmentProduction] objectForKey:Client_ID]) {
|
||||
_currentOAuthClientID = [[_configuration objectForKey:EnvironmentProduction] objectForKey:Client_ID];
|
||||
}
|
||||
if ([[_configuration objectForKey:EnvironmentProduction] objectForKey:Client_Secret]) {
|
||||
_currentOAuthClientSecret = [[_configuration objectForKey:EnvironmentProduction] objectForKey:Client_Secret];
|
||||
}
|
||||
}
|
||||
NSLog(@"%@",[[_configuration objectForKey:[_environments objectAtIndex:env]] objectForKey:BaseUrl]);
|
||||
|
||||
NSAssert(_currentEndPointBaseUrl,@"Selected environment generate error. Please check configuration");
|
||||
|
||||
[self manager];
|
||||
|
||||
}
|
||||
|
||||
- (NSString *) PNObjEndpoint {
|
||||
return _currentEnvironment;
|
||||
- (NSString *) baseUrl {
|
||||
return _currentEndPointBaseUrl;
|
||||
}
|
||||
|
||||
- (AFHTTPSessionManager *) manager {
|
||||
|
||||
BOOL canTryRefreh = NO;
|
||||
|
||||
if (!_manager) {
|
||||
_manager = [AFOAuth2Manager manager];
|
||||
}
|
||||
|
||||
if (_oauthEnabled && _currentOAuthClientID && _currentOAuthClientSecret) {
|
||||
|
||||
if (![_manager clientID]) {
|
||||
_manager = [AFOAuth2Manager managerWithBaseURL:[NSURL URLWithString:_currentEndPointBaseUrl] clientID:_currentOAuthClientID secret:_currentOAuthClientSecret];
|
||||
}
|
||||
|
||||
[_manager setUseHTTPBasicAuthentication:NO];
|
||||
|
||||
canTryRefreh = YES;
|
||||
}
|
||||
|
||||
for (NSString *key in [_headerFields allKeys]) {
|
||||
|
||||
[[_manager requestSerializer] setValue:[_headerFields objectForKey:key] forHTTPHeaderField:key];
|
||||
}
|
||||
|
||||
if (canTryRefreh) {
|
||||
|
||||
if (!_currentOauthCredential) {
|
||||
|
||||
[self tryRefreshToken];
|
||||
}
|
||||
else {
|
||||
[[_manager requestSerializer] setAuthorizationHeaderFieldWithCredential:_currentOauthCredential];
|
||||
}
|
||||
}
|
||||
|
||||
return _manager;
|
||||
}
|
||||
|
||||
- (void) tryRefreshToken {
|
||||
if (![PNUser currentUser]) {
|
||||
[_manager authenticateUsingOAuthWithURLString:[_currentEndPointBaseUrl stringByAppendingString:@"oauth-token"] username:[[PNUser currentUser] username] password:[[PNUser currentUser] password] scope:kAFOAuthPasswordCredentialsGrantType success:^(AFOAuthCredential * _Nonnull credential) {
|
||||
_currentOauthCredential = credential;
|
||||
[_manager.requestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential];
|
||||
} failure:^(NSError * _Nonnull error) {
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:PNObjectLocalNotificationRefreshTokenUserFail object:error];
|
||||
}];
|
||||
}
|
||||
else {
|
||||
|
||||
[_manager authenticateUsingOAuthWithURLString:[_currentEndPointBaseUrl stringByAppendingString:@"oauth-token"] scope:kAFOAuthClientCredentialsGrantType success:^(AFOAuthCredential * _Nonnull credential) {
|
||||
_currentOauthCredential = credential;
|
||||
[_manager.requestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential];
|
||||
} failure:^(NSError * _Nonnull error) {
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:PNObjectLocalNotificationRefreshTokenClientCredentialFail object:error];
|
||||
}];
|
||||
}
|
||||
}
|
||||
|
||||
- (void) setAcceptablePasswordLenght:(NSUInteger) passLenght {
|
||||
_minPasswordLenght = passLenght;
|
||||
_minPasswordLenght = passLenght;
|
||||
}
|
||||
|
||||
- (void) setHTTPHeaderValue:(NSString * _Nonnull)value forKey:(NSString * _Nonnull) key {
|
||||
[_headerFields setObject:value forKey:key];
|
||||
}
|
||||
|
||||
- (void) removeHTTPHeaderValueForKey:(NSString * _Nonnull) key {
|
||||
if ([_headerFields objectForKey:key]) {
|
||||
[_headerFields removeObjectForKey:key];
|
||||
}
|
||||
}
|
||||
|
||||
- (void) setClientID:(NSString * _Nonnull) clientID clientSecret:(NSString* _Nonnull) clientSecret forEnv:(Environment) environment {
|
||||
|
||||
if ([_configuration objectForKey:[_environments objectAtIndex:environment]]) {
|
||||
|
||||
NSMutableDictionary *currentConfigurationDict = [[NSMutableDictionary alloc] initWithDictionary:[_configuration objectForKey:[_environments objectAtIndex:environment]]];
|
||||
[currentConfigurationDict setObject:clientID forKey:Client_ID];
|
||||
[currentConfigurationDict setObject:clientSecret forKey:Client_Secret];
|
||||
|
||||
[_configuration setObject:currentConfigurationDict forKey:[_environments objectAtIndex:environment]];
|
||||
|
||||
if (_currentEnv == environment) {
|
||||
[self setEnvironment:environment];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
+200
-200
@@ -27,252 +27,252 @@ static bool isFirstAccess = YES;
|
||||
|
||||
+ (instancetype) sharedInstance
|
||||
{
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
isFirstAccess = NO;
|
||||
SINGLETON = [[super allocWithZone:NULL] init];
|
||||
});
|
||||
|
||||
return SINGLETON;
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
isFirstAccess = NO;
|
||||
SINGLETON = [[super allocWithZone:NULL] init];
|
||||
});
|
||||
|
||||
return SINGLETON;
|
||||
}
|
||||
|
||||
- (NSString * _Nullable) objectName:(id _Nonnull) object {
|
||||
|
||||
BOOL isPNObjectSubclass = [[object class] isSubclassOfClass:[PNObject class]];
|
||||
|
||||
if(isPNObjectSubclass) {
|
||||
|
||||
if ([[object class] conformsToProtocol:@protocol(PNObjectSubclassing)]) {
|
||||
|
||||
NSString *className;
|
||||
//if ([[object subClassDelegate] respondsToSelector:@selector(objectClassName)]) {
|
||||
|
||||
@try {
|
||||
|
||||
className = (NSString *)[[object class] performSelector:@selector(objectClassName)];
|
||||
|
||||
}
|
||||
@catch (NSException *exception) {
|
||||
return nil;
|
||||
}
|
||||
@finally {
|
||||
return className;
|
||||
}
|
||||
|
||||
|
||||
//}
|
||||
}
|
||||
}
|
||||
return nil;
|
||||
|
||||
BOOL isPNObjectSubclass = [[object class] isSubclassOfClass:[PNObject class]];
|
||||
|
||||
if(isPNObjectSubclass) {
|
||||
|
||||
if ([[object class] conformsToProtocol:@protocol(PNObjectSubclassing)]) {
|
||||
|
||||
NSString *className;
|
||||
//if ([[object subClassDelegate] respondsToSelector:@selector(objectClassName)]) {
|
||||
|
||||
@try {
|
||||
|
||||
className = (NSString *)[[object class] performSelector:@selector(objectClassName)];
|
||||
|
||||
}
|
||||
@catch (NSException *exception) {
|
||||
return nil;
|
||||
}
|
||||
@finally {
|
||||
return className;
|
||||
}
|
||||
|
||||
|
||||
//}
|
||||
}
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (BOOL) issetPNObjectModelForObject:(id _Nonnull) object {
|
||||
|
||||
NSString *className = [self objectName:object];
|
||||
|
||||
if (!className) {
|
||||
return NO;
|
||||
}
|
||||
|
||||
return [_fileManager checkPath:className];
|
||||
|
||||
NSString *className = [self objectName:object];
|
||||
|
||||
if (!className) {
|
||||
return NO;
|
||||
}
|
||||
|
||||
return [_fileManager checkPath:className];
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
|
||||
+ (id) allocWithZone:(NSZone *)zone
|
||||
{
|
||||
return [self sharedInstance];
|
||||
return [self sharedInstance];
|
||||
}
|
||||
|
||||
+ (id)copyWithZone:(struct _NSZone *)zone
|
||||
{
|
||||
return [self sharedInstance];
|
||||
return [self sharedInstance];
|
||||
}
|
||||
|
||||
+ (id)mutableCopyWithZone:(struct _NSZone *)zone
|
||||
{
|
||||
return [self sharedInstance];
|
||||
return [self sharedInstance];
|
||||
}
|
||||
|
||||
- (id)copy
|
||||
{
|
||||
return [[PNObjectModel alloc] init];
|
||||
return [[PNObjectModel alloc] init];
|
||||
}
|
||||
|
||||
- (id)mutableCopy
|
||||
{
|
||||
return [[PNObjectModel alloc] init];
|
||||
return [[PNObjectModel alloc] init];
|
||||
}
|
||||
|
||||
- (id) init
|
||||
{
|
||||
if(SINGLETON){
|
||||
return SINGLETON;
|
||||
}
|
||||
if (isFirstAccess) {
|
||||
[self doesNotRecognizeSelector:_cmd];
|
||||
}
|
||||
self = [super init];
|
||||
|
||||
if (self) {
|
||||
_fileManager = [PEARFileManager sharedInstatnce];
|
||||
|
||||
[_fileManager setRootDirectory:k_ROOT_DIR_DOCUMENTS];
|
||||
NSLogDebug(@"%@",[_fileManager getRootDirectoryPath]);
|
||||
}
|
||||
return self;
|
||||
if(SINGLETON){
|
||||
return SINGLETON;
|
||||
}
|
||||
if (isFirstAccess) {
|
||||
[self doesNotRecognizeSelector:_cmd];
|
||||
}
|
||||
self = [super init];
|
||||
|
||||
if (self) {
|
||||
_fileManager = [PEARFileManager sharedInstatnce];
|
||||
|
||||
[_fileManager setRootDirectory:k_ROOT_DIR_DOCUMENTS];
|
||||
NSLogDebug(@"%@",[_fileManager getRootDirectoryPath]);
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
#pragma mark Public Methods
|
||||
|
||||
- (id _Nonnull) fetchObjectsWithClass:(Class _Nonnull) class {
|
||||
BOOL isPNObjectSubclass = [class isSubclassOfClass:[PNObject class]];
|
||||
|
||||
if(isPNObjectSubclass) {
|
||||
|
||||
NSString *className;
|
||||
|
||||
@try {
|
||||
|
||||
className = (NSString *)[class performSelector:@selector(objectClassName)];
|
||||
|
||||
}
|
||||
@catch (NSException *exception) {
|
||||
|
||||
}
|
||||
@finally {
|
||||
|
||||
if ([_fileManager checkPath:className]) {
|
||||
return [NSKeyedUnarchiver unarchiveObjectWithData:[_fileManager fetchFileDataWithPath:className]];
|
||||
}
|
||||
else
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
BOOL isPNObjectSubclass = [class isSubclassOfClass:[PNObject class]];
|
||||
|
||||
if(isPNObjectSubclass) {
|
||||
|
||||
NSString *className;
|
||||
|
||||
@try {
|
||||
|
||||
className = (NSString *)[class performSelector:@selector(objectClassName)];
|
||||
|
||||
}
|
||||
@catch (NSException *exception) {
|
||||
|
||||
}
|
||||
@finally {
|
||||
|
||||
if ([_fileManager checkPath:className]) {
|
||||
return [NSKeyedUnarchiver unarchiveObjectWithData:[_fileManager fetchFileDataWithPath:className]];
|
||||
}
|
||||
else
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (id _Nonnull) saveLocally:(id _Nonnull) object {
|
||||
|
||||
BOOL isPNObjectSubclass = [[object class] isSubclassOfClass:[PNObject class]];
|
||||
|
||||
if(isPNObjectSubclass) {
|
||||
|
||||
if ([[object class] conformsToProtocol:@protocol(PNObjectSubclassing)]) {
|
||||
|
||||
if ([(PNObject*) object singleInstance]) {
|
||||
|
||||
SEL selector = NSSelectorFromString(@"JSONObject");
|
||||
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[PNObject class] instanceMethodSignatureForSelector:selector]];
|
||||
[invocation setSelector:selector];
|
||||
[invocation setTarget:object];
|
||||
[invocation invoke];
|
||||
|
||||
NSDictionary *objectDict;
|
||||
[invocation getReturnValue:&objectDict];
|
||||
|
||||
NSData *objectData = [NSKeyedArchiver archivedDataWithRootObject:objectDict];
|
||||
|
||||
if ([self issetPNObjectModelForObject:object]) {
|
||||
if ([_fileManager updateFileWithData:objectData filePath:[self objectName:object] permisson:@(0755)]) {
|
||||
return object;
|
||||
}
|
||||
else {
|
||||
return [NSError errorWithDomain:NSLocalizedString(@"Object cannot be updated", @"") code:kHTTPStatusCodeBadRequest userInfo:nil];
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ([_fileManager createFileWithData:objectData filePath:[self objectName:object] permisson:@(0755)]) {
|
||||
return object;
|
||||
}
|
||||
else {
|
||||
return [NSError errorWithDomain:NSLocalizedString(@"Object cannot be created", @"") code:kHTTPStatusCodeBadRequest userInfo:nil];
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ([self issetPNObjectModelForObject:object]) {
|
||||
|
||||
NSData * data = [_fileManager fetchFileDataWithPath:[self objectName:object]];
|
||||
|
||||
NSMutableArray *objects = [[NSMutableArray alloc] initWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:data]];
|
||||
|
||||
|
||||
SEL selector = NSSelectorFromString(@"JSONObject");
|
||||
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[PNObject class] instanceMethodSignatureForSelector:selector]];
|
||||
[invocation setSelector:selector];
|
||||
[invocation setTarget:object];
|
||||
[invocation invoke];
|
||||
|
||||
NSDictionary *objectDict;
|
||||
[invocation getReturnValue:&objectDict];
|
||||
|
||||
NSLogDebug(@"%@",objectDict);
|
||||
|
||||
[objects addObject:objectDict];
|
||||
|
||||
NSData *objectData = [NSKeyedArchiver archivedDataWithRootObject:objects];
|
||||
|
||||
if ([_fileManager updateFileWithData:objectData filePath:[self objectName:object] permisson:@(0755)]) {
|
||||
|
||||
return objects;
|
||||
}
|
||||
else {
|
||||
return [NSError errorWithDomain:NSLocalizedString(@"Objects list cannot be updated", @"") code:kHTTPStatusCodeBadRequest userInfo:nil];
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
NSMutableArray *objects = [[NSMutableArray alloc] init];
|
||||
|
||||
|
||||
SEL selector = NSSelectorFromString(@"JSONObject");
|
||||
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[PNObject class] instanceMethodSignatureForSelector:selector]];
|
||||
[invocation setSelector:selector];
|
||||
[invocation setTarget:object];
|
||||
[invocation invoke];
|
||||
|
||||
NSDictionary *objectDict;
|
||||
[invocation getReturnValue:&objectDict];
|
||||
|
||||
[objects addObject:objectDict];
|
||||
|
||||
NSData *objectData = [NSKeyedArchiver archivedDataWithRootObject:objects];
|
||||
|
||||
if ([_fileManager createFileWithData:objectData filePath:[self objectName:object] permisson:@(0755)]) {
|
||||
return object;
|
||||
}
|
||||
else {
|
||||
return [NSError errorWithDomain:NSLocalizedString(@"Objects list cannot be created", @"") code:kHTTPStatusCodeBadRequest userInfo:nil];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
return [NSError errorWithDomain:NSLocalizedString(@"passed object is not conform to protocol PNObjectSubclassing", @"") code:kHTTPStatusCodeBadRequest userInfo:nil];
|
||||
}
|
||||
}
|
||||
else {
|
||||
return [NSError errorWithDomain:NSLocalizedString(@"passed object is not PNObject Subclass", @"") code:kHTTPStatusCodeBadRequest userInfo:nil];
|
||||
}
|
||||
|
||||
BOOL isPNObjectSubclass = [[object class] isSubclassOfClass:[PNObject class]];
|
||||
|
||||
if(isPNObjectSubclass) {
|
||||
|
||||
if ([[object class] conformsToProtocol:@protocol(PNObjectSubclassing)]) {
|
||||
|
||||
if ([(PNObject*) object singleInstance]) {
|
||||
|
||||
SEL selector = NSSelectorFromString(@"JSONObject");
|
||||
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[PNObject class] instanceMethodSignatureForSelector:selector]];
|
||||
[invocation setSelector:selector];
|
||||
[invocation setTarget:object];
|
||||
[invocation invoke];
|
||||
|
||||
NSDictionary *objectDict;
|
||||
[invocation getReturnValue:&objectDict];
|
||||
|
||||
NSData *objectData = [NSKeyedArchiver archivedDataWithRootObject:objectDict];
|
||||
|
||||
if ([self issetPNObjectModelForObject:object]) {
|
||||
if ([_fileManager updateFileWithData:objectData filePath:[self objectName:object] permisson:@(0755)]) {
|
||||
return object;
|
||||
}
|
||||
else {
|
||||
return [NSError errorWithDomain:NSLocalizedString(@"Object cannot be updated", @"") code:kHTTPStatusCodeBadRequest userInfo:nil];
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ([_fileManager createFileWithData:objectData filePath:[self objectName:object] permisson:@(0755)]) {
|
||||
return object;
|
||||
}
|
||||
else {
|
||||
return [NSError errorWithDomain:NSLocalizedString(@"Object cannot be created", @"") code:kHTTPStatusCodeBadRequest userInfo:nil];
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ([self issetPNObjectModelForObject:object]) {
|
||||
|
||||
NSData * data = [_fileManager fetchFileDataWithPath:[self objectName:object]];
|
||||
|
||||
NSMutableArray *objects = [[NSMutableArray alloc] initWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:data]];
|
||||
|
||||
|
||||
SEL selector = NSSelectorFromString(@"JSONObject");
|
||||
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[PNObject class] instanceMethodSignatureForSelector:selector]];
|
||||
[invocation setSelector:selector];
|
||||
[invocation setTarget:object];
|
||||
[invocation invoke];
|
||||
|
||||
NSDictionary *objectDict;
|
||||
[invocation getReturnValue:&objectDict];
|
||||
|
||||
NSLogDebug(@"%@",objectDict);
|
||||
|
||||
[objects addObject:objectDict];
|
||||
|
||||
NSData *objectData = [NSKeyedArchiver archivedDataWithRootObject:objects];
|
||||
|
||||
if ([_fileManager updateFileWithData:objectData filePath:[self objectName:object] permisson:@(0755)]) {
|
||||
|
||||
return objects;
|
||||
}
|
||||
else {
|
||||
return [NSError errorWithDomain:NSLocalizedString(@"Objects list cannot be updated", @"") code:kHTTPStatusCodeBadRequest userInfo:nil];
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
NSMutableArray *objects = [[NSMutableArray alloc] init];
|
||||
|
||||
|
||||
SEL selector = NSSelectorFromString(@"JSONObject");
|
||||
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[PNObject class] instanceMethodSignatureForSelector:selector]];
|
||||
[invocation setSelector:selector];
|
||||
[invocation setTarget:object];
|
||||
[invocation invoke];
|
||||
|
||||
NSDictionary *objectDict;
|
||||
[invocation getReturnValue:&objectDict];
|
||||
|
||||
[objects addObject:objectDict];
|
||||
|
||||
NSData *objectData = [NSKeyedArchiver archivedDataWithRootObject:objects];
|
||||
|
||||
if ([_fileManager createFileWithData:objectData filePath:[self objectName:object] permisson:@(0755)]) {
|
||||
return object;
|
||||
}
|
||||
else {
|
||||
return [NSError errorWithDomain:NSLocalizedString(@"Objects list cannot be created", @"") code:kHTTPStatusCodeBadRequest userInfo:nil];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
return [NSError errorWithDomain:NSLocalizedString(@"passed object is not conform to protocol PNObjectSubclassing", @"") code:kHTTPStatusCodeBadRequest userInfo:nil];
|
||||
}
|
||||
}
|
||||
else {
|
||||
return [NSError errorWithDomain:NSLocalizedString(@"passed object is not PNObject Subclass", @"") code:kHTTPStatusCodeBadRequest userInfo:nil];
|
||||
}
|
||||
}
|
||||
|
||||
- (id _Nonnull) saveNSUSerDefautls:(id _Nonnull) object {
|
||||
|
||||
|
||||
}
|
||||
|
||||
- (BOOL) removeObjectLocally:(id _Nonnull) object {
|
||||
BOOL isPNObjectSubclass = [[object class] isSubclassOfClass:[PNObject class]];
|
||||
|
||||
if(isPNObjectSubclass) {
|
||||
|
||||
if ([[object class] conformsToProtocol:@protocol(PNObjectSubclassing)]) {
|
||||
|
||||
if ([self issetPNObjectModelForObject:object]) {
|
||||
return [_fileManager deletePath:[self objectName:object]];
|
||||
}
|
||||
}
|
||||
}
|
||||
return NO;
|
||||
BOOL isPNObjectSubclass = [[object class] isSubclassOfClass:[PNObject class]];
|
||||
|
||||
if(isPNObjectSubclass) {
|
||||
|
||||
if ([[object class] conformsToProtocol:@protocol(PNObjectSubclassing)]) {
|
||||
|
||||
if ([self issetPNObjectModelForObject:object]) {
|
||||
return [_fileManager deletePath:[self objectName:object]];
|
||||
}
|
||||
}
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in new issue
Block a user