%% luahttp.sty
%% Copyright 2023 Johannes Casaburi
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   https://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions of LaTeX
% version 2008 or later.
%
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is Johannes Casaburi (johannes.casaburi@protonmail.com).
%
% This work consists of the files luahttp.sty, display.lua and fetch.lua.

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{luahttp}[LuaHTTP Package, Version 1.0.1]

\RequirePackage{ifluatex}
\RequirePackage{url}
\RequirePackage{xparse}

\ifluatex
    \RequirePackage{luapackageloader}
    \directlua{
        version = 5.3
        package.path = 'lua_modules/share/lua/' .. version .. '/?.lua;lua_modules/share/lua/' .. version .. '/?/init.lua;' .. package.path
        package.cpath = 'lua_modules/lib/lua/' .. version .. '/?.so;' .. package.cpath
        display = require("luahttp-display")
    }

    % \fetchJson{URL}[optional: "key1,key2,.."]
    \NewDocumentCommand{\fetchJson}{m o}{
        \IfNoValueTF{#2}
            {\directlua{display.json("\luaescapestring{#1}")}} % without arg 2
            {\directlua{display.json("\luaescapestring{#1}", "\luaescapestring{#2}")}}
    }

    % \fetchJsonUsingFile{JSON file}[optional: "key1,key2,.."]
    \NewDocumentCommand{\fetchJsonUsingFile}{m o}{
        \IfNoValueTF{#2}
            {\directlua{display.json_using_file("\luaescapestring{#1}")}} % without arg 2
            {\directlua{display.json_using_file("\luaescapestring{#1}", "\luaescapestring{#2}")}}
    }

    % \fetchJsonUsingQuery{URL}{"key1,key2,.."} [optional: "queryparameter1=value1"] .. [optional: "queryparameter5=value5"]
    \NewDocumentCommand{\fetchJsonUsingQuery}{m m o o o o o}{
        \IfNoValueTF{#7}
            {\IfNoValueTF{#6}
                {\IfNoValueTF{#5}
                    {\IfNoValueTF{#4}
                        {\IfNoValueTF{#3}
                            {\directlua{display.json_using_query("\luaescapestring{#1}", "\luaescapestring{#2}")}}
                            {\directlua{display.json_using_query("\luaescapestring{#1}", "\luaescapestring{#2}", "\luaescapestring{#3}")}}
                        }
                        {\directlua{display.json_using_query("\luaescapestring{#1}", "\luaescapestring{#2}", "\luaescapestring{#3}", "\luaescapestring{#4}")}}
                    }
                    {\directlua{display.json_using_query("\luaescapestring{#1}", "\luaescapestring{#2}", "\luaescapestring{#3}", "\luaescapestring{#4}", "\luaescapestring{#5}")}}
                }
                {\directlua{display.json_using_query("\luaescapestring{#1}", "\luaescapestring{#2}", "\luaescapestring{#3}", "\luaescapestring{#4}", "\luaescapestring{#5}", "\luaescapestring{#6}")}}
            }
            {\directlua{display.json_using_query("\luaescapestring{#1}", "\luaescapestring{#2}", "\luaescapestring{#3}", "\luaescapestring{#4}", "\luaescapestring{#5}", "\luaescapestring{#6}",  "\luaescapestring{#7}")}}
    }

    % \fetchRss{URL}{limit}[optional: "feedinfokey1,feedinfokey2,.."][optional: "entrykey1,entrykey2,.."]
    \NewDocumentCommand{\fetchRss}{m m o o}{
        \IfNoValueTF{#4}
            {\IfNoValueTF{#3}
                {\directlua{display.rss("\luaescapestring{#1}", #2)}}
                {\directlua{display.rss("\luaescapestring{#1}", #2, "\luaescapestring{#3}")}}
            } % without arg 4
            {\directlua{display.rss("\luaescapestring{#1}", #2, "\luaescapestring{#3}", "\luaescapestring{#4}")}
        }
    }

    %\fetchImage{URL}[optional: width][optional: height]
    \NewDocumentCommand{\fetchImage}{m o o}{
        \IfNoValueTF{#2}
            {\directlua{display.image("\luaescapestring{#1}")}} % whithout arg 2
            {\IfNoValueTF{#3}
                {\directlua{display.image("\luaescapestring{#1}", "\luaescapestring{#2}")}} % without arg 3
                {\directlua{display.image("\luaescapestring{#1}", "\luaescapestring{#2}", "\luaescapestring{#3}")}}
            }
    }

\else
    \PackageError{luatexhttp}{LuaTeX is required}\@ehd
    \expandafter\endinput % abort early
\fi
