Hello everyone!
I've been trying/learning how to wrap HPE Haven OnDemand API
<https://dev.havenondemand.com/apis> with Julia: HavenOnDemand.jl. but I
havent figured out how to send multipart data, plus other parameters, it
seems to me that one can only do one or the other:
"Main `HavenOnDemand` function, used to call **Haven OnDemand** API."
function call_HOD(
endpoint :: String,
async :: Bool, # Some endpoints are `async_only`.
files :: Vector{FileParam},
options :: Dict;
api_url :: String = "https://api.havenondemand.com",
version :: Int = 1,
default_version :: Int = 1,
)
try
options["apikey"] = _HOD_API_KEY
catch
error("Use `HavenOnDemand.set_api_key(api_key::AbstractString)`
first.")
end
sync_str = async ? "async" : "sync"
r = post(
"$(api_url)/$(version)/api/$(sync_str)/$(endpoint)/v$(default_version)",
files = files, # <--- HERE'S THE PROBLEM!
data = options
)
return r.status == 200 ? json(r) : throw(HODException(r))
end
"`DataFrame` that holds the `HavenOnDemand` data used wrap the API."
const _HOD_API = readtable(
joinpath(Pkg.dir("HavenOnDemand"), "src", "api.data"),
separator = ' '
)
# Meta wrap most of the API:
for row in eachrow(_HOD_API::DataFrame)
func_name, endpoint, async_only, description = [v for (k, v) in row]
title = join([ucfirst(s) for s in split(func_name, '_')], ' ')
docstring = """
**HPE Haven OnDemand: $(title)**
`$(func_name)([kwargs...])`
$description
All the arguments are optional and they must be supplied as keyword
arguments, non valid keyword names are ignored.
For information about valid arguments, visit:
* https://dev.havenondemand.com/apis/$(endpoint)
"""
@eval begin
@doc $docstring ->
function $(symbol(func_name))(; file = [], kwargs...)
return call_HOD(
$endpoint,
$async_only,
[FileParam(open(f)) for f in file],
Dict(kwargs)
)
end
end
end
The error I get is:
ERROR: Multiple body options specified. Please only specify one
in do_stream_request at
C:\Users\Peter\.julia\v0.4\Requests\src\Requests.jl:268
If I try to use both *`file`* and *`data`* in the *`Requests.post`*
function. I need both because I send the *`apikey`* within the *`data`*
parameter, if I just use *`file`*, then the *`apikey`* is not passed to the
API and the request fails
These are some examples of what I'm trying to
achieve:
https://community.havenondemand.com/t5/Wiki/How-to-POST-a-multipart-form-data-Request/ta-p/200
Here are some implementations in other languages:
- Ruby: http://git.io/vRmJb
- Python: http://git.io/vRmfp
It all boils down to how to do something like this:
julia> post(
"$(api_url)/$(version)/api/$(sync_str)/$(endpoint)/v$(default_version)", files
= [FileParam("open(text.txt"))], data = Dict("apikey" => ENV["HOD_API_KEY"])
)
ERROR: Multiple body options specified. Please only specify one
in do_stream_request at
C:\Users\Peter\.julia\v0.4\Requests\src\Requests.jl:268
Thanks in advance!