Skip to content

Commit 9dbd4c1

Browse files
authored
Merge pull request #26 from omus/cv/refactor-try
Use `isdefined` to check if API function exists
2 parents 28fd87a + 563a2c1 commit 9dbd4c1

File tree

1 file changed

+29
-32
lines changed

1 file changed

+29
-32
lines changed

src/simpleapi.jl

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ function _get_apictx(ctx::KuberContext, O::Symbol, apiversion::Union{String,Noth
2020
apictx
2121
end
2222

23+
_api_function(name::Symbol) = isdefined(@__MODULE__, name) ? eval(name) : nothing
24+
_api_function(name) = _api_function(Symbol(name))
25+
2326
function list(ctx::KuberContext, O::Symbol, name::String, apiversion::Union{String,Nothing}=nothing; namespace::Union{String,Nothing}=ctx.namespace, kwargs...)
2427
isempty(ctx.apis) && set_api_versions!(ctx)
2528

@@ -62,58 +65,55 @@ function get(ctx::KuberContext, O::Symbol, name::String, apiversion::Union{Strin
6265
isempty(ctx.apis) && set_api_versions!(ctx; max_tries=max_tries)
6366

6467
apictx = _get_apictx(ctx, O, apiversion)
65-
try
66-
apicall = eval(Symbol("read$O"))
68+
if (apicall = _api_function("read$O")) !== nothing
6769
@repeat max_tries try
6870
return apicall(apictx, name; kwargs...)
6971
catch e
7072
@retry if isa(e, IOError)
71-
@debug("Retrying ", "read$O")
73+
@debug("Retrying ", nameof(apicall))
7274
sleep(2)
7375
end
7476
end
75-
catch ex
76-
isa(ex, UndefVarError) || rethrow()
77-
apicall = eval(Symbol("readNamespaced$O"))
77+
elseif (apicall = _api_function("readNamespaced$O")) !== nothing
7878
@repeat max_tries try
7979
return apicall(apictx, name, ctx.namespace; kwargs...)
8080
catch e
8181
@retry if isa(e, IOError)
82-
@debug("Retrying ", "readNamespaced$O")
82+
@debug("Retrying ", nameof(apicall))
8383
sleep(2)
8484
end
8585
end
86+
else
87+
throw(ArgumentError("No API functions could be located using :$O"))
8688
end
8789
end
8890

8991
function get(ctx::KuberContext, O::Symbol, apiversion::Union{String,Nothing}=nothing; label_selector=nothing, namespace::Union{String,Nothing}=ctx.namespace, max_tries::Integer=1)
9092
isempty(ctx.apis) && set_api_versions!(ctx; max_tries=max_tries)
9193

9294
apictx = _get_apictx(ctx, O, apiversion)
93-
try
94-
apiname = "list$O"
95-
(namespace === nothing) && (apiname *= "ForAllNamespaces")
96-
apicall = eval(Symbol(apiname))
95+
apiname = "list$O"
96+
namespace === nothing && (apiname *= "ForAllNamespaces")
97+
if (apicall = _api_function(apiname)) !== nothing
9798
@repeat max_tries try
9899
return apicall(apictx; labelSelector=label_selector)
99100
catch e
100101
@retry if isa(e, IOError)
101-
@debug("Retrying ", apiname)
102+
@debug("Retrying ", nameof(apicall))
102103
sleep(2)
103104
end
104105
end
105-
catch ex
106-
isa(ex, UndefVarError) || rethrow()
107-
(namespace === nothing) && rethrow()
108-
apicall = eval(Symbol("listNamespaced$O"))
106+
elseif (apicall = _api_function("listNamespaced$O")) !== nothing
109107
@repeat max_tries try
110108
return apicall(apictx, namespace; labelSelector=label_selector)
111109
catch e
112110
@retry if isa(e, IOError)
113-
@debug("Retrying ", "listNamespaced$O")
111+
@debug("Retrying ", nameof(apicall))
114112
sleep(2)
115113
end
116114
end
115+
else
116+
throw(ArgumentError("No API functions could be located using :$O"))
117117
end
118118
end
119119

@@ -126,13 +126,12 @@ function put!(ctx::KuberContext, O::Symbol, d::Dict{String,Any})
126126
isempty(ctx.apis) && set_api_versions!(ctx)
127127

128128
apictx = _get_apictx(ctx, O, get(d, "apiVersion", nothing))
129-
try
130-
apicall = eval(Symbol("create$O"))
129+
if (apicall = _api_function("create$O")) !== nothing
131130
return apicall(apictx, d)
132-
catch ex
133-
isa(ex, UndefVarError) || rethrow()
134-
apicall = eval(Symbol("createNamespaced$O"))
131+
elseif (apicall = _api_function("createNamespaced$O")) !== nothing
135132
return apicall(apictx, ctx.namespace, d)
133+
else
134+
throw(ArgumentError("No API functions could be located using :$O"))
136135
end
137136
end
138137

@@ -149,14 +148,13 @@ function delete!(ctx::KuberContext, O::Symbol, name::String, apiversion::Union{S
149148

150149
params = [apictx, name]
151150

152-
try
153-
apicall = eval(Symbol("delete$O"))
151+
if (apicall = _api_function("delete$O")) !== nothing
154152
return apicall(params...; kwargs...)
155-
catch ex
156-
isa(ex, UndefVarError) || rethrow()
157-
apicall = eval(Symbol("deleteNamespaced$O"))
153+
elseif (apicall = _api_function("deleteNamespaced$O")) !== nothing
158154
push!(params, ctx.namespace)
159155
return apicall(params...; kwargs...)
156+
else
157+
throw(ArgumentError("No API functions could be located using :$O"))
160158
end
161159
end
162160

@@ -172,13 +170,12 @@ function update!(ctx::KuberContext, O::Symbol, name::String, patch, patch_type,
172170

173171
apictx = _get_apictx(ctx, O, apiversion)
174172

175-
try
176-
apicall = eval(Symbol("patch$O"))
173+
if (apicall = _api_function("patch$O")) !== nothing
177174
return apicall(apictx, name, patch; _mediaType=patch_type)
178-
catch ex
179-
isa(ex, UndefVarError) || rethrow()
180-
apicall = eval(Symbol("patchNamespaced$O"))
175+
elseif (apicall = _api_function("patchNamespaced$O")) !== nothing
181176
return apicall(apictx, name, ctx.namespace, patch; _mediaType=patch_type)
177+
else
178+
throw(ArgumentError("No API functions could be located using :$O"))
182179
end
183180
end
184181

0 commit comments

Comments
 (0)