JobClient.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Overtrue\CosClient;
  3. use Overtrue\CosClient\Exceptions\InvalidConfigException;
  4. use Overtrue\CosClient\Support\XML;
  5. class JobClient extends Client
  6. {
  7. /**
  8. * @param \Overtrue\CosClient\Config|array $config
  9. *
  10. * @throws \Overtrue\CosClient\Exceptions\InvalidConfigException
  11. */
  12. public function __construct($config)
  13. {
  14. if (!($config instanceof Config)) {
  15. $config = new Config($config);
  16. }
  17. $this->validateConfig($config);
  18. parent::__construct($config);
  19. $this->setBaseUri(\sprintf(
  20. 'https://%s.cos-control.%s.myqcloud.com/',
  21. $config->get('uin'),
  22. $config->get('region')
  23. ));
  24. $this->setHeader('x-cos-appid', $config->get('app_id'));
  25. }
  26. /**
  27. * @param array $query
  28. *
  29. * @return \Overtrue\CosClient\Http\Response
  30. */
  31. public function getJobs(array $query = [])
  32. {
  33. return $this->get('/jobs', [
  34. 'query' => $query,
  35. ]);
  36. }
  37. /**
  38. * @param array $body
  39. *
  40. * @return \Overtrue\CosClient\Http\Response
  41. */
  42. public function createJob(array $body)
  43. {
  44. return $this->post('/jobs', [
  45. 'body' => XML::fromArray($body),
  46. ]);
  47. }
  48. /**
  49. * @param string $id
  50. *
  51. * @return \Overtrue\CosClient\Http\Response
  52. */
  53. public function describeJob(string $id)
  54. {
  55. return $this->get(\sprintf('/jobs/%s', $id));
  56. }
  57. /**
  58. * @param string $id
  59. * @param int $priority
  60. *
  61. * @return \Overtrue\CosClient\Http\Response
  62. */
  63. public function updateJobPriority(string $id, int $priority)
  64. {
  65. return $this->post(\sprintf('/jobs/%s/priority', $id), [
  66. 'query' => [
  67. 'priority' => $priority,
  68. ],
  69. ]);
  70. }
  71. /**
  72. * @param string $id
  73. * @param array $query
  74. *
  75. * @return \Overtrue\CosClient\Http\Response
  76. */
  77. public function updateJobStatus(string $id, array $query)
  78. {
  79. return $this->post(\sprintf('/jobs/%s/status', $id), \compact('query'));
  80. }
  81. /**
  82. * @param \Overtrue\CosClient\Config $config
  83. *
  84. * @throws \Overtrue\CosClient\Exceptions\InvalidConfigException
  85. */
  86. protected function validateConfig(Config $config)
  87. {
  88. if (!$config->has('uin')) {
  89. throw new InvalidConfigException('Invalid config uin.');
  90. }
  91. if (!$config->has('region')) {
  92. throw new InvalidConfigException('Invalid config region.');
  93. }
  94. }
  95. }