Skip to content

Commit 60ee92b

Browse files
committed
Add helper methods for additional HTTP methods
1 parent a7c4957 commit 60ee92b

File tree

3 files changed

+95
-25
lines changed

3 files changed

+95
-25
lines changed

src/HttpClient.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ final class HttpClient
2121
public const HTTP_VERSION_1 = '1.1';
2222
public const HTTP_VERSION_2 = '2.0';
2323

24+
public const METHOD_GET = 'GET';
25+
public const METHOD_POST = 'POST';
26+
public const METHOD_PUT = 'PUT';
27+
public const METHOD_PATCH = 'PATCH';
28+
public const METHOD_OPTIONS = 'OPTIONS';
29+
public const METHOD_DELETE = 'DELETE';
30+
2431
/** @var HttpClientInterface */
2532
private $client;
2633

src/RequestBuilder.php

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,19 @@
1414
namespace SolidWorx\ApiFy;
1515

1616
use SolidWorx\ApiFy\Exception\MissingUrlException;
17+
use SolidWorx\ApiFy\Traits\HttpMethodsTrait;
1718
use SolidWorx\ApiFy\Traits\HttpOptionsTrait;
1819
use Symfony\Component\HttpClient\ScopingHttpClient;
1920
use Symfony\Contracts\HttpClient\HttpClientInterface;
2021

2122
final class RequestBuilder
2223
{
2324
use HttpOptionsTrait;
24-
25-
public const METHOD_GET = 'GET';
26-
27-
public const METHOD_POST = 'POST';
25+
use HttpMethodsTrait;
2826

2927
/** @var string */
3028
private $url = '';
3129

32-
/** @var string */
33-
private $method = self::METHOD_GET;
34-
3530
private $options;
3631

3732
private $client;
@@ -55,24 +50,6 @@ public function url(string $url): self
5550
return $request;
5651
}
5752

58-
public function get(): RequestBuilder
59-
{
60-
return $this->method(self::METHOD_GET);
61-
}
62-
63-
public function post(): RequestBuilder
64-
{
65-
return $this->method(self::METHOD_POST);
66-
}
67-
68-
public function method(string $method): self
69-
{
70-
$request = clone $this;
71-
$request->method = \strtoupper($method);
72-
73-
return $request;
74-
}
75-
7653
public function setBaseUri(string $url): self
7754
{
7855
$builder = clone $this;

src/Traits/HttpMethodsTrait.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of SolidWorx/Apify project.
7+
*
8+
* Copyright (c) Pierre du Plessis <open-source@solidworx.co>
9+
*
10+
* This source file is subject to the MIT license that is bundled
11+
* with this source code in the file LICENSE.
12+
*/
13+
14+
namespace SolidWorx\ApiFy\Traits;
15+
16+
use Closure;
17+
use SolidWorx\ApiFy\HttpClient;
18+
use SolidWorx\ApiFy\Progress;
19+
use SolidWorx\ApiFy\RequestBuilder;
20+
use Symfony\Component\Mime\Part\DataPart;
21+
use Traversable;
22+
23+
trait HttpMethodsTrait
24+
{
25+
/** @var string */
26+
private $method = HttpClient::METHOD_GET;
27+
28+
/**
29+
* @return $this
30+
*/
31+
public function get(): self
32+
{
33+
return $this->method(HttpClient::METHOD_GET);
34+
}
35+
36+
/**
37+
* @return $this
38+
*/
39+
public function post(): self
40+
{
41+
return $this->method(HttpClient::METHOD_POST);
42+
}
43+
44+
/**
45+
* @return $this
46+
*/
47+
public function put(): self
48+
{
49+
return $this->method(HttpClient::METHOD_PUT);
50+
}
51+
52+
/**
53+
* @return $this
54+
*/
55+
public function patch(): self
56+
{
57+
return $this->method(HttpClient::METHOD_PATCH);
58+
}
59+
60+
/**
61+
* @return $this
62+
*/
63+
public function options(): self
64+
{
65+
return $this->method(HttpClient::METHOD_OPTIONS);
66+
}
67+
68+
/**
69+
* @return $this
70+
*/
71+
public function delete(): self
72+
{
73+
return $this->method(HttpClient::METHOD_DELETE);
74+
}
75+
76+
/**
77+
* @return $this
78+
*/
79+
public function method(string $method): self
80+
{
81+
$request = clone $this;
82+
$request->method = \strtoupper($method);
83+
84+
return $request;
85+
}
86+
}

0 commit comments

Comments
 (0)