Class: Logstream::CloudAPIV2
- Inherits:
-
Object
- Object
- Logstream::CloudAPIV2
- Defined in:
- lib/logstream/cloudapi_v2.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- CLOUDAPI_ENDPOINT =
'https://cloud.acquia.com/api'
Instance Attribute Summary collapse
-
#client_id ⇒ Object
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
Returns the value of attribute client_secret.
-
#endpoint ⇒ Object
Returns the value of attribute endpoint.
Instance Method Summary collapse
- #get(path) ⇒ Object
- #get_application_environments(application_uuid) ⇒ Object
- #get_envirornment_logstream(environment_uuid) ⇒ Object
- #get_token ⇒ Object
-
#initialize(client_id, client_secret) ⇒ CloudAPIV2
constructor
A new instance of CloudAPIV2.
Constructor Details
#initialize(client_id, client_secret) ⇒ CloudAPIV2
Returns a new instance of CloudAPIV2.
11 12 13 14 |
# File 'lib/logstream/cloudapi_v2.rb', line 11 def initialize(client_id, client_secret) @client_id = client_id @client_secret = client_secret end |
Instance Attribute Details
#client_id ⇒ Object
Returns the value of attribute client_id.
8 9 10 |
# File 'lib/logstream/cloudapi_v2.rb', line 8 def client_id @client_id end |
#client_secret ⇒ Object
Returns the value of attribute client_secret.
8 9 10 |
# File 'lib/logstream/cloudapi_v2.rb', line 8 def client_secret @client_secret end |
#endpoint ⇒ Object
Returns the value of attribute endpoint.
8 9 10 |
# File 'lib/logstream/cloudapi_v2.rb', line 8 def endpoint @endpoint end |
Instance Method Details
#get(path) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/logstream/cloudapi_v2.rb', line 16 def get(path) bearer_token = get_token uri = URI.parse("#{CLOUDAPI_ENDPOINT}#{path}") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true request = Net::HTTP::Get.new(uri.request_uri) request['Authorization'] = "Bearer #{bearer_token}" response = http.request(request) parsed = JSON.parse(response.body) rescue nil case response.code.to_i when 200 raise Error, "Unexpected reply #{response.body}" unless parsed parsed else raise Error, "HTTP #{response.code}: #{response.body}" end end |
#get_application_environments(application_uuid) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/logstream/cloudapi_v2.rb', line 34 def get_application_environments(application_uuid) response = get("/applications/#{application_uuid}/environments") #, { :query => { "filter" => "name%3D#{env}"}}) raise Error, "No Environments found." if response['total'] == 0 raise Error, "Unexpected reply #{response}" unless response['_embedded']['items'] response['_embedded']['items'] end |
#get_envirornment_logstream(environment_uuid) ⇒ Object
41 42 43 44 45 |
# File 'lib/logstream/cloudapi_v2.rb', line 41 def get_envirornment_logstream(environment_uuid) response = get("/environments/#{environment_uuid}/logstream") raise Error, "Unexpected reply #{response}" unless response['logstream'] response['logstream'] end |
#get_token ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/logstream/cloudapi_v2.rb', line 47 def get_token uri = URI.parse("https://accounts.acquia.com/api/auth/oauth/token") response = Net::HTTP.post_form(uri, 'client_id' => @client_id, 'client_secret' => @client_secret, 'grant_type' => 'client_credentials') parsed = JSON.parse(response.body) rescue nil case response.code.to_i when 200 raise Error, "Unexpected reply #{response.body}" unless parsed["access_token"] parsed["access_token"] else raise Error, "HTTP #{response.code}: #{response.body}" end end |