This repository has been archived by the owner on Dec 18, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Fixed a bug where the wrong JWT was sent to Casper upon login - Implemented a Casper token cache
- Loading branch information
1 parent
8ddc7ab
commit bb44e68
Showing
9 changed files
with
2,611 additions
and
2,404 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// NSMutableURLRequest+Util.h | ||
// Pods | ||
// | ||
// Created by Tanner on 2/21/16. | ||
// | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
|
||
@interface NSMutableURLRequest (Util) | ||
|
||
+ (instancetype)POST:(NSString *)url body:(NSDictionary *)body headers:(NSDictionary *)headers; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// | ||
// NSMutableURLRequest+Util.m | ||
// Pods | ||
// | ||
// Created by Tanner on 2/21/16. | ||
// | ||
// | ||
|
||
#import "NSMutableURLRequest+Util.h" | ||
#import "NSString+SnapchatKit.h" | ||
|
||
|
||
@implementation NSMutableURLRequest (Util) | ||
|
||
+ (instancetype)POST:(NSString *)urlString body:(NSDictionary *)body headers:(NSDictionary *)headers { | ||
NSURL *url = [NSURL URLWithString:urlString]; | ||
if (!url) { [NSException raise:NSInvalidArgumentException format:@"Tried to create an NSURL with a malformed URL string: %@", urlString]; } | ||
|
||
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; | ||
request.HTTPMethod = @"POST"; | ||
request.HTTPBody = [[NSString queryStringWithParams:body] dataUsingEncoding:NSUTF8StringEncoding]; | ||
|
||
[headers enumerateKeysAndObjectsUsingBlock:^(NSString *key, id value, BOOL *stop) { | ||
[request setValue:value forHTTPHeaderField:key]; | ||
}]; | ||
|
||
return request; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// SKCasperCache.h | ||
// Pods | ||
// | ||
// Created by Tanner on 2/21/16. | ||
// | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
|
||
/// See the SKCasperCache class for an example implementaiton. | ||
@protocol SKCasperCache <NSObject> | ||
+ (id)fromDictionary:(NSDictionary *)oldCache; | ||
@property (nonatomic, readonly) NSDictionary *dictionaryValue; | ||
|
||
/// Add entries to the cache. Takes a full response from the casper serves. | ||
- (void)update:(NSDictionary *)response; | ||
/// Remove all cache entries | ||
- (void)clear; | ||
/// Expected to return the same data returned from the Casper API for a given endpoint. | ||
- (NSDictionary *)objectForKeyedSubscript:(NSString *)endpoint; | ||
@end | ||
|
||
|
||
@interface SKCasperCache : NSObject <SKCasperCache> | ||
|
||
/// Use this for new caches | ||
- (instancetype)init; | ||
/// @return nil if the file was empty or could not be opened. | ||
+ (instancetype)fromDictionary:(NSDictionary *)oldCache; | ||
|
||
/// Useful for serialization | ||
@property (nonatomic, readonly) NSDictionary *dictionaryValue; | ||
- (void)clear; | ||
|
||
- (void)update:(NSDictionary *)response; | ||
|
||
/// @return The data for the given endpoint with the keys "headers" and "params" | ||
- (NSDictionary *)dataForEndpoint:(NSString *)endpoint; | ||
/// Same as dataForEndpoint: | ||
- (NSDictionary *)objectForKeyedSubscript:(NSString *)endpoint; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// | ||
// SKCasperCache.m | ||
// Pods | ||
// | ||
// Created by Tanner on 2/21/16. | ||
// | ||
// | ||
|
||
#import "SKCasperCache.h" | ||
#import "SnapchatKit-Constants.h" | ||
#import "NSDictionary+SnapchatKit.h" | ||
|
||
@interface SKCasperCache () | ||
@property (nonatomic) NSMutableDictionary *cache; | ||
@end | ||
|
||
@implementation SKCasperCache | ||
|
||
#pragma mark - Initializers | ||
|
||
- (instancetype)init { | ||
self = [super init]; | ||
if (self) { | ||
_cache = [NSMutableDictionary dictionary]; | ||
} | ||
|
||
return self; | ||
} | ||
|
||
+ (instancetype)fromDictionary:(NSDictionary *)oldCache { | ||
NSDate *now = [NSDate date]; | ||
NSMutableDictionary *cache = oldCache.mutableCopy; | ||
|
||
// Remove expired objects now or at a later date | ||
for (NSDictionary *endpoint in oldCache) { | ||
if ([now compare:endpoint[@"expires"]] == NSOrderedDescending) { | ||
[cache removeObjectForKey:endpoint[@"endpoint"]]; | ||
} else { | ||
NSInteger timeLeft = [endpoint[@"expires"] timeIntervalSinceDate:now]; | ||
[self performSelector:@selector(safelyRemoveDataForEndpoint:) withObject:endpoint[@"endpoint"] afterDelay:timeLeft]; | ||
} | ||
} | ||
|
||
// Fill out and return new cache | ||
SKCasperCache *ret = [SKCasperCache new]; | ||
ret.cache = cache; | ||
return ret; | ||
} | ||
|
||
#pragma mark - Public interface | ||
|
||
- (void)update:(NSDictionary *)response { | ||
if ([response[@"code"] integerValue] == 200) { | ||
if ([response[@"force_expire_cached"] boolValue]) { | ||
[_cache removeAllObjects]; | ||
} | ||
|
||
// Cache the objects with a specified expiration date | ||
for (NSDictionary *endpoint in response[@"endpoints"]) { | ||
NSInteger cacheTime = [endpoint[@"cache_millis"] floatValue]/1000; | ||
NSMutableDictionary *m = endpoint.mutableCopy; | ||
|
||
// Content-Type header for specific endpoints | ||
if ([endpoint[@"endpoint"] isEqualToString:SKEPStories.upload] || [endpoint[@"endpoint"] isEqualToString:SKEPSnaps.upload]) { | ||
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", SKConsts.boundary]; | ||
m[@"headers"] = SKMergeDictionaries(endpoint[@"headers"], @{SKHeaders.contentType: contentType}); | ||
} | ||
|
||
m[@"expires"] = [NSDate dateWithTimeIntervalSinceNow:cacheTime]; | ||
_cache[endpoint[@"endpoint"]] = m.copy; | ||
|
||
// Remove object from cache after delay | ||
[self performSelector:@selector(safelyRemoveDataForEndpoint:) withObject:endpoint[@"endpoint"] afterDelay:cacheTime]; | ||
} | ||
} | ||
} | ||
|
||
- (void)clear { | ||
[_cache removeAllObjects]; | ||
} | ||
|
||
- (NSDictionary *)dictionaryValue { return _cache.copy; } | ||
|
||
- (NSDictionary *)dataForEndpoint:(NSString *)endpoint { | ||
return _cache[endpoint]; | ||
} | ||
|
||
- (NSDictionary *)objectForKeyedSubscript:(NSString *)endpoint { | ||
return _cache[endpoint]; | ||
} | ||
|
||
#pragma mark - Private | ||
|
||
- (void)safelyRemoveDataForEndpoint:(NSString *)endpoint { | ||
if ([NSThread isMainThread]) { | ||
[_cache removeObjectForKey:endpoint]; | ||
} else { | ||
dispatch_sync(dispatch_get_main_queue(), ^{ | ||
[_cache removeObjectForKey:endpoint]; | ||
}); | ||
} | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.