Module: TDAuthService

Defined in:
lib/fastlane/plugin/appcircle_testing_distribution/helper/TDAuthService.rb

Class Method Summary collapse

Class Method Details

.get_ac_token(pat:, sub_organization_id: nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fastlane/plugin/appcircle_testing_distribution/helper/TDAuthService.rb', line 16

def self.get_ac_token(pat:, sub_organization_id: nil)
  endpoint_url = 'https://auth.appcircle.io/auth/v2/token'
  uri = URI(endpoint_url)

  # Create HTTP request
  request = Net::HTTP::Post.new(uri)
  request.content_type = 'application/x-www-form-urlencoded'
  request['Accept'] = 'application/json'
  
  # Encode parameters
  params = { pat: pat }
  params[:subOrganizationId] = sub_organization_id if sub_organization_id
  request.body = URI.encode_www_form(params)

  # Make the HTTP request
  response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
    http.request(request)
  end

  # Check response
  if response.is_a?(Net::HTTPSuccess)
    response_data = JSON.parse(response.body)

    user = UserResponse.new(
      accessToken: response_data['access_token']
    )

    return user
  else
    raise "Error: (#{response.code} #{response.message})."
  end
end

.get_organization_id(access_token:, name:) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fastlane/plugin/appcircle_testing_distribution/helper/TDAuthService.rb', line 49

def self.get_organization_id(access_token:, name:)
  endpoint_url = 'https://api.appcircle.io/identity/v1/organizations'
  uri = URI(endpoint_url)

  # Create HTTP request
  request = Net::HTTP::Get.new(uri)
  request['Authorization'] = "Bearer #{access_token}"
  request['Accept'] = 'application/json'

  # Make the HTTP request
  response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
    http.request(request)
  end

  # Check response
  if response.is_a?(Net::HTTPSuccess)
    response_data = JSON.parse(response.body)
    organizations = response_data['data']
    organization = organizations.find { |org| org['name'] == name }
    
    raise "Organization with name '#{name}' not found" unless organization
    return organization['id']

  else
    raise "Error: (#{response.code} #{response.message})"
  end
end