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

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

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.



14
15
16
17
# File 'lib/strata/cli/api/client.rb', line 14

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

Instance Method Details

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



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

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/strata/cli/api/client.rb', line 68

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

#get_deployment(project_id, branch_id, deployment_id) ⇒ Object



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

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



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

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



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

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