~singpolyma/sgx-jmp

fea0ffeb22d0f229d528b30f3844c425b5009ad9 — Stephen Paul Weber 1 year, 9 months ago 023c87a + 6290b10
Merge branch 'no-more-catapult'

* no-more-catapult:
  Remove catapult import code
8 files changed, 1 insertions(+), 183 deletions(-)

M config-schema.dhall
M config.dhall.sample
M lib/bandwidth_tn_order.rb
D lib/catapult.rb
D test/data/catapult_import_body.json
M test/test_bandwidth_tn_order.rb
M test/test_helper.rb
M test/test_registration.rb
M config-schema.dhall => config-schema.dhall +0 -8
@@ 11,14 11,6 @@
    , private_key : Text
    , public_key : Text
    }
, catapult :
    { application_id : Text
    , domain : Text
    , secret : Text
    , sip_host : Text
    , token : Text
    , user : Text
    }
, component : { jid : Text, secret : Text }
, credit_card_url : forall (jid : Text) -> forall (customer_id : Text) -> Text
, creds : { account : Text, password : Text, username : Text }

M config.dhall.sample => config.dhall.sample +0 -8
@@ 19,14 19,6 @@ in
		username = "dashboard user",
		password = "dashboard password"
	},
	catapult = {
		user = "",
		token = "",
		secret = "",
		application_id = "",
		domain = "",
		sip_host = ""
	},
	web_register = {
		to = "cheogram",
		from = "jmp-register@localhost"

M lib/bandwidth_tn_order.rb => lib/bandwidth_tn_order.rb +1 -28
@@ 4,8 4,6 @@ require "forwardable"
require "ruby-bandwidth-iris"
Faraday.default_adapter = :em_synchrony

require_relative "./catapult"

class BandwidthTNOrder
	def self.get(id)
		EMPromise.resolve(nil).then do


@@ 77,32 75,7 @@ class BandwidthTNOrder
		end

		def poll
			catapult_import.then do |http|
				raise "Catapult import failed" unless http.response_header.status == 201

				self
			end
		end

	protected

		# After buying, import to catapult and set v1 voice app
		def catapult_import
			CATAPULT.import(
				number: tel,
				provider: dashboard_provider
			)
		end

		def dashboard_provider
			{
				providerName: "bandwidth-dashboard",
				properties: {
					accountId: CONFIG[:creds][:account],
					userName: CONFIG[:creds][:username],
					password: CONFIG[:creds][:password]
				}
			}
			EMPromise.resolve(self)
		end
	end


D lib/catapult.rb => lib/catapult.rb +0 -99
@@ 1,99 0,0 @@
# frozen_string_literal: true

require "value_semantics/monkey_patched"

class Catapult
	value_semantics do
		user String
		token String
		secret String
		application_id String
		domain String
		sip_host String
	end

	def import(body)
		post(
			"phoneNumbers",
			body: { applicationId: application_id }.merge(body)
		)
	end

	def create_endpoint(body)
		post(
			"domains/#{@domain}/endpoints",
			body: { applicationId: @application_id }.merge(body)
		).then do |http|
			unless http.response_header.status == 201
				raise "Create new SIP account failed"
			end

			http.response_header["location"]
		end
	end

	def endpoint_list(page=0)
		get(
			"domains/#{@domain}/endpoints",
			query: { size: 1000, page: page }
		).then do |http|
			next [] if http.response_header.status == 404
			raise "Could not list endpoints" if http.response_header.status != 200

			JSON.parse(http.response)
		end
	end

	def endpoint_find(name, page=0)
		endpoint_list(page).then do |list|
			next if list.empty?

			if (found = list.find { |e| e["name"] == name })
				found.merge("url" => CATAPULT.mkurl(
					"domains/#{found['domainId']}/endpoints/#{found['id']}"
				))
			else
				endpoint_find(name, page + 1)
			end
		end
	end

	def post(path, body:, head: {})
		EM::HttpRequest.new(
			mkurl(path), tls: { verify_peer: true }
		).apost(
			head: catapult_headers.merge(head),
			body: body.to_json
		)
	end

	def delete(path, head: {})
		EM::HttpRequest.new(
			mkurl(path), tls: { verify_peer: true }
		).adelete(head: catapult_headers.merge(head))
	end

	def get(path, head: {}, **kwargs)
		EM::HttpRequest.new(
			mkurl(path), tls: { verify_peer: true }
		).aget(head: catapult_headers.merge(head), **kwargs)
	end

	def mkurl(path)
		base = "https://api.catapult.inetwork.com/v1/users/#{@user}/"
		return path if path.start_with?(base)

		"#{base}#{path}"
	end

protected

	def catapult_headers
		{
			"Authorization" => [@token, @secret],
			"Content-Type" => "application/json"
		}
	end
end

CATAPULT = Catapult.new(**CONFIG[:catapult])

D test/data/catapult_import_body.json => test/data/catapult_import_body.json +0 -1
@@ 1,1 0,0 @@
{"applicationId":"catapult_app","number":"+15555550000","provider":{"providerName":"bandwidth-dashboard","properties":{"accountId":"test_bw_account","userName":"test_bw_user","password":"test_bw_password"}}}

M test/test_bandwidth_tn_order.rb => test/test_bandwidth_tn_order.rb +0 -21
@@ 60,16 60,6 @@ class BandwidthTNOrderTest < Minitest::Test
					</CompletedNumbers>
				</OrderResponse>
			RESPONSE
			stub_request(
				:post,
				"https://api.catapult.inetwork.com/v1/users/catapult_user/phoneNumbers"
			).with(
				body: File.open("#{__dir__}/data/catapult_import_body.json").read.chomp,
				headers: {
					"Authorization" => "Basic Y2F0YXB1bHRfdG9rZW46Y2F0YXB1bHRfc2VjcmV0",
					"Content-Type" => "application/json"
				}
			).to_return(status: 201)
			@order.poll.sync
			assert_requested req
		end


@@ 86,18 76,7 @@ class BandwidthTNOrderTest < Minitest::Test
		end

		def test_poll
			req = stub_request(
				:post,
				"https://api.catapult.inetwork.com/v1/users/catapult_user/phoneNumbers"
			).with(
				body: File.open("#{__dir__}/data/catapult_import_body.json").read.chomp,
				headers: {
					"Authorization" => "Basic Y2F0YXB1bHRfdG9rZW46Y2F0YXB1bHRfc2VjcmV0",
					"Content-Type" => "application/json"
				}
			).to_return(status: 201)
			assert_equal @order, @order.poll.sync
			assert_requested req
		end
		em :test_poll
	end

M test/test_helper.rb => test/test_helper.rb +0 -8
@@ 64,14 64,6 @@ CONFIG = {
		username: "test_bw_user",
		password: "test_bw_password"
	},
	catapult: {
		user: "catapult_user",
		token: "catapult_token",
		secret: "catapult_secret",
		domain: "catapult_domain",
		sip_host: "host.bwapp.io.example.com",
		application_id: "catapult_app"
	},
	activation_amount: 1,
	plans: [
		{

M test/test_registration.rb => test/test_registration.rb +0 -10
@@ 691,16 691,6 @@ class RegistrationTest < Minitest::Test
			RESPONSE
			stub_request(
				:post,
				"https://api.catapult.inetwork.com/v1/users/catapult_user/phoneNumbers"
			).with(
				body: File.open("#{__dir__}/data/catapult_import_body.json").read.chomp,
				headers: {
					"Authorization" => "Basic Y2F0YXB1bHRfdG9rZW46Y2F0YXB1bHRfc2VjcmV0",
					"Content-Type" => "application/json"
				}
			).to_return(status: 201)
			stub_request(
				:post,
				"https://dashboard.bandwidth.com/v1.0/accounts//sites//sippeers//movetns"
			)
			Registration::Finish::REDIS.expect(