Request.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /*
  3. * This file is a part of the DiscordPHP-Http project.
  4. *
  5. * Copyright (c) 2021-present David Cole <david.cole1340@gmail.com>
  6. *
  7. * This file is subject to the MIT license that is bundled
  8. * with this source code in the LICENSE file.
  9. */
  10. namespace Discord\Http;
  11. use React\Promise\Deferred;
  12. /**
  13. * Represents an HTTP request.
  14. *
  15. * @author David Cole <david.cole1340@gmail.com>
  16. */
  17. class Request
  18. {
  19. /**
  20. * Deferred promise.
  21. *
  22. * @var Deferred
  23. */
  24. protected $deferred;
  25. /**
  26. * Request method.
  27. *
  28. * @var string
  29. */
  30. protected $method;
  31. /**
  32. * Request URL.
  33. *
  34. * @var Endpoint
  35. */
  36. protected $url;
  37. /**
  38. * Request content.
  39. *
  40. * @var string
  41. */
  42. protected $content;
  43. /**
  44. * Request headers.
  45. *
  46. * @var array
  47. */
  48. protected $headers;
  49. /**
  50. * Request constructor.
  51. *
  52. * @param Deferred $deferred
  53. * @param string $method
  54. * @param Endpoint $url
  55. * @param string $content
  56. * @param array $headers
  57. */
  58. public function __construct(Deferred $deferred, string $method, Endpoint $url, string $content, array $headers = [])
  59. {
  60. $this->deferred = $deferred;
  61. $this->method = $method;
  62. $this->url = $url;
  63. $this->content = $content;
  64. $this->headers = $headers;
  65. }
  66. /**
  67. * Gets the method.
  68. *
  69. * @return string
  70. */
  71. public function getMethod(): string
  72. {
  73. return $this->method;
  74. }
  75. /**
  76. * Gets the url.
  77. *
  78. * @return string
  79. */
  80. public function getUrl(): string
  81. {
  82. return Http::BASE_URL.'/'.$this->url;
  83. }
  84. /**
  85. * Gets the content.
  86. *
  87. * @return string
  88. */
  89. public function getContent(): string
  90. {
  91. return $this->content;
  92. }
  93. /**
  94. * Gets the headers.
  95. *
  96. * @return string
  97. */
  98. public function getHeaders(): array
  99. {
  100. return $this->headers;
  101. }
  102. /**
  103. * Returns the deferred promise.
  104. *
  105. * @return Deferred
  106. */
  107. public function getDeferred(): Deferred
  108. {
  109. return $this->deferred;
  110. }
  111. /**
  112. * Returns the bucket ID for the request.
  113. *
  114. * @return string
  115. */
  116. public function getBucketID(): string
  117. {
  118. return $this->method.$this->url->toAbsoluteEndpoint(true);
  119. }
  120. /**
  121. * Converts the request to a user-readable string.
  122. *
  123. * @return string
  124. */
  125. public function __toString()
  126. {
  127. return 'REQ '.strtoupper($this->method).' '.$this->url;
  128. }
  129. }