참고 URL\
Doc : https://juliaweb.github.io/HTTP.jl/dev/public_interface/
https://stackoverflow.com/questions/54612993/how-to-access-an-api-via-julia-http
https://github.com/JuliaWeb/HTTP.jl/issues/243
using HTTP, JSON
client_id = "xxxxxxxxxx"
client_secret = "xxxxxxxxxx"
username = "userid"
password = "userpw"
api_token = "https://auth.xxxx.xxxx/auth/v1/token"
api_host = "https://api.xxxx.xxxx/v1"
api_campaign = "/campaigns"
"/campaigns"
function get_access_token(url::String,
client_id::String, client_secret::String,
username::String, password::String)::String
u = HTTP.URIs.URI(url)
uri = HTTP.URIs.URI(;scheme=u.scheme,
userinfo=client_id*":"*client_secret,
host=u.host,
path=u.path)
headers = [
"Accept" => "application/json",
"Content-Type" => "application/x-www-form-urlencoded"
]
query_dict = [
"grant_type" => "password",
"username" => username,
"password" => password,
"scope" => "openid"]
body = HTTP.escapeuri(query_dict)
r = HTTP.request("POST",uri,headers,body)
j =JSON.parse(String(r.body))
return j["access_token"]
end
get_access_token (generic function with 1 method)
get_access_token() = get_access_token(api_token,client_id,client_secret,username,password)
get_access_token (generic function with 2 methods)
function get_data(api::String,params::Dict)
url = api_host*api
u = HTTP.URIs.URI(url)
uri = HTTP.URIs.URI(;scheme=u.scheme,
host=u.host,
path=u.path)
println("URL:",uri)
headers = [
"Authorization" =>"Bearer "*access_token,
"Accept" => "application/json",
"Content-Type" => "application/x-www-form-urlencoded"
]
body = HTTP.escapeuri(params)
try
r = HTTP.request("GET",uri,headers,body)
j =JSON.parse(String(r.body))
if haskey(j,"result") && haskey(j,"data")
return(j["result"],j["data"])
elseif haskey(j,"result")
return(j["result"],"NO DATA RETURNED!!!!!(ERROR???)")
else
return ()
end
catch e
println(e.status)
println(e.response)
#println(fieldnames(typeof(e)))
#println(fieldnames(typeof(e.response)))
end
end
get_data (generic function with 1 method)
access_token = get_access_token()
"a6ac1aaa-ab35-xxxx-xxxx-xxxx1b79xxxx"
(result, data)
camp_lst = get_data(api_campaign,Dict([]))
camp_lst[2][1]
URL:https://api.xxxx.xxxx/v1/campaigns
Dict{String,Any} with 2 entries: "document" => Dict{String,Any}("campaignDc"=>"캠페인 등록 테스트","campaign… "resourceId" => 26
resourceId = Int(floor(rand()*length(camp_lst[2])))
println(camp_lst[2][resourceId])
Dict{String,Any}("document" => Dict{String,Any}("campaignDc" => "캠페인 등록 테스트","campaignSn" => 82,"donateEndDate" => "20211231","optInfo" => "{}","campaignName" => "테스트","campaignType" => "02","goalDonateAmount" => 100000000,"donateBeginDate" => "20210311"),"resourceId" => 26)
api_campaign_detail = api_campaign*"/$resourceId"
get_data(api_campaign_detail,Dict())
URL:https://api.xxxx.xxxx/v1/campaigns/19
(true, "NO DATA RETURNED!!!!!(ERROR???)")