-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
|
||
namespace Ivan770\HttpClient; | ||
|
||
|
||
abstract class Request | ||
{ | ||
/** | ||
* HttpClient instance | ||
* | ||
* @var HttpClient | ||
*/ | ||
protected $client; | ||
|
||
/** | ||
* Request URL | ||
* | ||
* @var string | ||
*/ | ||
protected $resource; | ||
|
||
/** | ||
* Request method | ||
* | ||
* @var string | ||
*/ | ||
protected $method = 'GET'; | ||
|
||
/** | ||
* @param HttpClient $client | ||
*/ | ||
public function __construct(HttpClient $client) | ||
{ | ||
$this->client = $client; | ||
} | ||
|
||
/** | ||
* Method getter | ||
* | ||
* @return string | ||
*/ | ||
protected function getMethod() | ||
{ | ||
return strtolower($this->method); | ||
} | ||
|
||
/** | ||
* Resource getter | ||
* | ||
* @return string | ||
*/ | ||
protected function getResource() | ||
{ | ||
return $this->resource; | ||
} | ||
|
||
/** | ||
* Attach builder properties on execution | ||
* | ||
* @param HttpClient $client | ||
*/ | ||
protected function defaultAttach(HttpClient $client) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Attach builder properties. HttpClient instance is passed into Closure | ||
* | ||
* @param \Closure $callback | ||
* @return Request | ||
*/ | ||
public function attach($callback) | ||
{ | ||
$callback($this->client); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Run request | ||
* | ||
* @return Response | ||
*/ | ||
public function execute() | ||
{ | ||
$this->defaultAttach($this->client); | ||
|
||
$method = $this->getMethod(); | ||
|
||
return $this->client->$method($this->getResource()); | ||
} | ||
|
||
/** | ||
* Run request, and retrieve response contents | ||
* | ||
* @return \Illuminate\Support\Collection|string | ||
*/ | ||
public function get() | ||
{ | ||
return $this->execute()->getContent(); | ||
} | ||
} |