Skip to content

Commit

Permalink
Merge pull request #2 from jpsamaroo/fix-0.7-http
Browse files Browse the repository at this point in the history
Update to 0.7, replace Requests with HTTP
  • Loading branch information
rahulkp220 committed Aug 13, 2018
2 parents 38d5d21 + fc2c604 commit 89c5f5f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 28 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: julia
julia:
- 0.6
- nightly
script:
- julia -e 'Pkg.clone(pwd()); Pkg.build("Zabbix"); Pkg.test("Zabbix"; coverage=true)';
- julia -e 'using Pkg; Pkg.clone(pwd()); Pkg.build("Zabbix"); Pkg.test("Zabbix"; coverage=true)';
after_success:
- julia -e 'cd(Pkg.dir("Zabbix")); Pkg.add("Coverage"); using Coverage; Codecov.submit(process_folder())';
- julia -e 'using Pkg; cd(Pkg.dir("Zabbix")); Pkg.add("Coverage"); using Coverage; Codecov.submit(process_folder())';
4 changes: 2 additions & 2 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
julia 0.6
Requests
julia 0.7
HTTP
JSON
42 changes: 20 additions & 22 deletions src/Zabbix.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
__precompile__()

module Zabbix

# Imports
using Requests
using HTTP
using JSON

"""
Expand All @@ -18,7 +16,7 @@ ZabbixAPI("http://SERVER_URL/zabbix/api_jsonrpc.php", "USERNAME", "PASSWORD", tr
Here the verbosity is set to true by default. You may wish to set up by passing false instead of true.
"""
type ZabbixAPI
mutable struct ZabbixAPI

# To be supplied fields
server_url::String
Expand Down Expand Up @@ -69,11 +67,11 @@ function api_version(z::ZabbixAPI)
json_data = JSON.json(dict_data)

# requests data from zabbix
if verbose info("Hitting $(z.server_url) ...") end
output = Requests.post(z.server_url,data=json_data,headers=z.headers)
if verbose @info("Hitting $(z.server_url) ...") end
output = HTTP.post(z.server_url,body=json_data,headers=z.headers)

# return response
convert(VersionNumber, JSON.parse(convert(String, output.data))["result"])
VersionNumber(JSON.parse(String(output.body))["result"])
end


Expand Down Expand Up @@ -104,11 +102,11 @@ function auth_token(z::ZabbixAPI)
json_data = JSON.json(dict_data)

# requests data from zabbix
if verbose info("Hitting $(z.server_url) ...") end
output = Requests.post(z.server_url,data=json_data,headers=z.headers)
if verbose @info("Hitting $(z.server_url) ...") end
output = HTTP.post(z.server_url,body=json_data,headers=z.headers)

# return token
JSON.parse(convert(String, output.data))["result"]
JSON.parse(String(output.body))["result"]

end

Expand Down Expand Up @@ -140,7 +138,7 @@ function get_all_hosts(z::ZabbixAPI)
verbose = z.verbose

# get token
if verbose info("Getting authentication token ... ") end
if verbose @info("Getting authentication token ... ") end
token = auth_token(z)

# construct data
Expand All @@ -149,11 +147,11 @@ function get_all_hosts(z::ZabbixAPI)
json_data = JSON.json(dict_data)

# requests data from zabbix
if verbose info("Hitting $(z.server_url) ...") end
output = Requests.post(z.server_url,data=json_data,headers=z.headers)
if verbose @info("Hitting $(z.server_url) ...") end
output = HTTP.post(z.server_url,body=json_data,headers=z.headers)

# return token
JSON.parse(convert(String, output.data))
JSON.parse(String(output.body))

end

Expand Down Expand Up @@ -272,28 +270,28 @@ INFO: Updating request id for next API call ...
"""
function make_request(z::ZabbixAPI, method::String, params=Dict())
verbose = z.verbose

if method == "apiinfo.version" && params == Dict()
return api_version(z)
else
# get token
if verbose info("Getting authentication token ... ") end
if verbose @info("Getting authentication token ... ") end
token = auth_token(z)

# construct data
dict_data = Dict("jsonrpc"=>z.jsonrpc,"id"=>z.id,"method"=>method,"auth"=>token,"params"=>params)
json_data = JSON.json(dict_data)

# requests data from zabbix
if verbose info("Hitting $(z.server_url) ...") end
output = Requests.post(z.server_url,data=json_data,headers=z.headers)
if verbose @info("Hitting $(z.server_url) ...") end
output = HTTP.post(z.server_url,body=json_data,headers=z.headers)

# increment the id for further calls
if verbose info("Updating request id for next API call ...") end
if verbose @info("Updating request id for next API call ...") end
setfield!(z,:id, z.id+1)

# return token
JSON.parse(convert(String, output.data))
JSON.parse(String(output.body))
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Zabbix
using Base.Test
using Test

z = Zabbix.ZabbixAPI("https://www.example.com/zabbix/api_jsonrpc.php","username","password")
@test typeof(z) == ZabbixAPI

0 comments on commit 89c5f5f

Please sign in to comment.