-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feat] Add support for custom URL parameters percent encoding #56
Conversation
0b02efb
to
404e151
Compare
@matejmolnar I played with it for a bit and I would propose a bit difference solution. Custom string encoding similar like specific array encoding is edge case. In 99% cases user is ok with parameters [String: Any]. Therefore I would not push them to wrap always parameters into
|
@cejanen Agree, the intention behind designing ArrayParameter as it is was to avoid publishing a new breaking change to the library and making it optional for the user at the same time. For context, this is the // MARK: Build Query Items
private extension Requestable {
func buildQueryItems(urlParameters: [String: Any]) -> [URLQueryItem] { ... }
func buildQueryItems(key: String, value: Any) -> [URLQueryItem] {
switch value {
case let parameter as ArrayParameter:
return buildArrayParameter(key: key, parameter: parameter)
case let parameter as PercentEncodedParameter:
return buildPercentEncodedParameter(key: key, parameter: parameter)
default:
return [URLQueryItem(name: key, value: String(describing: value))]
}
}
func buildArrayParameter(
key: String,
parameter: ArrayParameter
) -> [URLQueryItem] { ... }
func buildPercentEncodedParameter(
key: String,
parameter: PercentEncodedParameter
) -> [URLQueryItem] {
[URLQueryItem(name: key, value: parameter.percentEncodedValue)]
}
}
public struct PercentEncodedParameter {
let value: String
public var percentEncodedValue: String? {
value.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)?
.replacingOccurrences(of: "+", with: "%2B")
}
public init(value: String) {
self.value = value
}
} is 'order to give a user the flexibility to encode the parameters however they want' this meant as the user can decide which parameters should be percent encoded or use literally any type of encoding for any parameter? then we'd be solving a different problem |
404e151
to
4f041ff
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested it and it doesn't work as expected. Will try to do updates. It wasn't very clear from my side.
tested URL params
page=1&test=1+1
NetworkingSampleApp/NetworkingSampleApp/API/Routers/SampleUserRouter.swift
Outdated
Show resolved
Hide resolved
…om/ios-networking into fix/55-url-encoding-and-the-+-sign
@gajddo00 @matejmolnar At the end I decided for simplest solution. By default are data encoded on URLComponent and you can pass custom encoded data which are expected like encoded already. All tests pass multiple times. |
public extension String { | ||
/// Help method to allow custom + sign encoding, more in ```CustomEncodedParameter``` | ||
func plusSignEncoded() -> Self? { | ||
self | ||
.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)? | ||
.replacingOccurrences(of: "+", with: "%2B") | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could just move this extension to the URLParametersTests file, since it's not being used anywhere else 🤔
/// // Request URL "https://test.com?specialCharacter=%3E" | ||
/// | ||
/// var urlParameters: [String: Any]? { | ||
/// ["specialCharacter": PercentEncodedParameter(">")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// ["specialCharacter": PercentEncodedParameter(">")] | |
/// ["specialCharacter": CustomEncodedParameter(">")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🍓 Agreed as in most cases custom encoding won't be necessary, we can elevate this to the user, why not.
This PR is trying to solve issue #55
Changes
Introduce a new type
PercentEncodedParameter
which can be passed asAny
inurlParameters: [String: Any]
. The value of this parameter won't get encoded during theURLRequest
creation.