Class: Shai::ApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/shai/api_client.rb

Instance Method Summary collapse

Constructor Details

#initialize ⇒ ApiClient

Returns a new instance of ApiClient.



9
10
11
# File 'lib/shai/api_client.rb', line 9

def initialize
  @connection = build_connection
end

Instance Method Details

#create_configuration(name:, description: nil, visibility: "private") ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/shai/api_client.rb', line 54

def create_configuration(name:, description: nil, visibility: "private")
  post("/api/v1/configurations", {
    configuration: {
      name: name,
      description: description,
      visibility: visibility
    }
  })
end

#delete_configuration(identifier) ⇒ Object



70
71
72
# File 'lib/shai/api_client.rb', line 70

def delete_configuration(identifier)
  delete("/api/v1/configurations/#{encode_identifier(identifier)}")
end

#device_authorize(client_name: nil) ⇒ Object

Device Flow Authentication (RFC 8628)



24
25
26
27
28
29
# File 'lib/shai/api_client.rb', line 24

def device_authorize(client_name: nil)
  client_name ||= default_client_name
  post_without_auth("/api/v1/device/authorize", {
    client_name: client_name
  })
end

#device_token(device_code:) ⇒ Object



31
32
33
34
35
36
# File 'lib/shai/api_client.rb', line 31

def device_token(device_code:)
  response = @connection.post("/api/v1/device/token") do |req|
    req.body = {device_code: device_code}
  end
  handle_device_token_response(response)
end

#encode_identifier(identifier) ⇒ Object

Encode identifier for URL (handles owner/slug format)



87
88
89
# File 'lib/shai/api_client.rb', line 87

def encode_identifier(identifier)
  URI.encode_www_form_component(identifier)
end

#get_configuration(identifier) ⇒ Object



50
51
52
# File 'lib/shai/api_client.rb', line 50

def get_configuration(identifier)
  get("/api/v1/configurations/#{encode_identifier(identifier)}")
end

#get_tree(identifier) ⇒ Object



74
75
76
# File 'lib/shai/api_client.rb', line 74

def get_tree(identifier)
  get("/api/v1/configurations/#{encode_identifier(identifier)}/tree")
end

#list_configurations ⇒ Object

Configurations



39
40
41
# File 'lib/shai/api_client.rb', line 39

def list_configurations
  get("/api/v1/configurations")
end

#login(identifier:, password:, client_name: nil) ⇒ Object

Authentication



14
15
16
17
18
19
20
21
# File 'lib/shai/api_client.rb', line 14

def (identifier:, password:, client_name: nil)
  client_name ||= default_client_name
  post("/api/v1/cli/session", {
    identifier: identifier,
    password: password,
    client_name: client_name
  })
end

#record_install(identifier) ⇒ Object



82
83
84
# File 'lib/shai/api_client.rb', line 82

def record_install(identifier)
  post_without_auth("/api/v1/configurations/#{encode_identifier(identifier)}/install", {})
end

#search_configurations(query: nil, tags: []) ⇒ Object



43
44
45
46
47
48
# File 'lib/shai/api_client.rb', line 43

def search_configurations(query: nil, tags: [])
  params = {}
  params[:q] = query if query
  params["tags[]"] = tags if tags.any?
  get("/api/v1/configurations/search", params)
end

#update_configuration(identifier, **attributes) ⇒ Object



64
65
66
67
68
# File 'lib/shai/api_client.rb', line 64

def update_configuration(identifier, **attributes)
  put("/api/v1/configurations/#{encode_identifier(identifier)}", {
    configuration: attributes
  })
end

#update_tree(identifier, tree) ⇒ Object



78
79
80
# File 'lib/shai/api_client.rb', line 78

def update_tree(identifier, tree)
  put("/api/v1/configurations/#{encode_identifier(identifier)}/tree", {tree: tree})
end