Skip to content
This repository has been archived by the owner on Dec 18, 2022. It is now read-only.

Commit

Permalink
Now uses a propert ptoken value in the login request
Browse files Browse the repository at this point in the history
Also added support to override header values for specific endpoints
  • Loading branch information
NSExceptional committed Aug 4, 2015
1 parent 465267a commit c03a529
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 68 deletions.
4 changes: 2 additions & 2 deletions Pod/Classes/Categories/NSDictionary+SnapchatKit.m
Expand Up @@ -39,7 +39,7 @@ - (NSArray *)split:(NSUInteger)entryLimit {
}

- (NSDictionary *)dictionaryByReplacingValuesForKeys:(NSDictionary *)dictionary {
if (!dictionary || !self) return self;
if (!dictionary || !dictionary.allKeys.count || !self) return self;

NSMutableDictionary *m = self.mutableCopy;
for (NSString *key in dictionary.allKeys)
Expand All @@ -49,7 +49,7 @@ - (NSDictionary *)dictionaryByReplacingValuesForKeys:(NSDictionary *)dictionary
}

- (NSDictionary *)dictionaryByReplacingKeysWithNewKeys:(NSDictionary *)oldKeysToNewKeys {
if (!oldKeysToNewKeys || !self) return self;
if (!oldKeysToNewKeys || !oldKeysToNewKeys.allKeys.count || !self) return self;

NSMutableDictionary *m = self.mutableCopy;
[oldKeysToNewKeys enumerateKeysAndObjectsUsingBlock:^(NSString *oldKey, NSString *newKey, BOOL *stop) {
Expand Down
4 changes: 3 additions & 1 deletion Pod/Classes/Model/SKMessage.m
Expand Up @@ -65,7 +65,9 @@ - (id)initWithDictionary:(NSDictionary *)json {
_mediaIV = media[@"iv"];
_mediaKey = media[@"key"];
_mediaType = media[@"media_type"];
if (![self.mediaType isEqualToString:@"VIDEO"])
if (!self.mediaType)
_mediaType = @"UNSPECIFIED";
else if (![self.mediaType isEqualToString:@"VIDEO"])
NSLog(@"New media type: %@", self.mediaType);

break;
Expand Down
24 changes: 17 additions & 7 deletions Pod/Classes/Model/SKRequest.h
Expand Up @@ -17,16 +17,25 @@


/** @brief Replaces header field values. Useful for changing the user-agent on the fly.
@discussion Pass \c nil to remove previous overrides. */
+ (void)overrideHeaderValues:(NSDictionary *)headers;
@discussion Pass \c nil to remove previous overrides. [SKRequest overrideHeaderValues:forEndpoint:] takes precedence over this method and affects every request, but otherwise functions the same.
@param headers A dictionary of header-value key-value pairs, i.e. @code @{@"User-Agent": @"iOS 2.0"} @endcode */
+ (void)overrideHeaderValuesGlobally:(NSDictionary *)headers;

/** @brief Replaces header field values for a specific endpoint.
@discussion Pass \c nil to \c headers to remove previous overrides for a given endpoint. See [SKRequest overrideHeaderValues:] for more information.
This method takes precedence over [SKRequest overrideHeaderValuesGlobally:] and is used before [SKRequest overrideEndpoints:], so make sure to override the original endpoint.
@param headers A dictionary of header-value key-value pairs, i.e. @code @{@"User-Agent": @"iOS 2.0"} @endcode
@param endpoint The endpoint whose header values you wish to override. */
+ (void)overrideHeaderValues:(NSDictionary *)headers forEndpoint:(NSString *)endpoint;

/** @brief Replaces values for specific query keys in the scope of the given endpoint.
@discussion Pass \c nil to \e queries to remove previous overrides for \e endpoint. This method takes precedence over \c overrideValuesForKeysGlobally: and is used before \c overrideEndpoints:.
@discussion Pass \c nil to \e queries to remove previous overrides for \e endpoint.
This method takes precedence over [SKRequest overrideValuesForKeysGlobally:] and is used before [SKRequest overrideEndpoints:], so make sure to override the original endpoint.
So making a request to \b /bq/add_everyone with this query: @code
@{@"username": @"ThePantsThief",
@"acton": @"add_everyone_duh"
@"r_u_sure": @YES} @endcode
having called \c overrideEndpoints: with this dictionary: @code
having called [SKRequest overrideEndpoints:] with this dictionary: @code
@{@"/bq/add_everyone": @"/bq/unfriend_everyone"} @endcode
and having called \c overrideValuesForKeys:forEndpoint: for the \b /bq/add_everyone endpoint with this dictionary: @code
@{@"action": @"unfriend_everyone_pls",
Expand All @@ -36,16 +45,17 @@
@"acton": @"unfriend_everyone_pls"
@"r_u_sure": @NO} @endcode
without affecting the value of \c \@"action" in any other requests.
If you \a did want to override that value in \a every request, you would pass @code @{@"action": newValue} @endcode to \c overrideValuesForKeysGlobally:.
If you \a did want to override that value in \a every request, you would pass @code @{@"action": newValue} @endcode to [SKRequest overrideValuesForKeysGlobally:].
*/
+ (void)overrideValuesForKeys:(NSDictionary *)queries forEndpoint:(NSString *)endpoint;

/** @brief Replaces values for specific query keys in every request.
@discussion Pass \c nil to remove previous overrides. \c overrideValuesForKeys:forEndpoint: takes precedence over this method and affects every request, but otherwise functions the same. */
@discussion Pass \c nil to remove previous overrides. [SKRequest overrideValuesForKeys:forEndpoint:] takes precedence over this method and affects every request, but otherwise functions the same. */
+ (void)overrideValuesForKeysGlobally:(NSDictionary *)queries;

/** @brief Replaces endpoint \c some_endpoint with \e endpoints[some_endpoint].
@discussion \c overrideValuesForKeys:forEndpoint: takes precedence over this method. This method will replace all ekeys in a request query with the given values in \c endpoints. */
@discussion [SKRequest overrideValuesForKeys:forEndpoint:] and [SKRequest overrideHeaderValues:forEndpoint:] take precedence over this method.
This method will replace all ekeys in a request query with the given values in \c endpoints. */
+ (void)overrideEndpoints:(NSDictionary *)endpoints;

/**
Expand Down
41 changes: 30 additions & 11 deletions Pod/Classes/Model/SKRequest.m
Expand Up @@ -16,27 +16,36 @@ @implementation SKRequest

#pragma mark Request overrides

static NSDictionary *headerOverrides;
static NSMutableDictionary *scopeKeyValueOverrides;
static NSDictionary *globalKeyValueOverrides;
static NSDictionary *endpointOverrides;
static NSDictionary *headerOverrides;
static NSDictionary *globalParamOverrides;
static NSMutableDictionary *scopeParamOverrides;
static NSMutableDictionary *scopeHeaderOverrides;
static NSDictionary *endpointOverrides;

+ (void)overrideHeaderValues:(NSDictionary *)headers {
+ (void)overrideHeaderValuesGlobally:(NSDictionary *)headers {
NSParameterAssert([headers isKindOfClass:[NSDictionary class]] || !headers);
headerOverrides = headers;
}

+ (void)overrideHeaderValues:(NSDictionary *)headers forEndpoint:(NSString *)endpoint {
NSParameterAssert([headers isKindOfClass:[NSDictionary class]] || !headers); NSParameterAssert(endpoint);
if (!scopeHeaderOverrides)
scopeHeaderOverrides = [NSMutableDictionary dictionary];

scopeHeaderOverrides[endpoint] = headers;
}

+ (void)overrideValuesForKeys:(NSDictionary *)queries forEndpoint:(NSString *)endpoint {
NSParameterAssert([queries isKindOfClass:[NSDictionary class]] || !queries); NSParameterAssert(endpoint);
if (!scopeKeyValueOverrides)
scopeKeyValueOverrides = [NSMutableDictionary dictionary];
if (!scopeParamOverrides)
scopeParamOverrides = [NSMutableDictionary dictionary];

scopeKeyValueOverrides[endpoint] = queries;
scopeParamOverrides[endpoint] = queries;
}

+ (void)overrideValuesForKeysGlobally:(NSDictionary *)queries {
NSParameterAssert([queries isKindOfClass:[NSDictionary class]] || !queries);
globalKeyValueOverrides = queries;
globalParamOverrides = queries;
}

+ (void)overrideEndpoints:(NSDictionary *)endpoints {
Expand All @@ -45,14 +54,21 @@ + (void)overrideEndpoints:(NSDictionary *)endpoints {

void SKRequestApplyOverrides(NSString **endpoint, NSDictionary **params) {
if (params != NULL) {
*params = [*params dictionaryByReplacingValuesForKeys:globalKeyValueOverrides];
*params = [*params dictionaryByReplacingValuesForKeys:scopeKeyValueOverrides[*endpoint]];
*params = [*params dictionaryByReplacingValuesForKeys:globalParamOverrides];
*params = [*params dictionaryByReplacingValuesForKeys:scopeParamOverrides[*endpoint]];
}
if (*endpoint) {
*endpoint = endpointOverrides[*endpoint] ?: *endpoint;
}
}

NSDictionary * SKRequestApplyHeaderOverrides(NSDictionary *httpHeaders, NSString *endpoint) {
if (!httpHeaders) return @{};
httpHeaders = [httpHeaders dictionaryByReplacingValuesForKeys:headerOverrides];
httpHeaders = [httpHeaders dictionaryByReplacingValuesForKeys:scopeHeaderOverrides[endpoint]];
return httpHeaders;
}

#pragma mark Convenience

+ (NSDictionary *)parseJSON:(NSData *)jsonData {
Expand Down Expand Up @@ -126,6 +142,7 @@ - (id)initWithHeaderFields:(NSDictionary *)httpHeaders {

- (id)initWithPOSTEndpoint:(NSString *)endpoint token:(NSString *)token query:(NSDictionary *)params headers:(NSDictionary *)httpHeaders ts:(NSString *)timestamp {
if (!token) token = SKConsts.staticToken;
httpHeaders = SKRequestApplyHeaderOverrides(httpHeaders, endpoint);

self = [self initWithHeaderFields:httpHeaders];
if (self) {
Expand Down Expand Up @@ -171,6 +188,8 @@ - (id)initWithPOSTEndpoint:(NSString *)endpoint token:(NSString *)token query:(N
}

- (id)initWithGETEndpoint:(NSString *)endpoint headers:(NSDictionary *)httpHeaders {
httpHeaders = SKRequestApplyHeaderOverrides(httpHeaders, endpoint);

self = [self initWithHeaderFields:httpHeaders];
if (self) {
SKRequestApplyOverrides(&endpoint, NULL);
Expand Down

0 comments on commit c03a529

Please sign in to comment.