Lua
Lua
SNS List Topics
See more Amazon SNS Examples
List the existing SNS topics.See SNS List Topics for more information.
Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local rest = chilkat.Rest.new()
-- Connect to the Amazon AWS REST server.
-- such as https://sns.us-west-2.amazonaws.com/
local bTls = true
local port = 443
local bAutoReconnect = true
local success = rest:connect("sns.us-west-2.amazonaws.com", port, bTls, bAutoReconnect)
-- Provide AWS credentials for the REST call.
local authAws = chilkat.AuthAws.new()
authAws.access_key = "AWS_ACCESS_KEY"
authAws.secret_key = "AWS_SECRET_KEY"
-- the region should match our URL above..
authAws.region = "us-west-2"
authAws.service_name = "sns"
rest:set_auth_aws(authAws)
rest:add_query_param("Action", "ListTopics")
local responseXml = rest:full_request_no_body("GET", "/")
if not rest.last_method_success then
print(rest.last_error_text)
return
end
-- A successful response will have a status code equal to 200.
if rest.response_status_code ~= 200 then
print("response status code = " .. rest.response_status_code)
print("response status text = " .. rest.response_status_text)
print("response header: " .. rest.response_header)
print("response body: " .. responseXml)
return
end
-- Examine the successful XML response (shown below)
local xml = chilkat.Xml.new()
xml:load_xml(responseXml)
print(xml:get_xml())
-- To iterate over the TopicArn's
local notUsed = xml:chilkat_path("ListTopicsResult|Topics|$")
local i = 0
local numTopics = xml.num_children
while i < numTopics do
xml:get_child2(i)
print(xml:get_child_content("TopicArn"))
xml:get_parent2()
i = i + 1
end
xml:get_root2()
-- A sample successful response:
-- <?xml version="1.0" encoding="utf-8" ?>
-- <ListTopicsResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/">
-- <ListTopicsResult>
-- <Topics>
-- <member>
-- <TopicArn>arn:aws:sns:us-west-2:957491831129:chilkat</TopicArn>
-- </member>
-- <member>
-- <TopicArn>arn:aws:sns:us-west-2:957491831129:chilkat123</TopicArn>
-- </member>
-- <member>
-- <TopicArn>arn:aws:sns:us-west-2:957491831129:chilkatses</TopicArn>
-- </member>
-- </Topics>
-- </ListTopicsResult>
-- <ResponseMetadata>
-- <RequestId>54514f94-b07d-5f0b-9c8a-1ff85ff4ac65</RequestId>
-- </ResponseMetadata>
-- </ListTopicsResponse>