mirror of
https://github.com/internetee/registry.git
synced 2025-06-05 20:27:30 +02:00
REPP: Move domain transfer info to rails API format
This commit is contained in:
parent
6c655e1179
commit
d1ab61f424
4 changed files with 68 additions and 52 deletions
|
@ -1,50 +0,0 @@
|
|||
module Repp
|
||||
class DomainV1 < Grape::API
|
||||
version 'v1', using: :path
|
||||
|
||||
resource :domains do
|
||||
desc 'Return list of domains'
|
||||
params do
|
||||
optional :limit, type: Integer, values: (1..200).to_a, desc: 'How many domains to show'
|
||||
optional :offset, type: Integer, desc: 'Domain number to start at'
|
||||
optional :details, type: String, values: %w(true false), desc: 'Whether to include details'
|
||||
end
|
||||
|
||||
get '/' do
|
||||
limit = params[:limit] || 200
|
||||
offset = params[:offset] || 0
|
||||
|
||||
if params[:details] == 'true'
|
||||
domains = current_user.registrar.domains.limit(limit).offset(offset)
|
||||
else
|
||||
domains = current_user.registrar.domains.limit(limit).offset(offset).pluck(:name)
|
||||
end
|
||||
|
||||
@response = {
|
||||
domains: domains,
|
||||
total_number_of_records: current_user.registrar.domains.count
|
||||
}
|
||||
end
|
||||
|
||||
# example: curl -u registrar1:password localhost:3000/repp/v1/domains/1/transfer_info -H "Auth-Code: authinfopw1"
|
||||
get '/:id/transfer_info', requirements: { id: /.*/ } do
|
||||
ident = params[:id]
|
||||
domain = ident.match?(/\A[0-9]+\z/) ? Domain.find_by(id: ident) : Domain.find_by_idn(ident)
|
||||
|
||||
error! I18n.t('errors.messages.epp_domain_not_found'), 404 unless domain
|
||||
error! I18n.t('errors.messages.epp_authorization_error'), 401 unless domain.transfer_code.eql? request.headers['Auth-Code']
|
||||
|
||||
contact_repp_json = proc{|contact|
|
||||
contact.as_json.slice("code", "name", "ident", "ident_type", "ident_country_code", "phone", "email", "street", "city", "zip","country_code", "statuses")
|
||||
}
|
||||
|
||||
@response = {
|
||||
domain: domain.name,
|
||||
registrant: contact_repp_json.call(domain.registrant),
|
||||
admin_contacts: domain.admin_contacts.map{|e| contact_repp_json.call(e)},
|
||||
tech_contacts: domain.tech_contacts.map{|e| contact_repp_json.call(e)}
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -50,10 +50,10 @@ module Repp
|
|||
end
|
||||
end
|
||||
|
||||
def render_epp_error
|
||||
def render_epp_error(status = :bad_request)
|
||||
render(
|
||||
json: { code: @epp_errors[0][:code], message: @epp_errors[0][:msg] },
|
||||
status: :bad_request
|
||||
status: status
|
||||
)
|
||||
end
|
||||
|
||||
|
|
60
app/controllers/repp/v1/domains_controller.rb
Normal file
60
app/controllers/repp/v1/domains_controller.rb
Normal file
|
@ -0,0 +1,60 @@
|
|||
module Repp
|
||||
module V1
|
||||
class DomainsController < BaseController
|
||||
before_action :set_authorized_domain, only: [:transfer_info]
|
||||
|
||||
def index
|
||||
records = current_user.registrar.domains
|
||||
domains = records.limit(limit).offset(offset)
|
||||
domains = domains.pluck(:name) unless params[:details] == 'true'
|
||||
|
||||
render_success(data: { domains: domains, total_number_of_records: records.count })
|
||||
end
|
||||
|
||||
def transfer_info
|
||||
contact_fields = %i[code name ident ident_type ident_country_code phone email street city
|
||||
zip country_code statuses]
|
||||
|
||||
data = {
|
||||
domain: @domain.name,
|
||||
registrant: @domain.registrant.as_json(only: contact_fields),
|
||||
admin_contacts: @domain.admin_contacts.map { |c| c.as_json(only: contact_fields) },
|
||||
tech_contacts: @domain.tech_contacts.map { |c| c.as_json(only: contact_fields) },
|
||||
}
|
||||
|
||||
render_success(data: data)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def transfer_info_params
|
||||
params.require(:id)
|
||||
params.permit(:id)
|
||||
end
|
||||
|
||||
def set_authorized_domain
|
||||
@epp_errors ||= []
|
||||
h = {}
|
||||
h[transfer_info_params[:id].match?(/\A[0-9]+\z/) ? :id : :name] = transfer_info_params[:id]
|
||||
@domain = Domain.find_by!(h)
|
||||
|
||||
return if @domain.transfer_code.eql?(request.headers['Auth-Code'])
|
||||
|
||||
@epp_errors << { code: '401', msg: I18n.t('errors.messages.epp_authorization_error') }
|
||||
handle_errors
|
||||
end
|
||||
|
||||
def limit
|
||||
params[:limit] || 200
|
||||
end
|
||||
|
||||
def offset
|
||||
params[:offset] || 0
|
||||
end
|
||||
|
||||
def index_params
|
||||
params.permit(:limit, :offset, :details)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -59,6 +59,12 @@ Rails.application.routes.draw do
|
|||
end
|
||||
end
|
||||
end
|
||||
resources :domains do
|
||||
collection do
|
||||
get ':id/transfer_info', to: 'domains#transfer_info', constraints: { id: /.*/ }
|
||||
end
|
||||
end
|
||||
|
||||
namespace :domains do
|
||||
resources :contacts do
|
||||
collection do
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue