Skip to content

Commit f30e998

Browse files
authored
Add query logic in Swift (#227)
## 🧰 Changes - Add query logic in Swift - URLQueryItem has url encode logic ## 🧬 QA & Testing I fixed test code in `/httpsnippet/src/targets/swift/urlsession/fixtures/` `npm run test`
1 parent 1629af6 commit f30e998

24 files changed

+143
-49
lines changed

src/targets/swift/urlsession/client.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const urlsession: Client<UrlsessionOptions> = {
2525
description: "Foundation's URLSession request",
2626
extname: '.swift',
2727
},
28-
convert: ({ allHeaders, postData, fullUrl, method }, options) => {
28+
convert: ({ allHeaders, postData, uriObj, queryObj, method }, options) => {
2929
const opts = {
3030
indent: ' ',
3131
pretty: true,
@@ -122,7 +122,36 @@ export const urlsession: Client<UrlsessionOptions> = {
122122

123123
blank();
124124

125-
push(`var request = URLRequest(url: URL(string: "${fullUrl}")!)`);
125+
push(`let url = URL(string: "${uriObj.href}")!`);
126+
127+
const queries = queryObj ? Object.entries(queryObj) : [];
128+
if (queries.length < 1) {
129+
push('var request = URLRequest(url: url)');
130+
} else {
131+
push('var components = URLComponents(url: url, resolvingAgainstBaseURL: true)!');
132+
push('let queryItems: [URLQueryItem] = [');
133+
134+
queries.forEach(query => {
135+
const key = query[0];
136+
const value = query[1];
137+
switch (Object.prototype.toString.call(value)) {
138+
case '[object String]':
139+
push(`${opts.indent}URLQueryItem(name: "${key}", value: "${value}"),`);
140+
break;
141+
case '[object Array]':
142+
value.forEach(val => {
143+
push(`${opts.indent}URLQueryItem(name: "${key}", value: "${val}"),`);
144+
});
145+
break;
146+
}
147+
});
148+
push(']');
149+
push('components.queryItems = components.queryItems.map { $0 + queryItems } ?? queryItems');
150+
151+
blank();
152+
push('var request = URLRequest(url: components.url!)');
153+
}
154+
126155
push(`request.httpMethod = "${method}"`);
127156

128157
if (req.hasHeaders) {
@@ -136,7 +165,7 @@ export const urlsession: Client<UrlsessionOptions> = {
136165
blank();
137166
// Retrieving the shared session will be less verbose than creating a new one.
138167

139-
push('let (data, response) = try await URLSession.shared.data(with: request)');
168+
push('let (data, response) = try await URLSession.shared.data(for: request)');
140169
push('print(String(decoding: data, as: UTF8.self))');
141170

142171
blank();

src/targets/swift/urlsession/fixtures/application-form-encoded.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ let headers = ["content-type": "application/x-www-form-urlencoded"]
88
var postData = Data("foo=bar".utf8)
99
postData.append(Data("&hello=world".utf8))
1010

11-
var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!)
11+
let url = URL(string: "https://httpbin.org/anything")!
12+
var request = URLRequest(url: url)
1213
request.httpMethod = "POST"
1314
request.allHTTPHeaderFields = headers
1415
request.httpBody = postData
1516

16-
let (data, response) = try await URLSession.shared.data(with: request)
17+
let (data, response) = try await URLSession.shared.data(for: request)
1718
print(String(decoding: data, as: UTF8.self))

src/targets/swift/urlsession/fixtures/application-json.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ let parameters = [
1515

1616
let postData = try JSONSerialization.data(withJSONObject: parameters, options: [])
1717

18-
var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!)
18+
let url = URL(string: "https://httpbin.org/anything")!
19+
var request = URLRequest(url: url)
1920
request.httpMethod = "POST"
2021
request.allHTTPHeaderFields = headers
2122
request.httpBody = postData
2223

23-
let (data, response) = try await URLSession.shared.data(with: request)
24+
let (data, response) = try await URLSession.shared.data(for: request)
2425
print(String(decoding: data, as: UTF8.self))

src/targets/swift/urlsession/fixtures/cookies.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import Foundation
55

66
let headers = ["cookie": "foo=bar; bar=baz"]
77

8-
var request = URLRequest(url: URL(string: "https://httpbin.org/cookies")!)
8+
let url = URL(string: "https://httpbin.org/cookies")!
9+
var request = URLRequest(url: url)
910
request.httpMethod = "GET"
1011
request.allHTTPHeaderFields = headers
1112

12-
let (data, response) = try await URLSession.shared.data(with: request)
13+
let (data, response) = try await URLSession.shared.data(for: request)
1314
print(String(decoding: data, as: UTF8.self))

src/targets/swift/urlsession/fixtures/custom-method.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import Foundation
33
import FoundationNetworking
44
#endif
55

6-
var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!)
6+
let url = URL(string: "https://httpbin.org/anything")!
7+
var request = URLRequest(url: url)
78
request.httpMethod = "PROPFIND"
89

9-
let (data, response) = try await URLSession.shared.data(with: request)
10+
let (data, response) = try await URLSession.shared.data(for: request)
1011
print(String(decoding: data, as: UTF8.self))

src/targets/swift/urlsession/fixtures/full.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,20 @@ let headers = [
1111

1212
let postData = Data("foo=bar".utf8)
1313

14-
var request = URLRequest(url: URL(string: "https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value")!)
14+
let url = URL(string: "https://httpbin.org/anything?key=value")!
15+
var components = URLComponents(url: url, resolvingAgainstBaseURL: true)!
16+
let queryItems: [URLQueryItem] = [
17+
URLQueryItem(name: "foo", value: "bar"),
18+
URLQueryItem(name: "foo", value: "baz"),
19+
URLQueryItem(name: "baz", value: "abc"),
20+
URLQueryItem(name: "key", value: "value"),
21+
]
22+
components.queryItems = components.queryItems.map { $0 + queryItems } ?? queryItems
23+
24+
var request = URLRequest(url: components.url!)
1525
request.httpMethod = "POST"
1626
request.allHTTPHeaderFields = headers
1727
request.httpBody = postData
1828

19-
let (data, response) = try await URLSession.shared.data(with: request)
29+
let (data, response) = try await URLSession.shared.data(for: request)
2030
print(String(decoding: data, as: UTF8.self))

src/targets/swift/urlsession/fixtures/headers.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ let headers = [
1010
"quoted-value": "\"quoted\" 'string'"
1111
]
1212

13-
var request = URLRequest(url: URL(string: "https://httpbin.org/headers")!)
13+
let url = URL(string: "https://httpbin.org/headers")!
14+
var request = URLRequest(url: url)
1415
request.httpMethod = "GET"
1516
request.allHTTPHeaderFields = headers
1617

17-
let (data, response) = try await URLSession.shared.data(with: request)
18+
let (data, response) = try await URLSession.shared.data(for: request)
1819
print(String(decoding: data, as: UTF8.self))

src/targets/swift/urlsession/fixtures/http-insecure.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import Foundation
33
import FoundationNetworking
44
#endif
55

6-
var request = URLRequest(url: URL(string: "http://httpbin.org/anything")!)
6+
let url = URL(string: "http://httpbin.org/anything")!
7+
var request = URLRequest(url: url)
78
request.httpMethod = "GET"
89

9-
let (data, response) = try await URLSession.shared.data(with: request)
10+
let (data, response) = try await URLSession.shared.data(for: request)
1011
print(String(decoding: data, as: UTF8.self))

src/targets/swift/urlsession/fixtures/indent-option.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import Foundation
33
import FoundationNetworking
44
#endif
55

6-
var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!)
6+
let url = URL(string: "https://httpbin.org/anything")!
7+
var request = URLRequest(url: url)
78
request.httpMethod = "GET"
89

9-
let (data, response) = try await URLSession.shared.data(with: request)
10+
let (data, response) = try await URLSession.shared.data(for: request)
1011
print(String(decoding: data, as: UTF8.self))

src/targets/swift/urlsession/fixtures/json-null-value.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ let parameters = ["foo": nil] as [String : Any]
88

99
let postData = try JSONSerialization.data(withJSONObject: parameters, options: [])
1010

11-
var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!)
11+
let url = URL(string: "https://httpbin.org/anything")!
12+
var request = URLRequest(url: url)
1213
request.httpMethod = "POST"
1314
request.allHTTPHeaderFields = headers
1415
request.httpBody = postData
1516

16-
let (data, response) = try await URLSession.shared.data(with: request)
17+
let (data, response) = try await URLSession.shared.data(for: request)
1718
print(String(decoding: data, as: UTF8.self))

0 commit comments

Comments
 (0)