Skip to content

Commit

Permalink
cover all endpoints from documentations
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegPhenomenon committed Oct 11, 2023
1 parent 16a95d1 commit f3a0340
Show file tree
Hide file tree
Showing 12 changed files with 277 additions and 37 deletions.
21 changes: 5 additions & 16 deletions app/services/bsa/application_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

module Bsa
module ApplicationService
include Errors
include Core::Errors
include Core::Settings

OK = '200'

Expand All @@ -14,9 +15,9 @@ def connect(url:)
http
end

def headers
def headers(content_type: 'x-www-form-urlencoded')
{
'Content-Type' => 'application/x-www-form-urlencoded',
'Content-Type' => "application/#{content_type}",
}
end

Expand All @@ -26,18 +27,6 @@ def token_format(token)
}
end

def bsa
'BSA'
end

def api_key
ENV['bsa_api_key']
end

def base_url
ENV['bsa_base_url']
end

def struct_response(response)
parsed_data = parse_response(response)
result = response.code == OK
Expand All @@ -61,7 +50,7 @@ def expire_token_at(token)
end

def redis
@redis ||= Redis.new(host: "redis", port: 6379, db: 7)
@redis ||= Redis.new(host: redist_host, port: redis_port, db: redis_db)
end
end
end
23 changes: 7 additions & 16 deletions app/services/bsa/block_order_list_service.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Bsa
class BlockOrderListService
include Bsa::ApplicationService
include ApplicationService
include Core::TokenHelper

attr_reader :sort_by, :order, :offset, :limit, :q

Expand All @@ -25,27 +26,17 @@ def call

private

def token
response = Bsa::AuthService.call

if response.result?
response.body.id_token
else
raise Bsa::AuthError, "#{response.error.message}: #{response.error.description}"
end
end

def endpoint
"/bsa/api/blockrsporder?#{query_string}"
end

def query_string
params = {
'sortBy' => @sort_by,
'order' => @order,
'offset' => @offset,
'limit' => @limit
}.merge(@q).compact
'sortBy' => sort_by,
'order' => order,
'offset' => offset,
'limit' => limit
}.merge(q).compact

URI.encode_www_form(params)
end
Expand Down
29 changes: 29 additions & 0 deletions app/services/bsa/block_order_status_setting_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Bsa
class BlockOrderStatusSettingService
include ApplicationService
include Core::TokenHelper

attr_reader :payload

def self.call(payload: [])
new(payload: payload).call
end

def initialize(payload:)
@payload = payload
end

def call
http = connect(url: base_url)
response = http.post(endpoint, payload.to_json, headers(content_type: 'json').merge(token_format(token)))

struct_response(response)
end

private

def endpoint
'/bsa/api/blockrsporder/status'
end
end
end
41 changes: 41 additions & 0 deletions app/services/bsa/block_order_view_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module Bsa
class BlockOrderViewService
include ApplicationService
include Core::TokenHelper

attr_reader :block_suborder_id, :offset, :limit

def self.call(block_suborder_id: nil, offset: nil, limit: nil)
new(block_suborder_id: block_suborder_id, offset: offset, limit: limit).call
end

def initialize(block_suborder_id:, offset:, limit:)
@block_suborder_id = block_suborder_id
@offset = offset
@limit = limit
end

def call
http = connect(url: base_url)
response = http.get(endpoint, headers.merge(token_format(token)))

struct_response(response)
end

private

def endpoint
"/bsa/api/blockrsporder/labels?#{query_string}"
end

def query_string
params = {
'blocksuborderid' => block_suborder_id,
'offset' => offset,
'limit' => limit
}.compact

URI.encode_www_form(params)
end
end
end
7 changes: 7 additions & 0 deletions app/services/bsa/core/errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Bsa
module Core
module Errors
class AuthError < StandardError; end
end
end
end
29 changes: 29 additions & 0 deletions app/services/bsa/core/settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Bsa
module Core
module Settings
def bsa
'BSA'
end

