Skip to content

Commit

Permalink
generate samples
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas1893 committed Dec 27, 2024
1 parent 56473e4 commit 5fd6825
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -636,12 +636,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics))
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,12 +636,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics))
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,12 +636,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics))
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,12 +636,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics))
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,12 +636,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics))
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,12 +636,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics))
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,12 +636,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics))
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,12 +636,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics))
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,12 +636,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics))
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,12 +636,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics))
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,12 +636,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics))
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down

0 comments on commit 5fd6825

Please sign in to comment.