Class: FluidCLI::API

Inherits:
Object
  • Object
show all
Defined in:
lib/fluid_cli/api.rb

Defined Under Namespace

Classes: APIRequestClientError, APIRequestError, APIRequestForbiddenError, APIRequestNotFoundError, APIRequestRetriableError, APIRequestServerError, APIRequestThrottledError, APIRequestTimeoutError, APIRequestUnauthorizedError, APIRequestUnexpectedError

Instance Method Summary collapse

Constructor Details

#initialize(ctx) ⇒ API

Returns a new instance of API.



26
27
28
# File 'lib/fluid_cli/api.rb', line 26

def initialize(ctx)
  @ctx = ctx
end

Instance Method Details

#delete(path:, **args, &block) ⇒ Object



42
43
44
# File 'lib/fluid_cli/api.rb', line 42

def delete(path:, **args, &block)
  rest_request(method: "DELETE", path: path, **args, &block)
end

#get(path:, **args, &block) ⇒ Object



30
31
32
# File 'lib/fluid_cli/api.rb', line 30

def get(path:, **args, &block)
  rest_request(method: "GET", path: path, **args, &block)
end

#get_company_or_abortObject



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/fluid_cli/api.rb', line 69

def get_company_or_abort
  env_store = FluidCLI::Environment.company
  return env_store unless env_store.nil?

  if FluidCLI::DB.exists?(:company)
    FluidCLI::DB.get(:company)
  else
    resp = FluidCLI::IdentityAuth.new(ctx: @ctx).attempt_reauthenticate
    @ctx.abort(
      "No company found. Please login again"
    ) unless resp 
  end
end

#multipart_post(path:, **args, &block) ⇒ Object



50
51
52
# File 'lib/fluid_cli/api.rb', line 50

def multipart_post(path:, **args, &block)
  rest_request(method: "MULTIPART_POST", path: path, **args, &block)
end

#multipart_put(path:, **args, &block) ⇒ Object



46
47
48
# File 'lib/fluid_cli/api.rb', line 46

def multipart_put(path:, **args, &block)
  rest_request(method: "MULTIPART_PUT", path: path, **args, &block)
end

#post(path:, **args, &block) ⇒ Object



38
39
40
# File 'lib/fluid_cli/api.rb', line 38

def post(path:, **args, &block)
  rest_request(method: "POST", path: path, **args, &block)
end

#put(path:, **args, &block) ⇒ Object



34
35
36
# File 'lib/fluid_cli/api.rb', line 34

def put(path:, **args, &block)
  rest_request(method: "PUT", path: path, **args, &block)
end

#rest_request(path:, query: nil, body: nil, method: "GET", token: nil, host: nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fluid_cli/api.rb', line 54

def rest_request(path:, query: nil, body: nil, method: "GET", token: nil, host: nil)
  CLI::Kit::Util.begin do
    url = URI::HTTPS.build(
      host: host || FluidCLI::API_HOST,
      path: "/api/#{path}",
      query: query,
    )
    resp = request(url: url.to_s, body: body, headers: auth_headers, method: method)
    resp

  end.retry_after(API::APIRequestUnauthorizedError) do
    FluidCLI::IdentityAuth.new(ctx: @ctx).reauthenticate unless path == "/me"
  end
end