SOMHunter Core
sh::NetworkApi Class Reference

Class responsible for listening for HTTP requests and for handlling them using the core. More...

#include <network-api.h>

Collaboration diagram for sh::NetworkApi:

Public Member Functions

 NetworkApi ()=delete
 Only single ctor overload is acceptable. More...
 
 NetworkApi (const ApiConfig &API, Somhunter *p_core)
 Constructs the HTTP API instance for the given core and with the given API config. More...
 
void initialize ()
 Initializes the HTTP listeners on the required endpoints. More...
 
void run ()
 The "main" function of the HTTP API - calls both initialize & terminate. More...
 
void terminate ()
 Safely terminates all the endpoint listeners. More...
 

Private Member Functions

void handle__api__GET (http_request req)
 
void handle__api__config__GET (http_request req)
 
void handle__config__GET (http_request req)
 
void handle__user__context__GET (http_request req)
 
void handle__dataset__video_detail__GET (http_request req)
 
void handle__search__get_top_display__POST (http_request req)
 
void handle__search__get_som_display__POST (http_request req)
 
void handle__search__get_som_relocation_display__POST (http_request req)
 
void handle__search__keyword_autocomplete__GET (http_request req)
 
void handle__search__reset__POST (http_request req)
 
void handle__search__rescore__POST (http_request req)
 
void handle__search__like_frame__POST (http_request req)
 
void handle__search__bookmark_frame__POST (http_request req)
 
void handle__search__context__POST (http_request req)
 
void handle__search__context__GET (http_request req)
 
void handle__log__scroll__GET (http_request req)
 
void handle__log__text_query_change__GET (http_request req)
 
void handle__log__canvas_query_change__GET (http_request req)
 
void handle__eval_server__submit__POST (http_request req)
 
void handle__eval_server__login__POST (http_request req)
 
void handle__eval_server__logout__POST (http_request req)
 
void push_endpoint (const std::string &path, std::function< void(NetworkApi *, http_request)> GET_handler=std::function< void(NetworkApi *, http_request)>{}, std::function< void(NetworkApi *, http_request)> POST_handler=std::function< void(NetworkApi *, http_request)>{}, std::function< void(NetworkApi *, http_request)> PUT_handler=std::function< void(NetworkApi *, http_request)>{}, std::function< void(NetworkApi *, http_request)> DEL_handler=std::function< void(NetworkApi *, http_request)>{})
 Registers the provided handlers for the given path in the HTTP listener. More...
 
RescoreMetadata extract_rescore_metadata (web::json::value &body)
 Extracts RescoreMetadata from the body variable. More...
 
std::vector< TextualQueryextract_textual_query (web::json::value &body)
 Extracts a vector of TextualQuery from the body variable. More...
 
std::vector< RelocationQueryextract_relocation_query (web::json::value &body)
 Extracts a vector of RelocationQuery from the body variable. More...
 
std::vector< CanvasQueryextract_canvas_query (web::json::value &body)
 Extracts a vector of CanvasQuery from the body variable. More...
 
Filters extract_filters (web::json::value &body)
 Extracts Filters from the body variable. More...
 
std::lock_guard< std::mutex > exclusive_lock () const
 

Static Private Member Functions

static void add_CORS_headers (http_response &res)
 Adds the CORS headers to the given HTTP response instance. More...
 

Private Attributes

ApiConfig _API_config
 A private copy of config. More...
 
Somhunter_p_core
 A pointer to the core that this API exposes. More...
 
std::string _base_addr
 Base HTTP API address. More...
 
std::vector< http_listener > _endpoints
 Vector of used endpoints. More...
 
std::mutex _req_mtx
 Lock used to synchronize some requests. More...
 

Detailed Description

Class responsible for listening for HTTP requests and for handlling them using the core.

This is the universal API to the SOMHunter Core if you're not able to call the native C++ API of class Somhunter directly.

See also
class Somhunter
Remarks
If you're on Windows and you want to listen for requests from public network, you need to have Administrator privilages, otherwise the listener initialization will fail.

Constructor & Destructor Documentation

◆ NetworkApi() [1/2]

sh::NetworkApi::NetworkApi ( )
delete

Only single ctor overload is acceptable.

◆ NetworkApi() [2/2]

NetworkApi::NetworkApi ( const ApiConfig API,
Somhunter p_core 
)

Constructs the HTTP API instance for the given core and with the given API config.

Member Function Documentation

◆ add_CORS_headers()

void NetworkApi::add_CORS_headers ( http_response &  res)
staticprivate

Adds the CORS headers to the given HTTP response instance.

◆ exclusive_lock()

std::lock_guard<std::mutex> sh::NetworkApi::exclusive_lock ( ) const
inlineprivate

◆ extract_canvas_query()

std::vector< CanvasQuery > NetworkApi::extract_canvas_query ( web::json::value &  body)
private

Extracts a vector of CanvasQuery from the body variable.

Parameters
bodyJSON structure to be parsed.
Returns
New instance of parsed vector of CanvasQuery structure.

◆ extract_filters()

Filters NetworkApi::extract_filters ( web::json::value &  body)
private

Extracts Filters from the body variable.

Parameters
bodyJSON structure to be parsed.
Returns
New instance of parsed Filters structure.

◆ extract_relocation_query()

std::vector< RelocationQuery > NetworkApi::extract_relocation_query ( web::json::value &  body)
private

Extracts a vector of RelocationQuery from the body variable.

Parameters
bodyJSON structure to be parsed.
Returns
New instance of parsed vector of RelocationQuery structure.

◆ extract_rescore_metadata()