def api_key
ENV['bsa_api_key']
end

def base_url
ENV['bsa_base_url']
end

def redist_host
ENV['bsa_redis_host'] || 'redis'
end

def redis_port
ENV['bsa_redis_port'] || 6379
end

def redis_db
ENV['bsa_redis_db'] || 7
end
end
end
end
15 changes: 15 additions & 0 deletions app/services/bsa/core/token_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Bsa
module Core
module TokenHelper
def token
response = Bsa::AuthService.call

if response.result?
response.body.id_token
else
raise Bsa::AuthError, "#{response.error.message}: #{response.error.description}"
end
end
end
end
end
35 changes: 35 additions & 0 deletions app/services/bsa/download_non_blocked_name_list_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module Bsa
class DownloadNonBlockedNameListService
include ApplicationService
include Core::TokenHelper

attr_reader :suborder_id, :filename

def self.call(suborder_id:, filename: Time.now.strftime("%Y-%m-%d_%H-%M-%S"))
new(suborder_id: suborder_id, filename: filename).call
end

def initialize(suborder_id:, filename:)
@suborder_id = suborder_id
@filename = filename
end

def call
http = connect(url: base_url)
response = http.get(endpoint, headers.merge(token_format(token)))

File.open("#{filename}.csv", 'wb') do |file|
file.write(response.body)
end

# TODO: finish with response
# struct_response(response)
end

private

def endpoint
"/bsa/api/blockrsporder/#{suborder_id}/nonblockednames"
end
end
end
5 changes: 0 additions & 5 deletions app/services/bsa/errors.rb

This file was deleted.

49 changes: 49 additions & 0 deletions app/services/bsa/non_blocked_name_action_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module Bsa
class NonBlockedNameActionService
include ApplicationService
include Core::TokenHelper

attr_reader :action, :suborder_id, :payload

# Actions:
# - add
# - remove
# - remove_all

def self.call(action:, suborder_id:, payload:)
new(action: action, suborder_id: suborder_id, payload: payload).call
end

def initialize(action:, suborder_id:, payload:)
@action = action
@suborder_id = suborder_id
@payload = payload
end

def call
http = connect(url: base_url)
response = http.post(endpoint, payload.to_json, headers(content_type: 'json').merge(token_format(token)))

struct_response(response)
end

private

def endpoint
if action == 'remove_all'
"/bsa/api/blockrsporder/#{suborder_id}/nonblockednames?action=remove"
else
"/bsa/api/blockrsporder/nonblockednames?#{query_string}"
end
end

def query_string
params = {
'action' => action,
'suborderid' => suborder_id,
}.compact

URI.encode_www_form(params)
end
end
end
60 changes: 60 additions & 0 deletions app/services/bsa/upload_unavailable_domain_report_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
module Bsa
class UploadUnavailableDomainReportService
include ApplicationService
include Core::TokenHelper

attr_reader :file_path

def self.call(file_path:)
new(file_path: file_path).call
end

def initialize(file_path:)
@file_path = file_path
end

def call
http = connect(url: base_url)
uri = URI("#{base_url}")

checksum = calculate_checksum(@file_path)

request = Net::HTTP::Post.new(uri)
request['Authorization'] = "Bearer #{token}"
request['Content-Type'] = 'multipart/form-data'

form_data = [
['Zone', { "checkSum" => checksum }.to_json ],
['File', File.open(@file_path)]
]

request.set_form form_data, 'multipart/form-data'

response = http.request(request)

struct_response(response)
end

private

def endpoint
"/bsa/api/zonefile"
end

def form_data_headers
{
'Content-Type' => 'multipart/form-data',
}
end

def calculate_checksum(file_path)
digest = OpenSSL::Digest::SHA256.new
File.open(file_path, 'rb') do |f|
while chunk = f.read(4096)
digest.update(chunk)
end
end
digest.hexdigest
end
end
end
Empty file added your_filename.csv
Empty file.

0 comments on commit f3a0340

Please sign in to comment.