Explorar o código

Utilize NSURLQueryItem for storing query params

Tanner Bennett %!s(int64=6) %!d(string=hai) anos
pai
achega
8a762a66ae

+ 24 - 4
Classes/Network/FLEXNetworkTransactionDetailTableViewController.m

@@ -403,7 +403,7 @@ typedef UIViewController *(^FLEXNetworkDetailRowSelectionFuture)(void);
         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;
@@ -411,10 +411,10 @@ typedef UIViewController *(^FLEXNetworkDetailRowSelectionFuture)(void);
 
 + (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;
 }
@@ -432,8 +432,9 @@ typedef UIViewController *(^FLEXNetworkDetailRowSelectionFuture)(void);
 
 + (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];
@@ -441,6 +442,25 @@ typedef UIViewController *(^FLEXNetworkDetailRowSelectionFuture)(void);
         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];
 }
 

+ 1 - 1
Classes/Utility/FLEXUtility.h

@@ -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;

+ 8 - 20
Classes/Utility/FLEXUtility.m

@@ -276,19 +276,17 @@
 
 + (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:@"&"];
@@ -296,24 +294,14 @@
         // [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