# typed: false # frozen_string_literal: true class YettoService cattr_reader :yetto_client, instance_accessor: false do HTTPX.plugin(:persistent).with_headers({ "Content-Type" => "application/json", "Accept" => "application/json", "User-Agent" => "PlugApp/#{PlugApp::Application::GIT_SHA}", }) end PROTOCOL = Rails.env.development? ? "http" : "https" YETTO_API_VERSION_TLD = "#{PROTOCOL}://#{YETTO_API_TLD}/#{YETTO_API_VERSION}" class << self def get_plug_installation(organization_id, inbox_id, plug_installation_id) yetto_client.get("#{YETTO_API_VERSION_TLD}/organizations/#{organization_id}/inboxes/#{inbox_id}/plug_installations/#{plug_installation_id}") end def update_installation(organization_id, inbox_id, plug_installation_id, params) plug_installation = {} plug_installation[:settings] = params[:plug_installation].fetch(:settings, {}) plug_installation[:credentials] = params[:plug_installation].fetch(:credentials, {}) body = { plug_installation: plug_installation, } yetto_client.patch("#{YETTO_API_VERSION_TLD}/organizations/#{organization_id}/inboxes/#{inbox_id}/plug_installations/#{plug_installation_id}", json: body) end def create_switch(organization_id, inbox_id, plug_id, params) inbox_id = params.fetch(:inbox).fetch(:id) creator_id = params.fetch(:plug).fetch(:id) payload = { name: "After install", creator: { id: creator_id }, } yetto_client.post("#{YETTO_API_VERSION_TLD}/organizations/#{organization_id}/inboxes/#{inbox_id}/switches", json: payload) end def create_message(organization_id, inbox_id, params) payload = params[:payload] yetto_client.post("#{YETTO_API_VERSION_TLD}/organizations/#{organization_id}/inboxes/#{inbox_id}/messages", json: payload) end def parse_body(response) JSON.parse(response.body.read) end def unavailable?(response) return true unless (200..299).cover?(response.status) response.body.blank? || response.body == "{}" end def available?(response) !unavailable?(response) end end end