Skip to content

Commit

Permalink
Add request class
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan770 committed Sep 28, 2019
1 parent efe9ad3 commit c204f1f
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions src/Request.php
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();
}
}

0 comments on commit c204f1f

Please sign in to comment.