Class: Strata::CLI::API::Client

Inherits:
Object
  • Object
show all
Includes:
ConnectionErrorHandler, ResponseErrorHandler
Defined in:
lib/strata/cli/api/client.rb

Constant Summary

Constants included from ResponseErrorHandler

ResponseErrorHandler::DETAIL_LIMIT

Instance Method Summary collapse

Methods included from ConnectionErrorHandler

#with_connection_error_handling

Constructor Details

#initialize(server_url, api_key) ⇒ Client

Returns a new instance of Client.



16
17
18
19
# File 'lib/strata/cli/api/client.rb', line 16

def initialize(server_url, api_key)
  @server_url = server_url.chomp("/")
  @api_key = api_key
end

Instance Method Details

#branches(project_id) ⇒ Object



70
71
72
73
74
75
# File 'lib/strata/cli/api/client.rb', line 70

def branches(project_id)
  response = with_connection_error_handling(@server_url) { connection.get(branches_url(project_id)) }
  return [] if response.status == 404

  handle_response(response)
end

#create_deployment(project_id, branch_id, archive_path, metadata) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/strata/cli/api/client.rb', line 32

def create_deployment(project_id, branch_id, archive_path, )
  url = deployment_url(project_id, branch_id)
  file = Faraday::Multipart::FilePart.new(
    archive_path,
    "application/gzip"
  )

  response = with_connection_error_handling(@server_url) do
    connection.post(url) do |req|
      req.body = {
        "deployment[archive]" => file,
        "deployment[commit]" => [:commit],
        "deployment[commit_message]" => [:commit_message],
        "deployment[committer_name]" => [:committer_name],
        "deployment[committer_email]" => [:committer_email],
        "deployment[file_modifications]" => [:file_modifications].to_json
      }
    end
  end

  handle_response(response)
end

#create_project(name, uid, description: nil, git: nil, production_branch: "main") ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/strata/cli/api/client.rb', line 85

def create_project(name, uid, description: nil, git: nil, production_branch: "main")
  url = "#{@server_url}/api/v1/projects"
  response = with_connection_error_handling(@server_url) do
    connection.post(url) do |req|
      req.headers["Content-Type"] = "application/json"
      req.body = {
        project: {
          name: name,
          uid: uid,
          description: description,
          git: git
        },
        production_branch: production_branch
      }.to_json
    end
  end

  handle_response(response)
end

#delete_branch(project_id, branch_id) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/strata/cli/api/client.rb', line 77

def delete_branch(project_id, branch_id)
  response = with_connection_error_handling(@server_url) { connection.delete(branch_url(project_id, branch_id)) }
  return false if response.status == 404

  handle_response(response)
  true
end

#get_deployment(project_id, branch_id, deployment_id) ⇒ Object



55
56
57
58
59
# File 'lib/strata/cli/api/client.rb', line 55

def get_deployment(project_id, branch_id, deployment_id)
  url = "#{deployment_url(project_id, branch_id)}/#{deployment_id}"
  response = with_connection_error_handling(@server_url) { connection.get(url) }
  handle_response(response)
end

#last_successful_deployment(project_id, branch_id) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/strata/cli/api/client.rb', line 21

def last_successful_deployment(project_id, branch_id)
  url = deployment_url(project_id, branch_id)
  response = with_connection_error_handling(@server_url) { connection.get(url) }
  deployments = handle_response(response)

  return nil unless deployments.is_a?(Array)

  # Find the first successful deployment
  deployments.find { |d| d["status"] == "succeeded" }
end

#latest_deployment(project_id, branch_id) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/strata/cli/api/client.rb', line 61

def latest_deployment(project_id, branch_id)
  url = deployment_url(project_id, branch_id)
  response = with_connection_error_handling(@server_url) { connection.get(url) }
  deployments = handle_response(response)
  return nil unless deployments.is_a?(Array) && deployments.any?

  deployments.first
end