Promuj serwer
csb-vote-rewards / addons / amxmodx / scripting / include / easy_http.inc
128 lines 4.5 KiB Pawn Raw Permalink History
/*
 * easy_http.inc - TRIMMED copy for CSB Vote Rewards.
 *
 * Only the natives csb_vote_rewards.sma calls, copied unchanged from
 * AmxxEasyHttp 1.5.0 (amxx/scripting/include/easy_http.inc and
 * easy_http_json.inc): https://github.com/Next21Team/AmxxEasyHttp
 *
 * You do not need this file on your server. Install the AmxxEasyHttp module
 * itself (its .so or .dll in addons/amxmodx/modules) - the full include
 * ships with it. If you compile against the full include
 * instead, this plugin compiles the same.
 *
 * MIT License
 *
 * Copyright (c) 2021 Igor Minin
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#if defined _easy_http_included
    #endinput
#endif
#define _easy_http_included

#pragma reqlib easy_http
#if !defined AMXMODX_NOAUTOLOAD
    #pragma loadlib easy_http
#endif

enum EzHttpErrorCode {
    EZH_OK = 0,
    EZH_CONNECTION_FAILURE,
    EZH_EMPTY_RESPONSE,
    EZH_HOST_RESOLUTION_FAILURE,
    EZH_INTERNAL_ERROR,
    EZH_INVALID_URL_FORMAT,
    EZH_NETWORK_RECEIVE_ERROR,
    EZH_NETWORK_SEND_FAILURE,
    EZH_OPERATION_TIMEDOUT,
    EZH_PROXY_RESOLUTION_FAILURE,
    EZH_SSL_CONNECT_ERROR,
    EZH_SSL_LOCAL_CERTIFICATE_ERROR,
    EZH_SSL_REMOTE_CERTIFICATE_ERROR,
    EZH_SSL_CACERT_ERROR,
    EZH_GENERIC_SSL_ERROR,
    EZH_UNSUPPORTED_PROTOCOL,
    EZH_REQUEST_CANCELLED,
    EZH_TOO_MANY_REDIRECTS,
    EZH_UNKNOWN_ERROR = 1000,
};

enum EzHttpPluginEndBehaviour
{
    EZH_CANCEL_REQUEST = 0,
    EZH_FORGET_REQUEST,
};

enum EzJSONType
{
	EzJSONError   = -1,
	EzJSONNull    = 1,
	EzJSONString  = 2,
	EzJSONNumber  = 3,
	EzJSONObject  = 4,
	EzJSONArray   = 5,
	EzJSONBoolean = 6
};

enum EzJSON
{
	EzInvalid_JSON = -1
}

native EzHttpOptions:ezhttp_create_options(bool:auto_destroy = true);
native ezhttp_option_set_user_agent(EzHttpOptions:options_id, const user_agent[]);
native ezhttp_option_set_body(EzHttpOptions:options_id, const body[]);
native ezhttp_option_set_header(EzHttpOptions:options_id, const key[], const value[]);
native ezhttp_option_set_timeout(EzHttpOptions:options_id, timeout_ms);
native ezhttp_option_set_connect_timeout(EzHttpOptions:options_id, timeout_ms);
native ezhttp_option_set_user_data(EzHttpOptions:options_id, const data[], len);
native ezhttp_option_set_plugin_end_behaviour(
    EzHttpOptions:options_id,
    EzHttpPluginEndBehaviour:plugin_end_behaviour
);

native EzHttpRequest:ezhttp_get(
    const url[] = "",
    const on_complete[] = "",
    EzHttpOptions:options_id = EzHttpOptions:0,
    const data[] = {},
    const data_len = 0
);
native EzHttpRequest:ezhttp_post(
    const url[] = "",
    const on_complete[] = "",
    EzHttpOptions:options_id = EzHttpOptions:0,
    const data[] = {},
    const data_len = 0
);

native ezhttp_get_http_code(EzHttpRequest:request_id);
native EzJSON:ezhttp_parse_json_response(EzHttpRequest:request_id, bool:with_comments = false);
native EzHttpErrorCode:ezhttp_get_error_code(EzHttpRequest:request_id);
native ezhttp_get_error_message(EzHttpRequest:request_id, buffer[], max_len);
native ezhttp_get_user_data(EzHttpRequest:request_id, data[]);

native EzJSONType:ezjson_get_type(const EzJSON:value);
native bool:ezjson_free(&EzJSON:handle);
native ezjson_object_get_string(const EzJSON:object, const name[], buffer[], maxlen, bool:dot_not = false);
native ezjson_object_get_number(const EzJSON:object, const name[], bool:dot_not = false);
native bool:ezjson_object_get_bool(const EzJSON:object, const name[], bool:dot_not = false);
native bool:ezjson_object_has_value(const EzJSON:object, const name[], EzJSONType:type = EzJSONError, bool:dot_not = false);