2017-01-20 06:55:44 +08:00
|
|
|
module Services
|
|
|
|
class LiveEventsSubscriptionService
|
|
|
|
class << self
|
|
|
|
def available?
|
|
|
|
settings.present?
|
|
|
|
end
|
|
|
|
|
2017-02-09 03:04:05 +08:00
|
|
|
def tool_proxy_subscription(tool_proxy, subscription_id)
|
|
|
|
options = { headers: headers(tool_proxy_jwt_body(tool_proxy)) }
|
|
|
|
request(:get, "/api/subscriptions/#{subscription_id}", options)
|
|
|
|
end
|
|
|
|
|
2017-01-20 06:55:44 +08:00
|
|
|
def tool_proxy_subscriptions(tool_proxy)
|
2017-02-09 03:04:05 +08:00
|
|
|
options = { headers: headers(tool_proxy_jwt_body(tool_proxy)) }
|
2017-01-20 06:55:44 +08:00
|
|
|
request(:get, '/api/subscriptions', options)
|
|
|
|
end
|
|
|
|
|
Add create to live event subscription service
fixes PLAT-2184
Test plan:
* With the subscription service and canvas configured to use the same
encryption and signing secret
* Start up the subscription service and a rails console
* In the console run the following commands
ToolProxy = Struct.new("ToolProxy", :guid, :product_family)
Family = Struct.new("Family", :developer_key)
f = Family.new(10000000000003)
tp = ToolProxy.new('hahahah', f)
subscription = {
"RootAccountId" => "1",
"EventTypes" => ["submission_created"],
"ContextType" => "quiz",
"ContextId" => "5001",
"Format" => "live-event",
"TransportType" => "sqs",
"TransportMetadata" => {
"Url" => "http://sqs.docker"
},
"UserId" => "3000",
}
res = Services::LiveEventsSubscriptionService.create_tool_proxy_subscription(tp, subscription)
* Ensure that you get a 200 response back with the newly created
subscription in it
* Ensure that dynamo has the new record
Change-Id: Ifa7f68983efbcb79058854fbd3ba802505b8d4b2
Reviewed-on: https://gerrit.instructure.com/101069
Tested-by: Jenkins
Reviewed-by: Matthew Wheeler <mwheeler@instructure.com>
Reviewed-by: Nathan Mills <nathanm@instructure.com>
QA-Review: August Thornton <august@instructure.com>
Product-Review: Andrew Butterfield <abutterfield@instructure.com>
2017-02-04 06:57:17 +08:00
|
|
|
def create_tool_proxy_subscription(tool_proxy, subscription)
|
|
|
|
options = {
|
2017-02-09 03:04:05 +08:00
|
|
|
headers: headers(tool_proxy_jwt_body(tool_proxy), { 'Content-Type' => 'application/json' }),
|
Add create to live event subscription service
fixes PLAT-2184
Test plan:
* With the subscription service and canvas configured to use the same
encryption and signing secret
* Start up the subscription service and a rails console
* In the console run the following commands
ToolProxy = Struct.new("ToolProxy", :guid, :product_family)
Family = Struct.new("Family", :developer_key)
f = Family.new(10000000000003)
tp = ToolProxy.new('hahahah', f)
subscription = {
"RootAccountId" => "1",
"EventTypes" => ["submission_created"],
"ContextType" => "quiz",
"ContextId" => "5001",
"Format" => "live-event",
"TransportType" => "sqs",
"TransportMetadata" => {
"Url" => "http://sqs.docker"
},
"UserId" => "3000",
}
res = Services::LiveEventsSubscriptionService.create_tool_proxy_subscription(tp, subscription)
* Ensure that you get a 200 response back with the newly created
subscription in it
* Ensure that dynamo has the new record
Change-Id: Ifa7f68983efbcb79058854fbd3ba802505b8d4b2
Reviewed-on: https://gerrit.instructure.com/101069
Tested-by: Jenkins
Reviewed-by: Matthew Wheeler <mwheeler@instructure.com>
Reviewed-by: Nathan Mills <nathanm@instructure.com>
QA-Review: August Thornton <august@instructure.com>
Product-Review: Andrew Butterfield <abutterfield@instructure.com>
2017-02-04 06:57:17 +08:00
|
|
|
body: subscription.to_json
|
|
|
|
}
|
|
|
|
request(:post, '/api/subscriptions', options)
|
|
|
|
end
|
|
|
|
|
2017-02-09 03:12:11 +08:00
|
|
|
def destroy_tool_proxy_subscription(tool_proxy, subscription_id)
|
|
|
|
options = { headers: headers(tool_proxy_jwt_body(tool_proxy)) }
|
|
|
|
request(:delete, "/api/subscriptions/#{subscription_id}", options)
|
|
|
|
end
|
|
|
|
|
2017-01-20 06:55:44 +08:00
|
|
|
private
|
|
|
|
def request(method, endpoint, options = {})
|
2017-01-27 07:29:30 +08:00
|
|
|
Canvas.timeout_protection("live-events-subscription-service-session", raise_on_timeout: true) do
|
|
|
|
HTTParty.send(method, "#{settings['app-host']}#{endpoint}", options.merge(timeout: 10))
|
|
|
|
end
|
2017-01-20 06:55:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def headers(jwt_body, headers = {})
|
|
|
|
token = Canvas::Security::ServicesJwt.generate(jwt_body)
|
|
|
|
headers['Authorization'] = "Bearer #{token}"
|
|
|
|
headers
|
|
|
|
end
|
|
|
|
|
|
|
|
def settings
|
2017-01-27 07:29:30 +08:00
|
|
|
Canvas::DynamicSettings.from_cache("live-events-subscription-service", expires_in: 5.minutes)
|
2017-01-20 06:55:44 +08:00
|
|
|
rescue Faraday::ConnectionFailed,
|
|
|
|
Faraday::ClientError,
|
|
|
|
Canvas::DynamicSettings::ConsulError,
|
|
|
|
Diplomat::KeyNotFound => e
|
|
|
|
Canvas::Errors.capture_exception(:live_events_subscription, e)
|
|
|
|
nil
|
|
|
|
end
|
2017-02-09 03:04:05 +08:00
|
|
|
|
|
|
|
def tool_proxy_jwt_body(tool_proxy, options = {})
|
|
|
|
options.merge({
|
|
|
|
sub: "ltiToolProxy:#{tool_proxy.guid}",
|
|
|
|
developerKey: tool_proxy.product_family.developer_key.to_s
|
|
|
|
})
|
|
|
|
end
|
2017-01-20 06:55:44 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|