Skip to content

Commit

Permalink
feat(Http): 允许通过 url 方法初始化服务,并通过 paramsjson 等方法链式调用
Browse files Browse the repository at this point in the history
  • Loading branch information
twinh committed Nov 2, 2023
1 parent 918f4f2 commit b6d6c0a
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
22 changes: 22 additions & 0 deletions docs/auto-completion-static.php
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,17 @@ class Gravatar

class Http
{
/**
* Set URL of the current request
*
* @param string $url
* @return $this
* @see Http::url
*/
public static function url(string $url): self
{
}

/**
* Create a new HTTP object and execute
*
Expand Down Expand Up @@ -8083,6 +8094,17 @@ class Gravatar

class Http
{
/**
* Set URL of the current request
*
* @param string $url
* @return $this
* @see Http::url
*/
public function url(string $url): self
{
}

/**
* Create a new HTTP object and execute
*
Expand Down
61 changes: 61 additions & 0 deletions lib/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,67 @@ public function toRet(array $data = [])
return $this->ret->err($data + ['code' => $e->getCode(), 'message' => $e->getMessage()]);
}

/**
* Set request method
*
* @param string $method
* @return $this
*/
public function method(string $method): self
{
$this->setMethod($method);
return $this;
}

/**
* Set the data to send to the server
*
* @param array|string $data
* @return $this
*/
public function data($data): self
{
$this->data = $data;
return $this;
}

/**
* The json data to send to the server
*
* @param array|\JsonSerializable $json
* @return $this
*/
public function json($json): self
{
$this->json = $json;
return $this;
}

/**
* Set the data append to the URL
*
* @param array|string $params
* @return $this
*/
public function params($params): self
{
$this->params = $params;
return $this;
}

/**
* Set URL of the current request
*
* @param string $url
* @return $this
* @svc
*/
protected function url(string $url): self
{
$this->url = $url;
return $this;
}

/**
* Create a new HTTP object and execute
*
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/HttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1079,4 +1079,16 @@ public function testJson()
$this->assertSame('application/json', $http['headers']['Content-Type']);
$this->assertSame('b', $http['json']['a']);
}

public function testUrl()
{
$http = Http::url('https://httpbin.org/post')
->params(['a' => 'b'])
->json(['e' => 'f'])
->method('POST')
->request();

$this->assertSame('b', $http['args']['a']);
$this->assertSame('f', $http['json']['e']);
}
}

0 comments on commit b6d6c0a

Please sign in to comment.