Project "FreeSWITCH user-contributed scripts, etc" received a push.

branch: master updated

       via: d93d8a0e353b8a112167961178bffda485356ee7 (commit)
      from: f192c27939941db18bdf2c543a9e14b21ae16fd0 (commit)


= COMMIT LOG ===========================================================
committer: Chad Phillips
comments: 
adding service module, for accessing external services -- intial code has an http_request action, which allows GET requests using the HTTP protocol.

diff --git a/hunmonk/jester/conf.lua b/hunmonk/jester/conf.lua
index 721c602..3cc79f1 100644
--- a/hunmonk/jester/conf.lua
+++ b/hunmonk/jester/conf.lua
@@ -61,6 +61,7 @@ modules = {
   "navigation",
   "play",
   "record",
+  "service",
   "system",
   "tracker",
 }
diff --git a/hunmonk/jester/modules/service/conf.lua b/hunmonk/jester/modules/service/conf.lua
new file mode 100644
index 0000000..20d75b8
--- /dev/null
+++ b/hunmonk/jester/modules/service/conf.lua
@@ -0,0 +1,5 @@
+jester.action_map.http_request = {
+  mod = "service",
+  func = "http_request",
+}
+
diff --git a/hunmonk/jester/modules/service/help.lua b/hunmonk/jester/modules/service/help.lua
new file mode 100644
index 0000000..41ce8a8
--- /dev/null
+++ b/hunmonk/jester/modules/service/help.lua
@@ -0,0 +1,39 @@
+jester.help_map.service = {}
+jester.help_map.service.description_short = [[Access external services.]]
+jester.help_map.service.description_long = [[This module provides actions for accessing external services, such as webservices.]]
+
+jester.help_map.service.actions = {}
+
+jester.help_map.service.actions.http_request = {}
+jester.help_map.service.actions.http_request.description_short = [[Make an HTTP request to a external server.]]
+jester.help_map.service.actions.http_request.description_long = [[Make an HTTP request to an external server.
+
+Only the http:// protocol is supported.  The resulting status of the request is stored in the 'last_http_request' storage area, with the following keys:
+
+  code:
+    An HTTP/1.1 return code, or 'error' if the request fails.
+  description:
+    A human-readable description of the code.
+
+If the request returns a '200' code, then the body of the response will be stored according to the parameter settings.]]
+jester.help_map.service.actions.http_request.params = {
+  server = [[(Optional) The server to send the request to, eg. 'www.example.com'. Default is 'localhost'.]],
+  port = [[(Optional) The port to send the request to.  Default is 80.]],
+  path = [[(Optional) The server path to send the request to, no leading or trailing slash, eg. 'path/to/resource'.]],
+  query = [[(Optional) A table of query parameters to append to the URL.  Keys must be only numbers, letters, and underscores, values can be any string and will be properly URL escaped before sending, eg. '{foo = "bar", baz = "two words"}'.]],
+  fragment = [[(Optional) A fragment to append to the URL.  It will be properly escaped before sending.]],
+  user = [[(Optional) A user name to use for basic authentication.]],
+  password = [[(Optional) A password to use for basic authentication.  'user' parameter must also be provided.]],
+  response = [[(Optional) The format to expect the response in:
+  lua:
+    A string that represents a Lua table, of the following format:
+      return {
+        foo = 'bar',
+      }
+      The table data will be loaded into the specified storage area. Table values cannot contain a table.
+  raw:
+    A string of raw data.  This will be stored in the specified storage area under the 'raw' storage key.
+Default is 'raw'.]],
+  storage_area = [[(Optional) The storage area to store the response in.  Defaults to 'service'.]],
+}
+
diff --git a/hunmonk/jester/modules/service/service.lua b/hunmonk/jester/modules/service/service.lua
new file mode 100644
index 0000000..b4190d5
--- /dev/null
+++ b/hunmonk/jester/modules/service/service.lua
@@ -0,0 +1,91 @@
+module(..., package.seeall)
+
+--[[
+  Log to the console.
+]]
+function http_request(action)
+  local server = action.server or "localhost"
+  local port = action.port or "80"
+  local path = action.path or "/"
+  local query = action.query
+  local fragment = action.fragment or ""
+  local user = action.user
+  local password = action.password
+  local response = action.response or "raw"
+  local area = action.storage_area or "service"
+  local query_string = ""
+  local user_string = ""
+
+  local http = require("socket.http")
+  local url = require("socket.url")
+
+  -- Transform the query params into a query string.
+  if path ~= "/" then
+    path = "/" .. path .. "/"
+  end
+  if query then
+    local query_parts = {}
+    for k, v in pairs(query) do
+      -- Escape values but not keys.
+      query_parts[#query_parts + 1] = k .. "=" .. url.escape(v)
+    end
+    query_string = "?" .. table.concat(query_parts, "&")
+  end
+  if fragment ~= "" then
+    fragment = "#" .. fragment
+  end
+  if user then
+    user_string = user
+    if password then
+      user_string = user_string .. ":" .. password
+    end
+    user_string = user_string .. "@"
+  end
+  url = "http://" .. user_string .. server .. ":" .. port .. path .. query_string .. fragment
+  jester.debug_log("HTTP request, URL: %s", url)
+
+  local code, description, data
+  -- Send the request.
+  local body, status_code, headers, status_description = http.request(url)
+  -- Request succeeded.
+  if body then
+    data = body
+    code = status_code
+    description = status_description
+    -- Only store response data if we get a 200 OK code.
+    if code == 200 then
+      -- Store raw data.
+      if response == "raw" then
+        jester.set_storage(area, "raw", data)
+      -- Data is a Lua table.
+      elseif response == "lua" then
+        -- Load the data string as Lua code.
+        local func = loadstring(data)
+        if func then
+          -- Sandbox the function to protect against malicious code.
+          setfenv(func, {})
+          local table_data = func()
+          -- Make sure a table was returned.
+          if type(table_data) == "table" then
+            for k, v in pairs(table_data) do
+              jester.set_storage(area, k, v)
+            end
+          else
+            jester.debug_log("ERROR: Returned data is not a Lua table.")
+          end
+        else
+          jester.debug_log("ERROR: Failed to parse response body as Lua code.")
+        end
+      end
+    end
+  -- Request failed.
+  else
+    code = "error"
+    description = status_code
+    data = ""
+  end
+  jester.set_storage("last_http_request", "code", code)
+  jester.set_storage("last_http_request", "description", description)
+  jester.debug_log("HTTP response, Code: %s, Description: %s, Data: %s", code, description, data)
+end
+
========================================================================

Summary of changes:
 hunmonk/jester/conf.lua                    |    1 +
 hunmonk/jester/modules/service/conf.lua    |    5 ++
 hunmonk/jester/modules/service/help.lua    |   39 ++++++++++++
 hunmonk/jester/modules/service/service.lua |   91 ++++++++++++++++++++++++++++
 4 files changed, 136 insertions(+), 0 deletions(-)
 create mode 100644 hunmonk/jester/modules/service/conf.lua
 create mode 100644 hunmonk/jester/modules/service/help.lua
 create mode 100644 hunmonk/jester/modules/service/service.lua

this email was generated because of /git/your-repo.git/hooks/post-receive by the file /git-core/contrib/hooks/post-receive-email
For more info, see http://blog.chomperstomp.com/?p=630 --
FreeSWITCH user-contributed scripts, etc