Remove need for 'List' classes (for example, I'll be delete ApiKeyList in the future). Add json_do method similar to the one used in Python. Add TLSCertificateClient, EndpointConfigurationsClient, ReservedDomainsClient)

This commit is contained in:
David Parker
2021-09-10 10:46:50 -06:00
parent 6796ea6374
commit e3d602c403
11 changed files with 578 additions and 44 deletions
+33 -24
View File
@@ -2,8 +2,6 @@
module NgrokAPI
class Client
attr_accessor :api_keys,
:event_streams
attr_reader :api_key,
:base_url
@@ -19,10 +17,24 @@ module NgrokAPI
@_api_keys ||= NgrokAPI::Services::ApiKeysClient.new(client: self)
end
def endpoint_configurations
@_endpoint_configurations ||= NgrokAPI::Services::EndpointConfigurationsClient.new(
client: self
)
end
def event_streams
# @_event_streams ||= NgrokAPI::Services::EventStreamsClient.new(client: self)
end
def reserved_domains
@_reserved_domains ||= NgrokAPI::Services::ReservedDomainsClient.new(client: self)
end
def tls_certificates
@_tls_certificates ||= NgrokAPI::Services::TlsCertificatesClient.new(client: self)
end
def headers
{
'Authorization': "Bearer #{@api_key}",
@@ -30,6 +42,17 @@ module NgrokAPI
}
end
def headers_with_json
headers.merge({ 'Content-Type': 'application/json' })
end
def json_do(uri, req, data: nil)
resp = Net::HTTP.start(uri.hostname, uri.port, req_options(uri)) do |http|
http.request(req, data)
end
JSON.parse(resp.body) if resp.body
end
def req_options(uri)
{ use_ssl: uri.scheme == 'https' }
end
@@ -41,41 +64,27 @@ module NgrokAPI
def delete(path)
uri = URI(url(path))
response = Net::HTTP.start(uri.hostname, uri.port, req_options(uri)) do |http|
http.delete(uri, headers)
end
response.code
req = Net::HTTP::Delete.new(uri, headers)
json_do(uri, req)
end
def get(path, data: {})
data = data != {} ? "?#{URI.encode_www_form(data)}" : ""
uri = URI("#{url(path)}#{data}")
response = Net::HTTP.get(uri, headers)
JSON.parse(response)
req = Net::HTTP::Get.new(uri, headers)
json_do(uri, req)
end
def patch(path, data: {})
uri = URI(url(path))
response = Net::HTTP.start(uri.hostname, uri.port, req_options(uri)) do |http|
http.patch(
uri,
data.to_json,
headers.merge({ 'Content-Type': 'application/json' })
)
end
JSON.parse(response.body)
req = Net::HTTP::Patch.new(uri, headers_with_json)
json_do(uri, req, data: data.to_json)
end
def post(path, data: {})
uri = URI(url(path))
response = Net::HTTP.start(uri.hostname, uri.port, req_options(uri)) do |http|
http.post(
uri,
data.to_json,
headers.merge({ 'Content-Type': 'application/json' })
)
end
JSON.parse(response.body)
req = Net::HTTP::Post.new(uri, headers_with_json)
json_do(uri, req, data: data.to_json)
end
end
end
+5 -5
View File
@@ -3,10 +3,10 @@
module NgrokAPI
module Models
class ApiKey
attr_reader :client,
attr_reader :id,
:client,
:created_at,
:description,
:id,
:metadata,
:result,
:token,
@@ -35,9 +35,9 @@ module NgrokAPI
@client.delete(id: @id)
end
def update(description: '', metadata: '')
@description = description
@metadata = metadata
def update(description: nil, metadata: nil)
@description = description if description
@metadata = metadata if metadata
@client.update(id: @id, description: description, metadata: metadata)
end
end
+2 -2
View File
@@ -15,8 +15,8 @@ module NgrokAPI
@result = result
@next_page_uri = @result['next_page_uri']
@uri = @result['uri']
@keys = @result['keys'].each do |key|
NgrokAPI::Models::ApiKey.new(client: client, result: key)
@keys = @result['keys'].each do |result|
NgrokAPI::Models::ApiKey.new(client: client, result: result)
end
@iter = NgrokAPI::PagedIterator.new(client: client, page: self, list_property: 'keys')
end
@@ -0,0 +1,43 @@
# frozen_string_literal: true
module NgrokAPI
module Models
class EndpointConfiguration
attr_reader :id,
:client,
:created_at,
:description,
:metadata,
:result,
:uri
def initialize(client:, result:)
@client = client
@result = result
@created_at = @result['created_at']
@id = @result['id']
@description = @result['description']
@metadata = @result['metadata']
@uri = @result['uri']
end
# TODO: equality
def ==(other)
end
# TODO: to_s
def to_s
end
def delete
@client.delete(id: @id)
end
def update(description: nil, metadata: nil)
@description = description if description
@metadata = metadata if metadata
@client.update(id: @id, description: description, metadata: metadata)
end
end
end
end
+40
View File
@@ -0,0 +1,40 @@
# frozen_string_literal: true
module NgrokAPI
module Models
class Listable
attr_reader :client,
:klass,
:iter,
:items,
:list_property,
:next_page_uri,
:result,
:uri
def initialize(client:, result:, list_property:, klass:)
@client = client
@result = result
@list_property = list_property
@next_page_uri = @result['next_page_uri']
@uri = @result['uri']
@items = @result[list_property].each do |result|
klass.new(client: client, result: result)
end
@iter = NgrokAPI::PagedIterator.new(
client: client,
page: self,
list_property: list_property
)
end
# TODO: equality
def ==(other)
end
# TODO: to_s
def to_s
end
end
end
end
+135
View File
@@ -0,0 +1,135 @@
# frozen_string_literal: true
module NgrokAPI
module Models
class ReservedDomain
attr_reader :id,
:certificate,
:certificate_management_policy,
:certificate_management_status,
:client,
:cname_target,
:created_at,
:description,
:domain,
:http_endpoint_configuration,
:https_endpoint_configuration,
:metadata,
:region,
:result,
:uri
def initialize(client:, result:)
@client = client
@result = result
@created_at = @result['created_at']
@certificate = @result['certificate']
@certificate_management_policy = @result['certificate_management_policy']
@certificate_management_status = @result['certificate_management_status']
@cname_target = @result['cname_target']
@description = @result['description']
@domain = @result['domain']
@http_endpoint_configuration = @result['http_endpoint_configuration']
@https_endpoint_configuration = @result['https_endpoint_configuration']
@id = @result['id']
@metadata = @result['metadata']
@region = @result['region']
@uri = @result['uri']
end
# TODO: equality
def ==(other)
end
# TODO: to_s
def to_s
end
def delete
@client.delete(id: @id)
end
def update(
certificate_id: nil,
certificate_management_policy: nil,
description: '',
http_endpoint_configuration_id: nil,
https_endpoint_configuration_id: nil,
metadata: ''
)
@certificate_id = certificate_id if certificate_id
if certificate_management_policy
@certificate_management_policy = certificate_management_policy
end
@description = description if description
if http_endpoint_configuration_id
@http_endpoint_configuration_id = http_endpoint_configuration_id
end
if https_endpoint_configuration_id
@https_endpoint_configuration_id = https_endpoint_configuration_id
end
@metadata = metadata if metadata
@client.update(
id: @id,
description: description,
metadata: metadata,
http_endpoint_configuration_id: http_endpoint_configuration_id,
https_endpoint_configuration_id: https_endpoint_configuration_id,
certificate_id: certificate_id,
certificate_management_policy: certificate_management_policy
)
end
def delete_certificate(id: nil)
@certificate = nil
@client.delete_certificate(id: @id)
end
def delete_certificate_management_policy(id: nil)
@certificate_management_policy = nil
@certificate_management_status = nil
@client.delete_certificate_management_policy(id: @id)
end
def delete_http_endpoint_config(id: nil)
@http_endpoint_configuration = nil
@client.delete_http_endpoint_config(id: @id)
end
def delete_https_endpoint_config(id: nil)
@https_endpoint_configuration = nil
@client.delete_https_endpoint_config(id: @id)
end
def restore_certificate
@certificate = @result['certificate']
@client.update(id: @id, certificate_id: @result['certificate']['id'])
end
def restore_certificate_management_policy
@certificate_management_policy = @result['certificate_management_policy']
@certificate_management_status = @result['certificate_management_status']
@client.update(
id: @id,
certificate_management_policy: @result['certificate_management_policy']
)
end
def restore_http_endpoint_config
@http_endpoint_configuration = @result['http_endpoint_configuration']
@client.update(
id: @id,
http_endpoint_configuration_id: @result['http_endpoint_configuration']['id']
)
end
def restore_https_endpoint_config
@https_endpoint_configuration = @result['https_endpoint_configuration']
@client.update(
id: @id,
https_endpoint_configuration_id: @result['https_endpoint_configuration']['id']
)
end
end
end
end
+43
View File
@@ -0,0 +1,43 @@
# frozen_string_literal: true
module NgrokAPI
module Models
class TlsCertificate
attr_reader :id,
:client,
:created_at,
:description,
:metadata,
:result,
:uri
def initialize(client:, result:)
@client = client
@result = result
@created_at = @result['created_at']
@id = @result['id']
@description = @result['description']
@metadata = @result['metadata']
@uri = @result['uri']
end
# TODO: equality
def ==(other)
end
# TODO: to_s
def to_s
end
def delete
@client.delete(id: @id)
end
def update(description: nil, metadata: nil)
@description = description if description
@metadata = metadata if metadata
@client.update(id: @id, description: description, metadata: metadata)
end
end
end
end
+17 -13
View File
@@ -3,6 +3,7 @@
module NgrokAPI
module Services
class ApiKeysClient
LIST_PROPERTY = 'keys'.freeze
PATH = '/api_keys'.freeze
attr_reader :client
@@ -11,11 +12,10 @@ module NgrokAPI
@client = client
end
def create(description: '', metadata: '')
data = {
description: description,
metadata: metadata,
}
def create(description: nil, metadata: nil)
data = {}
data[:description] = description if description
data[:metadata] = metadata if metadata
result = @client.post(PATH, data: data)
NgrokAPI::Models::ApiKey.new(client: self, result: result)
end
@@ -32,20 +32,24 @@ module NgrokAPI
def list(before_id: nil, limit: nil, url: nil)
result = if url.nil?
data = {}
data[:before_id] = before_id unless before_id.nil?
data[:limit] = limit unless limit.nil?
data[:before_id] = before_id if before_id
data[:limit] = limit if limit
@client.get(PATH, data: data)
else
@client.get(url)
end
NgrokAPI::Models::ApiKeyList.new(client: self, result: result)
NgrokAPI::Models::Listable.new(
client: self,
result: result,
list_property: LIST_PROPERTY,
klass: NgrokAPI::Models::ApiKey
)
end
def update(id: nil, description: '', metadata: '')
data = {
description: description,
metadata: metadata,
}
def update(id: nil, description: nil, metadata: nil)
data = {}
data[:description] = description if description
data[:metadata] = metadata if metadata
result = @client.patch("#{PATH}/#{id}", data: data)
NgrokAPI::Models::ApiKey.new(client: self, result: result)
end
@@ -0,0 +1,82 @@
# frozen_string_literal: true
module NgrokAPI
module Services
class EndpointConfigurationsClient
LIST_PROPERTY = 'endpoint_configurations'.freeze
PATH = '/endpoint_configurations'.freeze
attr_reader :client
def initialize(client:)
@client = client
end
def create(
description: '',
metadata: '',
type: ''
)
data = {
type: type,
description: description,
metadata: metadata,
}
result = @client.post(PATH, data: data)
NgrokAPI::Models::EndpointConfiguration.new(client: self, result: result)
end
def delete(id: nil)
@client.delete("#{PATH}/#{id}")
end
def get(id: nil)
result = @client.get("#{PATH}/#{id}")
NgrokAPI::Models::EndpointConfiguration.new(client: self, result: result)
end
def list(before_id: nil, limit: nil, url: nil)
result = if url.nil?
data = {}
data[:before_id] = before_id if before_id
data[:limit] = limit if limit
@client.get(PATH, data: data)
else
@client.get(url)
end
NgrokAPI::Models::Listable.new(
client: self,
result: result,
list_property: LIST_PROPERTY,
klass: NgrokAPI::Models::EndpointConfiguration
)
end
def update(
id: nil,
certificate_id: nil,
certificate_management_policy: nil,
description: nil,
http_endpoint_configuration_id: nil,
https_endpoint_configuration_id: nil,
metadata: nil
)
data = {}
data[:certificate_id] = certificate_id if certificate_id
if certificate_management_policy
data[:certificate_management_policy] = certificate_management_policy
end
data[:description] = description if description
if http_endpoint_configuration_id
data[:http_endpoint_configuration_id] = http_endpoint_configuration_id
end
if https_endpoint_configuration_id
data[:https_endpoint_configuration_id] = https_endpoint_configuration_id
end
data[:metadata] = metadata if metadata
result = @client.patch("#{PATH}/#{id}", data: data)
NgrokAPI::Models::EndpointConfiguration.new(client: self, result: result)
end
end
end
end
@@ -0,0 +1,108 @@
# frozen_string_literal: true
module NgrokAPI
module Services
class ReservedDomainsClient
LIST_PROPERTY = 'reserved_domains'.freeze
PATH = '/reserved_domains'.freeze
attr_reader :client
def initialize(client:)
@client = client
end
def create(
name: '',
region: '',
description: '',
metadata: '',
http_endpoint_configuration_id: '',
https_endpoint_configuration_id: '',
certificate_id: nil,
certificate_management_policy: nil
)
data = {
name: name,
region: region,
description: description,
metadata: metadata,
http_endpoint_configuration_id: http_endpoint_configuration_id,
https_endpoint_configuration_id: https_endpoint_configuration_id,
certificate_id: certificate_id,
certificate_management_policy: certificate_management_policy,
}
result = @client.post(PATH, data: data)
NgrokAPI::Models::ReservedDomain.new(client: self, result: result)
end
def delete(id: nil)
@client.delete("#{PATH}/#{id}")
end
def get(id: nil)
result = @client.get("#{PATH}/#{id}")
NgrokAPI::Models::ReservedDomain.new(client: self, result: result)
end
def list(before_id: nil, limit: nil, url: nil)
result = if url.nil?
data = {}
data[:before_id] = before_id if before_id
data[:limit] = limit if limit
@client.get(PATH, data: data)
else
@client.get(url)
end
NgrokAPI::Models::Listable.new(
client: self,
result: result,
list_property: LIST_PROPERTY,
klass: NgrokAPI::Models::ReservedDomain
)
end
def update(
id: nil,
certificate_id: nil,
certificate_management_policy: nil,
description: nil,
http_endpoint_configuration_id: nil,
https_endpoint_configuration_id: nil,
metadata: nil
)
data = {}
data[:certificate_id] = certificate_id if certificate_id
if certificate_management_policy
data[:certificate_management_policy] = certificate_management_policy
end
data[:description] = description if description
if http_endpoint_configuration_id
data[:http_endpoint_configuration_id] = http_endpoint_configuration_id
end
if https_endpoint_configuration_id
data[:https_endpoint_configuration_id] = https_endpoint_configuration_id
end
data[:metadata] = metadata if metadata
result = @client.patch("#{PATH}/#{id}", data: data)
NgrokAPI::Models::ReservedDomain.new(client: self, result: result)
end
def delete_certificate_management_policy(id: nil)
@client.delete("#{PATH}/#{id}/certificate_management_policy")
end
def delete_certificate(id: nil)
@client.delete("#{PATH}/#{id}/certificate")
end
def delete_http_endpoint_config(id: nil)
@client.delete("#{PATH}/#{id}/http_endpoint_configuration")
end
def delete_https_endpoint_config(id: nil)
@client.delete("#{PATH}/#{id}/https_endpoint_configuration")
end
end
end
end
@@ -0,0 +1,70 @@
# frozen_string_literal: true
module NgrokAPI
module Services
class TlsCertificatesClient
LIST_PROPERTY = 'tls_certificates'.freeze
PATH = '/tls_certificates'.freeze
attr_reader :client
def initialize(client:)
@client = client
end
def create(
certificate_pem: '',
description: '',
metadata: '',
private_key_pem: ''
)
data = {
certificate_pem: certificate_pem,
description: description,
metadata: metadata,
private_key_pem: private_key_pem,
}
result = @client.post(PATH, data: data)
NgrokAPI::Models::TlsCertificate.new(client: self, result: result)
end
def delete(id: nil)
@client.delete("#{PATH}/#{id}")
end
def get(id: nil)
result = @client.get("#{PATH}/#{id}")
NgrokAPI::Models::TlsCertificate.new(client: self, result: result)
end
def list(before_id: nil, limit: nil, url: nil)
result = if url.nil?
data = {}
data[:before_id] = before_id if before_id
data[:limit] = limit if limit
@client.get(PATH, data: data)
else
@client.get(url)
end
NgrokAPI::Models::Listable.new(
client: self,
result: result,
list_property: LIST_PROPERTY,
klass: NgrokAPI::Models::TlsCertificate
)
end
def update(
id: nil,
description: nil,
metadata: nil
)
data = {}
data[:description] = description if description
data[:metadata] = metadata if metadata
result = @client.patch("#{PATH}/#{id}", data: data)
NgrokAPI::Models::TlsCertificate.new(client: self, result: result)
end
end
end
end