Skip to content

Commit c204f1f

Browse files
committed
Add request class
1 parent efe9ad3 commit c204f1f

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

src/Request.php

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
4+
namespace Ivan770\HttpClient;
5+
6+
7+
abstract class Request
8+
{
9+
/**
10+
* HttpClient instance
11+
*
12+
* @var HttpClient
13+
*/
14+
protected $client;
15+
16+
/**
17+
* Request URL
18+
*
19+
* @var string
20+
*/
21+
protected $resource;
22+
23+
/**
24+
* Request method
25+
*
26+
* @var string
27+
*/
28+
protected $method = 'GET';
29+
30+
/**
31+
* @param HttpClient $client
32+
*/
33+
public function __construct(HttpClient $client)
34+
{
35+
$this->client = $client;
36+
}
37+
38+
/**
39+
* Method getter
40+
*
41+
* @return string
42+
*/
43+
protected function getMethod()
44+
{
45+
return strtolower($this->method);
46+
}
47+
48+
/**
49+
* Resource getter
50+
*
51+
* @return string
52+
*/
53+
protected function getResource()
54+
{
55+
return $this->resource;
56+
}
57+
58+
/**
59+
* Attach builder properties on execution
60+
*
61+
* @param HttpClient $client
62+
*/
63+
protected function defaultAttach(HttpClient $client)
64+
{
65+
//
66+
}
67+
68+
/**
69+
* Attach builder properties. HttpClient instance is passed into Closure
70+
*
71+
* @param \Closure $callback
72+
* @return Request
73+
*/
74+
public function attach($callback)
75+
{
76+
$callback($this->client);
77+
78+
return $this;
79+
}
80+
81+
/**
82+
* Run request
83+
*
84+
* @return Response
85+
*/
86+
public function execute()
87+
{
88+
$this->defaultAttach($this->client);
89+
90+
$method = $this->getMethod();
91+
92+
return $this->client->$method($this->getResource());
93+
}
94+
95+
/**
96+
* Run request, and retrieve response contents
97+
*
98+
* @return \Illuminate\Support\Collection|string
99+
*/
100+
public function get()
101+
{
102+
return $this->execute()->getContent();
103+
}
104+
}

0 commit comments

Comments
 (0)