view libsci/apic.h @ 56:308aa1086702

lib: cleanup apic
author David Demelier <markand@malikania.fr>
date Wed, 17 Aug 2022 12:51:04 +0200
parents b474f0985e39
children 71cd8447e3a4
line wrap: on
line source

/*
 * apic.h -- synchronous HTTP request
 *
 * Copyright (c) 2021-2022 David Demelier <markand@malikania.fr>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef SCI_APIC_H
#define SCI_APIC_H

/**
 * \file apic.h
 * \brief Synchronous HTTP request.
 *
 * This module provides function to retrieve and send data to scid using HTTP
 * requests.
 */

#include <jansson.h>

/**
 * \brief Maximum error message.
 */
#define APIC_ERR_MAX 128
/**
 * \brief Maximum URL length.
 */
#define APIC_URL_MAX 512
/**
 * \brief Maximum URL length.
 */
#define APIC_KEY_MAX (40 + 1)

/**
 * \brief Request context.
 *
 * This structure contains the error message and the HTTP return status,
 * nothing has to be free'd.
 */
struct apic {
	char error[APIC_ERR_MAX];       /*!< Error message (empty unless). */
	long status;                    /*!< HTTP return code. */
};

/**
 * \brief Client configuration.
 */
extern struct apiconf {
	char baseurl[APIC_URL_MAX];     /*!< Base API URL for requets. */
	char key[APIC_KEY_MAX];         /*!< Maximum length for API key. */
} apiconf /*! Global variable. */;

/**
 * Perform a GET request.
 *
 * The URL format will be appended to the base URL using printf(3) format
 * style.
 *
 * \pre req != NULL
 * \pre fmt != NULL
 * \param req the request output
 * \param fmt the format string for URL page
 * \return the JSON document received from the server or NULL on failure
 */
json_t *
apic_get(struct apic *req, const char *fmt, ...);

/**
 * Perform a POST request.
 *
 * The URL format will be appended to the base URL using printf(3) format
 * style.
 *
 * \pre req != NULL
 * \pre fmt != NULL
 * \param req the request output
 * \param body the optional document body
 * \param fmt the format string for URL page
 * \return the JSON document received from the server or NULL on failure
 */
json_t *
apic_post(struct apic *req, const json_t *body, const char *fmt, ...);

/**
 * Add a new job.
 *
 * \pre req != NULL
 * \pre job != NULL
 * \param req the request output
 * \param job the job model
 * \return 0 on success or -1 on error
 */
int
apic_job_add(struct apic *req, json_t *job);

/**
 * Get a list of job to perform for this worker.
 *
 * \pre req != NULL
 * \pre worker != NULL
 * \param req the request output
 * \param worker the worker name
 * \return the JSON document received from the server or NULL on failure
 */
json_t *
apic_job_todo(struct apic *req, const char *worker);

/**
 * Add a new job result.
 *
 * \pre req != NULL
 * \pre jobresult != NULL
 * \param req the request output
 * \param jobresult the jobresult model
 * \return 0 on success or -1 on error
 */
int
apic_jobresult_add(struct apic *req, json_t *jobresult);

/**
 * Add or update a project.
 *
 * \pre req != NULL
 * \pre project != NULL
 * \param req the request output
 * \param project the project model
 * \return 0 on success or -1 on error
 */
int
apic_project_save(struct apic *req, json_t *project);

/**
 * Get a list of projects.
 *
 * \pre req != NULL
 * \param req the request output
 * \return the JSON document received from the server or NULL on failure
 */
json_t *
apic_project_list(struct apic *req);

/**
 * Get a project by name.
 *
 * \pre req != NULL
 * \pre name != NULL
 * \param name the project name
 * \param req the request output
 * \return the JSON document received from the server or NULL on failure
 */
json_t *
apic_project_find(struct apic *req, const char *name);

/**
 * Add or update a worker.
 *
 * \pre req != NULL
 * \pre worker != NULL
 * \param req the request output
 * \param worker the worker model
 * \return 0 on success or -1 on error
 */
int
apic_worker_save(struct apic *req, json_t *worker);

/**
 * Get a list of workers.
 *
 * \pre req != NULL
 * \param req the request output
 * \return the JSON document received from the server or NULL on failure
 */
json_t *
apic_worker_list(struct apic *req);

/**
 * Get a worker by name.
 *
 * \pre req != NULL
 * \pre name != NULL
 * \param name the worker name
 * \param req the request output
 * \return the JSON document received from the server or NULL on failure
 */
json_t *
apic_worker_find(struct apic *req, const char *name);

#endif /* !SCI_APIC_H */