Skip to content

Commit 8928973

Browse files
authored
allow escaping of path params to be optional (#58)
Certain OpenAPI specs depend on the path params to be not escaped (e.g. OpenPolicyAgent specifications). This will allow the escaping of path parameters to be specified at request context level (with default as `true`), and also allows overriding at the client instance level (defaults to no overridiing). The existing behavior of always escaping is retained as the default.
1 parent 0fb4a03 commit 8928973

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/client.jl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ end
103103
long_polling_timeout::Int=DEFAULT_LONGPOLL_TIMEOUT_SECS,
104104
timeout::Int=DEFAULT_TIMEOUT_SECS,
105105
pre_request_hook::Function=noop_pre_request_hook,
106+
escape_path_params::Union{Nothing,Bool}=nothing,
106107
verbose::Union{Bool,Function}=false,
107108
)
108109
@@ -126,6 +127,7 @@ Keyword parameters:
126127
- `pre_request_hook`: A function that is called before every API call. This function must provide two methods:
127128
- `pre_request_hook(ctx::Ctx)`: This method is called before every API call. It is passed the context object that will be used for the API call. The function should return the context object to be used for the API call.
128129
- `pre_request_hook(resource_path::AbstractString, body::Any, headers::Dict{String,String})`: This method is called before every API call. It is passed the resource path, request body and request headers that will be used for the API call. The function should return those after making any modifications to them.
130+
- `escape_path_params`: Whether the path parameters should be escaped before being used in the URL. This is useful if the path parameters contain characters that are not allowed in URLs or contain path separators themselves.
129131
- `verbose`: Can be set either to a boolean or a function.
130132
- If set to true, then the client will log all HTTP requests and responses.
131133
- If set to a function, then that function will be called with the following parameters:
@@ -141,6 +143,7 @@ struct Client
141143
downloader::Downloader
142144
timeout::Ref{Int}
143145
pre_request_hook::Function # user provided hook to modify the request before it is sent
146+
escape_path_params::Union{Nothing,Bool}
144147
long_polling_timeout::Int
145148

146149
function Client(root::String;
@@ -149,6 +152,7 @@ struct Client
149152
long_polling_timeout::Int=DEFAULT_LONGPOLL_TIMEOUT_SECS,
150153
timeout::Int=DEFAULT_TIMEOUT_SECS,
151154
pre_request_hook::Function=noop_pre_request_hook,
155+
escape_path_params::Union{Nothing,Bool}=nothing,
152156
verbose::Union{Bool,Function}=false,
153157
)
154158
clntoptions = Dict{Symbol,Any}(:throw=>false)
@@ -163,7 +167,7 @@ struct Client
163167
# disable ALPN to support servers that enable both HTTP/2 and HTTP/1.1 on same port
164168
Downloads.Curl.setopt(easy, LibCURL.CURLOPT_SSL_ENABLE_ALPN, 0)
165169
end
166-
new(root, headers, get_return_type, clntoptions, downloader, Ref{Int}(timeout), pre_request_hook, long_polling_timeout)
170+
new(root, headers, get_return_type, clntoptions, downloader, Ref{Int}(timeout), pre_request_hook, escape_path_params, long_polling_timeout)
167171
end
168172
end
169173

@@ -232,14 +236,16 @@ struct Ctx
232236
timeout::Int
233237
curl_mime_upload::Ref{Any}
234238
pre_request_hook::Function
239+
escape_path_params::Bool
235240

236241
function Ctx(client::Client, method::String, return_types::Dict{Regex,Type}, resource::String, auth, body=nothing;
237242
timeout::Int=client.timeout[],
238243
pre_request_hook::Function=client.pre_request_hook,
244+
escape_path_params::Bool=something(client.escape_path_params, true),
239245
)
240246
resource = client.root * resource
241247
headers = copy(client.headers)
242-
new(client, method, return_types, resource, auth, Dict{String,String}(), Dict{String,String}(), headers, Dict{String,String}(), Dict{String,String}(), body, timeout, Ref{Any}(nothing), pre_request_hook)
248+
new(client, method, return_types, resource, auth, Dict{String,String}(), Dict{String,String}(), headers, Dict{String,String}(), Dict{String,String}(), body, timeout, Ref{Any}(nothing), pre_request_hook, escape_path_params)
243249
end
244250
end
245251

@@ -436,7 +442,8 @@ function do_request(ctx::Ctx, stream::Bool=false; stream_to::Union{Channel,Nothi
436442
# prepare the url
437443
resource_path = replace(ctx.resource, "{format}"=>"json")
438444
for (k,v) in ctx.path
439-
resource_path = replace(resource_path, "{$k}"=>escapeuri(v))
445+
esc_v = ctx.escape_path_params ? escapeuri(v) : v
446+
resource_path = replace(resource_path, "{$k}"=>esc_v)
440447
end
441448
# append query params if needed
442449
if !isempty(ctx.query)

0 commit comments

Comments
 (0)