如何使用OpenAssetRESTAPI从特定项目获取图像?

如何使用OpenAssetRESTAPI从特定项目获取图像?,rest,Rest,我不久前问过关于使用的问题 我是。多亏了一个来自的消息,我发现秘密密码正在使用参数进行过滤 这仍然感觉有点不舒服,因为它需要4次到OA服务器的访问,所以我可能仍然缺少一些可以将这些请求连接成一个请求的东西 我编写了一个简短的Ruby程序,它将获得一张图像来说明原理: baseURL = "http://IP.A.DDR.ESS" password = "password" username = "login" require 'net/http' require "uri" requir

我不久前问过关于使用的问题

我是。

多亏了一个来自的消息,我发现秘密密码正在使用
参数进行过滤

这仍然感觉有点不舒服,因为它需要4次到OA服务器的访问,所以我可能仍然缺少一些可以将这些请求连接成一个请求的东西

我编写了一个简短的Ruby程序,它将获得一张图像来说明原理:

baseURL  = "http://IP.A.DDR.ESS"

password = "password"
username = "login"


require 'net/http'
require "uri"
require 'json'

def ask_oa(request, baseURL, params, username, password, useParams)
    #This constructs a REST request, makes it, and returns the JSON response.
    uri = URI.parse(baseURL + request)

    if useParams
        uri.query = URI.encode_www_form(params)
    end
    puts uri

    http = Net::HTTP.new(uri.host, uri.port)
    request = Net::HTTP::Get.new(uri.request_uri)
    request.basic_auth(username, password)
    response = Net::HTTP.start(uri.hostname, uri.port) {|http|
    http.request(request)
    }
    json_response = JSON.parse(response.body)
    puts JSON.pretty_generate(json_response)

    return json_response
end

puts "Step 1"
#You'll need to change this for *your* project name
project_name = "M020707"
puts "go and get a project where the project number is "+project_name+":"
request  = "/REST/1/Projects"
params = { :textMatching  => "exact",
           :code          => project_name
         }
projectJSON = ask_oa(request, baseURL, params, username, password, true)
projectNumber = projectJSON[0]["id"]
#This ID is the ID of the Project. You use this as the Project_id to
#key into the Files list that we access next

puts "Step 2"
puts "go and get the files that have a project_id of "+projectNumber+":"
request  = "/REST/1/Files"
params = { :textMatching  => "exact",
           :project_id    => projectNumber
         }
fileJSON = ask_oa(request, baseURL, params, username, password, true)
#You'd want a more exciting way of picking a file out of that list
#probably based on the ranking of the files or some other secret knowledge
the_file_I_want = 0
fileID = fileJSON[the_file_I_want]["id"]
#This is a single file, picked from a list of all the files associated
#with the project that we pulled in step 1. Now that we have a file id
#we're back to where we were when I asked my last question on SO.
#To OA, File means the source file, we now need to pick a
#processed/formatted version of it:

puts "Step 3"
puts "go and get the size options for the file with the ID "+fileID+":"
request  = "/REST/1/Files/"+fileID+"/Sizes"
params = {}
sizeJSON = ask_oa(request, baseURL, params, username, password, false)
#implement a picker in here to get the format that you want. 0 is just
#the first, and I don't know if the order that they are provided in is
#deterministic
s = sizeJSON[0]
#This gets to the list of actual files that you can now construct a URL from

puts "Step 4"
puts "Request an image with this URL:"
puts baseURL + s["http_root"] + s["relative_path"]
#Now we can just request the file at the end of
#this url and we'll see a picture!