Class: Util::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/vas/util/client.rb

Constant Summary collapse

TASK_POLLING_INTERVAL =
1

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Client

Returns a new instance of Client.



25
26
27
28
# File 'lib/vas/util/client.rb', line 25

def initialize(username, password)
  @username = username
  @password = password
end

Instance Method Details

#delete(location) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/vas/util/client.rb', line 60

def delete(location)
  req = Net::HTTP::Delete.new(location)
  
  do_request(req) { |res|
    case res.code
      when "200"
        return
      when "202"
        await_task(res["Location"])
      else
        raise_vas_exception(res)
    end
  }
end

#get(location) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/vas/util/client.rb', line 30

def get(location)
  
  req = Net::HTTP::Get.new(location)
  
  do_request(req) { |res|
    case res.code
      when "200"
        return JSON.parse(res.body)
      else
        raise_vas_exception(res)
    end
  }
  
end

#get_stream(location, &block) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/vas/util/client.rb', line 45

def get_stream(location, &block)
  
  req = Net::HTTP::Get.new(location)
  
  do_request(req) { |res|
    case res.code
      when "200"
        return res.read_body(&block)
      else
        raise_vas_exception(res)
    end
  }
  
end

#post(location, payload, rel = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/vas/util/client.rb', line 75

def post(location, payload, rel = nil)

  req = Net::HTTP::Post.new(location)

  if (payload.is_a?(String))
    req['Content-Type'] = "text/plain"
    req.body = payload
  else
    req['Content-Type'] = "application/json"
    req.body = payload.to_json
  end

  do_request(req) { |res|
    case res.code
      when "202"
        return await_task(res["Location"], rel)
      when "200"
        return
      else
        raise_vas_exception(res)
    end
  }
end

#post_image(location, path, metadata = nil) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/vas/util/client.rb', line 99

def post_image(location, path,  = nil)
  File.open(path) { |image|
    if (.nil?)
      req = Net::HTTP::Post::Multipart.new(location, :data => UploadIO.new(image, "application/octet-stream"))
    elsif
      req = Net::HTTP::Post::Multipart.new(location, :data => UploadIO.new(image, "application/octet-stream"), :metadata => UploadIO.new(StringIO.new(.to_json), "application/json", "metadata.json"))
    end

    do_request(req) { |res|
      case res.code
        when "201"
          return res["Location"]
        else
          raise_vas_exception(res)
      end
    }
  }
end