api_client.rb 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. require 'json'
  2. require 'net/http'
  3. require 'net/http/post/multipart'
  4. require 'uri'
  5. class ApiClient
  6. def initialize(host, base_id)
  7. @host = host
  8. @current_id = Integer(base_id)
  9. end
  10. def self.from_environment
  11. ApiClient.new(
  12. ENV.fetch('ESPMH_HOSTNAME'),
  13. ENV.fetch('ESPMH_TEST_DEVICE_ID_BASE')
  14. )
  15. end
  16. def generate_id
  17. id = @current_id
  18. @current_id += 1
  19. id
  20. end
  21. def set_auth!(username, password)
  22. @username = username
  23. @password = password
  24. end
  25. def clear_auth!
  26. @username = nil
  27. @password = nil
  28. end
  29. def reboot
  30. post('/system', '{"command":"restart"}')
  31. end
  32. def request(type, path, req_body = nil)
  33. uri = URI("http://#{@host}#{path}")
  34. Net::HTTP.start(uri.host, uri.port) do |http|
  35. req_type = Net::HTTP.const_get(type)
  36. req = req_type.new(uri)
  37. if req_body
  38. req['Content-Type'] = 'application/json'
  39. req_body = req_body.to_json if !req_body.is_a?(String)
  40. req.body = req_body
  41. end
  42. if @username && @password
  43. req.basic_auth(@username, @password)
  44. end
  45. res = http.request(req)
  46. begin
  47. res.value
  48. rescue Exception => e
  49. puts "REST Client Error: #{e}\nBody:\n#{res.body}"
  50. raise e
  51. end
  52. body = res.body
  53. if res['content-type'].downcase == 'application/json'
  54. body = JSON.parse(body)
  55. end
  56. body
  57. end
  58. end
  59. def upload_json(path, file)
  60. `curl -s "http://#{@host}#{path}" -X POST -F 'f=@#{file}'`
  61. end
  62. def patch_settings(settings)
  63. put('/settings', settings)
  64. end
  65. def get(path)
  66. request(:Get, path)
  67. end
  68. def put(path, body)
  69. request(:Put, path, body)
  70. end
  71. def post(path, body)
  72. request(:Post, path, body)
  73. end
  74. def delete(path)
  75. request(:Delete, path)
  76. end
  77. def state_path(params = {})
  78. query = if params[:blockOnQueue].nil? || params[:blockOnQueue]
  79. "?blockOnQueue=true"
  80. else
  81. ""
  82. end
  83. "/gateways/#{params[:id]}/#{params[:type]}/#{params[:group_id]}#{query}"
  84. end
  85. def delete_state(params = {})
  86. delete(state_path(params))
  87. end
  88. def get_state(params = {})
  89. get(state_path(params))
  90. end
  91. def patch_state(state, params = {})
  92. put(state_path(params), state.to_json)
  93. end
  94. def schedule_transition(_id_params, transition_params)
  95. id_params = {
  96. device_id: _id_params[:id],
  97. remote_type: _id_params[:type],
  98. group_id: _id_params[:group_id]
  99. }
  100. post("/transitions", id_params.merge(transition_params))
  101. end
  102. def transitions
  103. get('/transitions')['transitions']
  104. end
  105. end