Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Adding an Async HttpClient module #1

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

danilopucci
Copy link
Owner

Description

This PR adds a Http client module.

  • It has support to make Http resquests in lua and in C++
  • Async requests
  • easy usage syntax (inspired on Requests from Python)

There is example on how to use it via talkaction: /httpclient <request_name>
/httpclient get
/httpclient put
More details, check in data/talkactions/scripts/httpclient.lua

How to use in lua:

local function requestCallback(response)
 --[[response is a table with current fields:
    "requestId"
    "version"
    "statusCode"
    "location"
    "contentType"
    "responseTimeMs"
    "headerData"
    "bodySize"
    "bodyData"
    "success"
    "errorMessage"
    
    where you can use "contentType" to get if it is a json and parse "bodyData" accordinly
 ]]
end


-- dummy exemple to get something
local httpClientRequest = HttpClientRequest()
local url = "https://myapi.mydomain.com"
httpClientRequest:get(url, requestCallback)


-- dummy example to post something

local url = "https://myapi.mydomain.com"
local token = "abcdef123456789"
local data = "I want to post something"
local fields = {
    accept = "application/json", 
    authorization = "Bearer " .. token
    }
httpClientRequest:post(url, requestCallback, fields, data)

How to use in C++:

//define a callback function to parse response:
void requestCallback(const HttpClient::HttpResponse_ptr& response)
{
//checkout HttpResponse structure to know its contents
}

HttpClientLib::HttpRequest_ptr httpRequest = std::make_shared<HttpClientLib::HttpRequest>();

httpRequest->url = "https://myapi.mydomain.com";
httpRequest->data = "I want to post something";
httpRequest->fields = {
                {"Authorization", "Bearer " + token},
                {"Accept", "application/json"}
            };

httpRequest->callbackData.callbackFunction = requestCallback;

g_http.addRequest(httpRequest);

Behaviour

It is based on http client lib. There is a dedicated thread, very similar to DatabaseTasks which will handle requests both comming from C++ or Lua context.

Files added:

  • httpclient.cpp : here you can find the thread implementation, which is similar to DatabaseTasks
  • httpclient.h: header to httpclient
  • httpclientlib.h: here you can find the lib.
    • In short it is a header only library, implemented over Boost.beast lib

Expected

Do this and that happens

Fixes #issuenumber

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Test A
  • Test B

Test Configuration:

  • Server Version:
  • Client:
  • Operating System:

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I checked the PR checks reports
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant