-- Autogenerated with DRAKON Editor 1.32
box.cfg {
	background = true,
	pid_file = "/dkt/tmp/https_sender.pid",
	log = "/dkt/logs/https_sender.log",
	memtx_dir = "/dkt/tmp",
	wal_dir = "/dkt/tmp",
	custom_proc_title = "https_sender",
	work_dir = "/dkt/app"
}



local table = table
local string = string
local pairs = pairs
local ipairs = ipairs
local type = type

local tostring = tostring
local tonumber = tonumber
local error = error
local pcall = pcall

-- configuration
local global_cfg = global_cfg

local math = require("math")
local clock = require("clock")
local log = require("log")

local digest = require("digest")
local fiber = require("fiber")
local io = require("io")
local os = require("os")
local utf8 = require("lua-utf8")
local json=require('json')
local msgpack = require("msgpack")
local pickle = require("pickle")
local socket = require("socket")

local utils = require("utils")

setfenv(1, {}) 



function handler(sock, peer)
    -- item 29
    local result
    -- item 13
    local request = utils.receive_msgpack(sock)
    -- item 14
    if request then
        -- item 17
        local ok, response = make_https_call(request)
        -- item 26
        if ok then
            -- item 30
            result = response
        else
            -- item 45
            local message = 
              "error when making http call: "
              .. tostring(response)
            -- item 33
            log.error(message)
            -- item 28
            result = {error=message}
        end
        -- item 18
        utils.send_msgpack(sock, result)
    else
        -- item 32
        log.error("could not read request")
    end
end

function make_https_call(request)
    -- item 38
    local result
    -- item 35
    if type(request.data) == "string" then
        -- item 39
        local result = utils.http_post(
        	request.url,
        	request.data,
        	request.mime,
        	request.headers,
        	request.user
        )
        -- item 40
        if result then
            -- item 59
            if result == "" then
                -- item 61
                return true, {}
            else
                -- item 62
                if request.result == "json" then
                    -- item 53
                    local decode = function()
                     return json.decode(result)
                    end
                    -- item 54
                    local ok, obj2 = pcall(decode)
                    -- item 55
                    if ok then
                        -- item 57
                        return true, obj2
                    else
                        -- item 58
                        return false, tostring(obj2)
                         .. ": '" .. tostring(result) .."'"
                    end
                else
                    -- item 64
                    return true, result
                end
            end
        else
            -- item 44
            return false, "error making HTTP request"
        end
    else
        -- item 24
        local obj = utils.http_post_json(
        	request.url,
        	request.data,
        	request.headers,
        	request.user
        )
        -- item 25
        return true, obj
    end
end

function start(port)
    -- item 6
    log.info("starting to listen on port "
     .. tostring(port))
    -- item 7
    socket.tcp_server('localhost', port, handler)
end


start(3400)


return {
	
}
