Skip to content

Commit

Permalink
Utilize NSURLQueryItem for storing query params
Browse files Browse the repository at this point in the history
  • Loading branch information
NSExceptional committed Sep 14, 2019
1 parent d537d3c commit 8a762a6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
28 changes: 24 additions & 4 deletions Classes/Network/FLEXNetworkTransactionDetailTableViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -403,18 +403,18 @@ + (FLEXNetworkDetailSection *)postBodySectionForTransaction:(FLEXNetworkTransact
NSString *contentType = [transaction.request valueForHTTPHeaderField:@"Content-Type"];
if ([contentType hasPrefix:@"application/x-www-form-urlencoded"]) {
NSString *bodyString = [NSString stringWithCString:[self postBodyDataForTransaction:transaction].bytes encoding:NSUTF8StringEncoding];
postBodySection.rows = [self networkDetailRowsFromDictionary:[FLEXUtility dictionaryFromQuery:bodyString]];
postBodySection.rows = [self networkDetailRowsFromQueryItems:[FLEXUtility itemsFromQueryString:bodyString]];
}
}
return postBodySection;
}

+ (FLEXNetworkDetailSection *)queryParametersSectionForTransaction:(FLEXNetworkTransaction *)transaction
{
NSDictionary<NSString *, id> *queryDictionary = [FLEXUtility dictionaryFromQuery:transaction.request.URL.query];
NSArray<NSURLQueryItem *> *queries = [FLEXUtility itemsFromQueryString:transaction.request.URL.query];
FLEXNetworkDetailSection *querySection = [FLEXNetworkDetailSection new];
querySection.title = @"Query Parameters";
querySection.rows = [self networkDetailRowsFromDictionary:queryDictionary];
querySection.rows = [self networkDetailRowsFromQueryItems:queries];

return querySection;
}
Expand All @@ -432,15 +432,35 @@ + (FLEXNetworkDetailSection *)responseHeadersSectionForTransaction:(FLEXNetworkT

+ (NSArray<FLEXNetworkDetailRow *> *)networkDetailRowsFromDictionary:(NSDictionary<NSString *, id> *)dictionary
{
NSMutableArray<FLEXNetworkDetailRow *> *rows = [NSMutableArray arrayWithCapacity:dictionary.count];
NSMutableArray<FLEXNetworkDetailRow *> *rows = [NSMutableArray new];
NSArray<NSString *> *sortedKeys = [dictionary.allKeys sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

for (NSString *key in sortedKeys) {
id value = dictionary[key];
FLEXNetworkDetailRow *row = [FLEXNetworkDetailRow new];
row.title = key;
row.detailText = [value description];
[rows addObject:row];
}

return rows.copy;
}

+ (NSArray<FLEXNetworkDetailRow *> *)networkDetailRowsFromQueryItems:(NSArray<NSURLQueryItem *> *)items
{
// Sort the items by name
items = [items sortedArrayUsingComparator:^NSComparisonResult(NSURLQueryItem *item1, NSURLQueryItem *item2) {
return [item1.name caseInsensitiveCompare:item2.name];
}];

NSMutableArray<FLEXNetworkDetailRow *> *rows = [NSMutableArray new];
for (NSURLQueryItem *item in items) {
FLEXNetworkDetailRow *row = [FLEXNetworkDetailRow new];
row.title = item.name;
row.detailText = item.value;
[rows addObject:row];
}

return [rows copy];
}

Expand Down
2 changes: 1 addition & 1 deletion Classes/Utility/FLEXUtility.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
+ (NSString *)stringFromRequestDuration:(NSTimeInterval)duration;
+ (NSString *)statusCodeStringFromURLResponse:(NSURLResponse *)response;
+ (BOOL)isErrorStatusCodeFromURLResponse:(NSURLResponse *)response;
+ (NSDictionary<NSString *, id> *)dictionaryFromQuery:(NSString *)query;
+ (NSArray<NSURLQueryItem *> *)itemsFromQueryString:(NSString *)query;
+ (NSString *)prettyJSONStringFromData:(NSData *)data;
+ (BOOL)isValidJSONData:(NSData *)data;
+ (NSData *)inflatedDataFromCompressedData:(NSData *)compressedData;
Expand Down
28 changes: 8 additions & 20 deletions Classes/Utility/FLEXUtility.m
Original file line number Diff line number Diff line change
Expand Up @@ -276,44 +276,32 @@ + (NSString *)statusCodeStringFromURLResponse:(NSURLResponse *)response

+ (BOOL)isErrorStatusCodeFromURLResponse:(NSURLResponse *)response
{
NSIndexSet *errorStatusCodes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(400, 200)];

if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
return [errorStatusCodes containsIndex:httpResponse.statusCode];
return httpResponse.statusCode >= 400;
}

return NO;
}

+ (NSDictionary<NSString *, id> *)dictionaryFromQuery:(NSString *)query
+ (NSArray<NSURLQueryItem *> *)itemsFromQueryString:(NSString *)query
{
NSMutableDictionary<NSString *, id> *queryDictionary = [NSMutableDictionary dictionary];
NSMutableArray<NSURLQueryItem *> *items = [NSMutableArray new];

// [a=1, b=2, c=3]
NSArray<NSString *> *queryComponents = [query componentsSeparatedByString:@"&"];
for (NSString *keyValueString in queryComponents) {
// [a, 1]
NSArray<NSString *> *components = [keyValueString componentsSeparatedByString:@"="];
if (components.count == 2) {
NSString *key = [components.firstObject stringByRemovingPercentEncoding];
id value = [components.lastObject stringByRemovingPercentEncoding];

// Handle multiple entries under the same key as an array
id existingEntry = queryDictionary[key];
if (existingEntry) {
if ([existingEntry isKindOfClass:[NSArray class]]) {
value = [existingEntry arrayByAddingObject:value];
} else {
value = @[existingEntry, value];
}
}

[queryDictionary setObject:value forKey:key];
NSString *key = components.firstObject.stringByRemovingPercentEncoding;
NSString *value = components.lastObject.stringByRemovingPercentEncoding;

[items addObject:[NSURLQueryItem queryItemWithName:key value:value]];
}
}

return queryDictionary;
return items.copy;
}

+ (NSString *)prettyJSONStringFromData:(NSData *)data
Expand Down

0 comments on commit 8a762a6

Please sign in to comment.