HttpTrait.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace AlibabaCloud\Client\Traits;
  3. use AlibabaCloud\Client\Support\Arrays;
  4. use AlibabaCloud\Client\Filter\ClientFilter;
  5. use AlibabaCloud\Client\Exception\ClientException;
  6. /**
  7. * Trait HttpTrait
  8. *
  9. * @package AlibabaCloud\Client\Traits
  10. */
  11. trait HttpTrait
  12. {
  13. /**
  14. * @var array
  15. */
  16. public $options = [];
  17. /**
  18. * @param int|float $seconds
  19. *
  20. * @return $this
  21. * @throws ClientException
  22. */
  23. public function timeout($seconds)
  24. {
  25. $this->options['timeout'] = ClientFilter::timeout($seconds);
  26. return $this;
  27. }
  28. /**
  29. * @param int $milliseconds
  30. *
  31. * @return $this
  32. * @throws ClientException
  33. */
  34. public function timeoutMilliseconds($milliseconds)
  35. {
  36. ClientFilter::milliseconds($milliseconds);
  37. $seconds = $milliseconds / 1000;
  38. return $this->timeout($seconds);
  39. }
  40. /**
  41. * @param int|float $seconds
  42. *
  43. * @return $this
  44. * @throws ClientException
  45. */
  46. public function connectTimeout($seconds)
  47. {
  48. $this->options['connect_timeout'] = ClientFilter::connectTimeout($seconds);
  49. return $this;
  50. }
  51. /**
  52. * @param int $milliseconds
  53. *
  54. * @return $this
  55. * @throws ClientException
  56. */
  57. public function connectTimeoutMilliseconds($milliseconds)
  58. {
  59. ClientFilter::milliseconds($milliseconds);
  60. $seconds = $milliseconds / 1000;
  61. return $this->connectTimeout($seconds);
  62. }
  63. /**
  64. * @param bool $debug
  65. *
  66. * @return $this
  67. */
  68. public function debug($debug)
  69. {
  70. $this->options['debug'] = $debug;
  71. return $this;
  72. }
  73. /**
  74. * @codeCoverageIgnore
  75. *
  76. * @param array $cert
  77. *
  78. * @return $this
  79. */
  80. public function cert($cert)
  81. {
  82. $this->options['cert'] = $cert;
  83. return $this;
  84. }
  85. /**
  86. * @codeCoverageIgnore
  87. *
  88. * @param array|string $proxy
  89. *
  90. * @return $this
  91. */
  92. public function proxy($proxy)
  93. {
  94. $this->options['proxy'] = $proxy;
  95. return $this;
  96. }
  97. /**
  98. * @param mixed $verify
  99. *
  100. * @return $this
  101. */
  102. public function verify($verify)
  103. {
  104. $this->options['verify'] = $verify;
  105. return $this;
  106. }
  107. /**
  108. * @param array $options
  109. *
  110. * @return $this
  111. */
  112. public function options(array $options)
  113. {
  114. if ($options !== []) {
  115. $this->options = Arrays::merge([$this->options, $options]);
  116. }
  117. return $this;
  118. }
  119. }