Skip to content
Snippets Groups Projects
Select Git revision
  • master
1 result

AdditionalFile.inc

Blame
  • Requests.inc 1.49 KiB
    <?php
    
    // vim: expandtab sw=4 ts=4 sts=4 :
    
    namespace WebMMB;
    
    define('MTYPE_JSON', 'application/json');
    define('MTYPE_OCTET', 'application/octet-stream');
    define('EP_ADDITIONAL_FILE', 'additional_file');
    define('EP_API', 'api');
    define('EP_AUTH', 'auth');
    define('EP_STRUCTURE', 'structure');
    define('EP_XFR', 'xfr');
    
    require_once('Exceptions.inc');
    require_once('internal/Api.inc');
    require_once('internal/HttpUtil.inc');
    
    function api_request($ch, string $url, Api\ApiRequest $req, Cookie $auth_cookie)
    {
        $resp = post_request($ch, $url, EP_API, MTYPE_JSON, $req->toJSON(), [ $auth_cookie ]);
        if (!$resp) {
            throw new ServerError('Invalid server response');
        }
    
        return $resp;
    }
    
    function auth_request($ch, string $url, Api\AuthRequest $req)
    {
        $resp = post_request($ch, $url, EP_AUTH, MTYPE_JSON, $req->toJSON());
        if (!$resp) {
            throw new ServerError('Invalid server response');
        }
    
        if ($resp->hasCookie('webmmb_auth')) {
            return $resp->getCookie('webmmb_auth');
        }
        return false;
    }
    
    function fetch_request($ch, $url, $auth_cookie)
    {
        $resp = get_request_raw($ch, $url, [ $auth_cookie ]);
        if (!$resp) {
            throw new ServerError('Invalid server response');
        }
    
        return $resp;
    }
    
    function xfr_request($ch, $url, Api\FileTransferChunk $req, $auth_cookie)
    {
        $resp = post_request($ch, $url, EP_XFR, MTYPE_OCTET, $req->toBytes(), [ $auth_cookie ]);
        if (!$resp) {
            throw new ServerError('Invalid server response');
        }
    
        return $resp;
    }