Skip to content

Commit be5ad27

Browse files
authored
Merge pull request #2 from RockfordWei/master
enforcing url encode for api requests
2 parents f6c8aba + 9a74504 commit be5ad27

2 files changed

Lines changed: 13 additions & 14 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ playground.xcworkspace
3939
/.build
4040
/Packages
4141
Package.pins
42+
*.resolved
4243
/*.xcodeproj
4344
buildlinux
4445
Package.swift.orig

Sources/Curly/HttpClient.swift

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public struct HttpClient {
2828

2929
/// Send a request - Async - with params
3030
/// This method can be used for **GET** requests to external APIs
31-
/// - Parameter params: GET request parameters
31+
/// - Parameter params: GET request parameters **CAUTION** for api with `.valueOnly` or `.keyValuePair`, the route api could be unexpected sorted.,
32+
/// i.e., ["route1": "one", "route2": "two"] may result in /route2/two/route1/one, depends on the internal NSDictionary key arrangement structure.
3233
/// - Parameter responseType: expected response type
3334
/// - Parameter completion: Closure as the completion handler
3435
/// - Parameter response: mapped model ready to use
@@ -46,23 +47,20 @@ public struct HttpClient {
4647
break
4748
case .get:
4849
if api.paramFormat == .valueOnly {
49-
for (_, value) in params {
50-
parameteredURL.append("/\(value)")
51-
}
50+
let path:[String] = params.map { "/" + "\($0.value)".stringByEncodingURL }
51+
parameteredURL.append(path.joined())
5252
} else if api.paramFormat == .keyValuePair {
53-
for (key, value) in params {
54-
parameteredURL.append("/\(key)/\(value)")
53+
let path:[String] = params.map {
54+
"/" + $0.key.stringByEncodingURL +
55+
"/" + "\($0.value)".stringByEncodingURL
5556
}
57+
parameteredURL.append(path.joined())
5658
} else if api.paramFormat == .urlEncoding {
57-
parameteredURL.append("?")
58-
var index = 0
59-
for (key, value) in params {
60-
parameteredURL.append("\(key)=\(value)")
61-
index += 1
62-
if index < params.count {
63-
parameteredURL.append("&")
64-
}
59+
let path:[String] = params.map {
60+
$0.key.stringByEncodingURL +
61+
"=" + "\($0.value)".stringByEncodingURL
6562
}
63+
parameteredURL.append("?" + path.joined(separator: "&"))
6664
}
6765
default:
6866
break

0 commit comments

Comments
 (0)