RescoreMetadata NetworkApi::extract_rescore_metadata ( web::json::value &  body)
private

Extracts RescoreMetadata from the body variable.

Parameters
bodyJSON structure to be parsed.
Returns
New instance of parsed RescoreMetadata structure.
Here is the call graph for this function:

◆ extract_textual_query()

std::vector< TextualQuery > NetworkApi::extract_textual_query ( web::json::value &  body)
private

Extracts a vector of TextualQuery from the body variable.

Parameters
bodyJSON structure to be parsed.
Returns
New instance of parsed vector of TextualQuery structure.

◆ handle__api__config__GET()

void NetworkApi::handle__api__config__GET ( http_request  req)
private
Here is the call graph for this function:

◆ handle__api__GET()

void NetworkApi::handle__api__GET ( http_request  req)
private
Here is the call graph for this function:

◆ handle__config__GET()

void NetworkApi::handle__config__GET ( http_request  req)
private
Here is the call graph for this function:

◆ handle__dataset__video_detail__GET()

void NetworkApi::handle__dataset__video_detail__GET ( http_request  req)
private
Here is the call graph for this function:

◆ handle__eval_server__login__POST()

void NetworkApi::handle__eval_server__login__POST ( http_request  req)
private
Here is the call graph for this function:

◆ handle__eval_server__logout__POST()

void NetworkApi::handle__eval_server__logout__POST ( http_request  req)
private
Here is the call graph for this function:

◆ handle__eval_server__submit__POST()

void NetworkApi::handle__eval_server__submit__POST ( http_request  req)
private

◆ handle__log__canvas_query_change__GET()

void NetworkApi::handle__log__canvas_query_change__GET ( http_request  req)
private
Here is the call graph for this function:

◆ handle__log__scroll__GET()

void NetworkApi::handle__log__scroll__GET ( http_request  req)
private
Here is the call graph for this function:

◆ handle__log__text_query_change__GET()

void NetworkApi::handle__log__text_query_change__GET ( http_request  req)
private
Here is the call graph for this function:

◆ handle__search__bookmark_frame__POST()

void NetworkApi::handle__search__bookmark_frame__POST ( http_request  req)
private
Here is the call graph for this function:

◆ handle__search__context__GET()

void NetworkApi::handle__search__context__GET ( http_request  req)
private
Here is the call graph for this function:

◆ handle__search__context__POST()

void NetworkApi::handle__search__context__POST ( http_request  req)
private
Here is the call graph for this function:

◆ handle__search__get_som_display__POST()

void NetworkApi::handle__search__get_som_display__POST ( http_request  req)
private
Here is the call graph for this function:

◆ handle__search__get_som_relocation_display__POST()

void NetworkApi::handle__search__get_som_relocation_display__POST ( http_request  req)
private
Here is the call graph for this function:

◆ handle__search__get_top_display__POST()

void NetworkApi::handle__search__get_top_display__POST ( http_request  req)
private
Here is the call graph for this function:

◆ handle__search__keyword_autocomplete__GET()

void NetworkApi::handle__search__keyword_autocomplete__GET ( http_request  req)
private
Here is the call graph for this function:

◆ handle__search__like_frame__POST()

void NetworkApi::handle__search__like_frame__POST ( http_request  req)
private
Here is the call graph for this function:

◆ handle__search__rescore__POST()

void NetworkApi::handle__search__rescore__POST ( http_request  req)
private
Here is the call graph for this function:

◆ handle__search__reset__POST()

void NetworkApi::handle__search__reset__POST ( http_request  req)
private
Here is the call graph for this function:

◆ handle__user__context__GET()

void NetworkApi::handle__user__context__GET ( http_request  req)
private
Here is the call graph for this function:

◆ initialize()

void NetworkApi::initialize ( )

Initializes the HTTP listeners on the required endpoints.

Here is the call graph for this function:

◆ push_endpoint()

void NetworkApi::push_endpoint ( const std::string &  path,
std::function< void(NetworkApi *, http_request)>  GET_handler = std::function<void(NetworkApi*, http_request)>{},
std::function< void(NetworkApi *, http_request)>  POST_handler = std::function<void(NetworkApi*, http_request)>{},
std::function< void(NetworkApi *, http_request)>  PUT_handler = std::function<void(NetworkApi*, http_request)>{},
std::function< void(NetworkApi *, http_request)>  DEL_handler = std::function<void(NetworkApi*, http_request)>{} 
)
private

Registers the provided handlers for the given path in the HTTP listener.

Parameters
pathString denoting the path of this endpoint (e.g. /api/do-something).
GET_handlerHandler to call in case of GET request.
POST_handlerHandler to call in case of POST request.
PUT_handlerHandler to call in case of PUT request.
DEL_handlerHandler to call in case of DEL request.
Returns
void
Here is the call graph for this function:

◆ run()

void NetworkApi::run ( )

The "main" function of the HTTP API - calls both initialize & terminate.

Here is the call graph for this function:

◆ terminate()

void NetworkApi::terminate ( )

Safely terminates all the endpoint listeners.

Member Data Documentation

◆ _API_config

ApiConfig sh::NetworkApi::_API_config
private

A private copy of config.

◆ _base_addr

std::string sh::NetworkApi::_base_addr
private

Base HTTP API address.

◆ _endpoints

std::vector<http_listener> sh::NetworkApi::_endpoints
private

Vector of used endpoints.

◆ _p_core

Somhunter* sh::NetworkApi::_p_core
private

A pointer to the core that this API exposes.

◆ _req_mtx

std::mutex sh::NetworkApi::_req_mtx
mutableprivate

Lock used to synchronize some requests.


The documentation for this class was generated from the following files: