mjydh/httpclient

v1.0.2 2022-03-23 11:03 UTC

This package is auto-updated.

Last update: 2024-09-19 19:48:08 UTC


README

仓库

https://packagist.org.cn/packages/mjydh/httpclient

包的安装,目前需要手动安装。

通过仓库选项,添加到 composer.json

"repositories": [
    {"type": "package", 
        "package": { 
            "name": "mjydh/httpclientbundle", 
            "version": "1.0.2", 
            "source": {
                "url": "https://github.com/camposgustavoj/httpclient.git",
                "type": "git",
                "reference": "main" 
            }
        }
    }
],

执行 composer require mjydh/httpclientbundle

1 - 从 https://github.com/camposgustavoj/httpclient 下载项目
2 - 在 vendor 目录下创建 mjydh 文件夹,并将下载的包放置其中。
3 - 在 composer.json 项目中的 autoload / psr-4 添加对包的引用

"autoload": {
        "psr-4": {
        "MJYDH\\HttpClientBundle\\": "vendor/mjydh/HttpClientBundle"
    },
},

4 - 执行

composer dump-autoload -o

如果无法执行 dump-autoload(如 adminformel 和 formularioelectronico 的情况),需要在 \vendor\composer\autoload_psr4.php 中添加以下行

'MJYDH\\HttpClientBundle\\' => array($vendorDir . '/mjydh/HttpClientBundle'),

5 - Symfony < 3.4 在 AppKernel.php 中添加

new MJYDH\HttpClientBundle\HttpClientBundle(),

5 - Symfony > 4 在 config/bundles.php 中添加

MJYDH\HttpClientBundle\HttpClientBundle::class=>['all'=>true]

如何实现

// Http client 
use MJYDH\HttpClientBundle\Service\HttpClient;
use MJYDH\HttpClientBundle\Exception\HttpException;
use MJYDH\HttpClientBundle\Exception\CatchExceptions;


try
{
    $http = new HttpClient();
    $http->setAuth($user, $pass); //En caso que sea por BASIC AUTH
    $http->setHeaderApiKey($keyValue, "apikey"); //En case que el auto sea por apikey
    //Se agrega al array todos los http_codes que se quieran recortar cuando se llama al Execute();
    $http->setHttpCodeResponses(array(200));
    //Se agregan todos los http_codes que tiran un CatchExceptions
    $http->setCatchExceptions(array(502=> new CatchExceptions("Titulo - Error 502", "Mensaje de error"), 
                                    0=> new CatchExceptions("Titulo - Error 502", "Mensaje de error")));

    //Ejecuta el http request y retorna un HttpResult
    $result = $http->Execute('GET', $url);      

}
} catch (CatchExceptions $cehttp) {
    return $this->showError($cehttp->getMessage(), $cehttp->getTitle()); 
}catch (HttpException $ehttp){
    return $this->showError($ehttp->getMessage(), $ehttp->getTitle()); 
}

函数

setHttpCodeResponses($codesArray)

 * Setea los http code que debe responder el execute.
 * Los http code que no esten en las lista, los maneja el execute automaticamente.
 * 
 * @param $codesArray array("http_code")

setCatchExceptions($catchExceptions)

 * Setea los http codes y sus respectivos mensajes de error que debe capturarse
 * 
 * @param $catchExceptions array("http_code" => "Mensaje de error")

项目额外的注释

版本控制 - https://semver.org/lang/es/

给定一个版本号 MAYOR.MENOR.PARCHE,增加

当对 API 进行不兼容更改时,增加 MAYOR 版本;当添加与旧版本兼容的功能时,增加 MENOR 版本;当修复与旧版本兼容的错误时,增加 PARCHE 版本。还有预发布标签和编译元数据标签作为 MAYOR.MENOR.PARCHE 格式的扩展。