Browse Source

Added authorization and bearer authorization utility methods

Jan Oris 4 years ago
parent
commit
796bd307ca
4 changed files with 46 additions and 1 deletions
  1. 5 0
      CHANGELOG.md
  2. 18 0
      README.md
  3. 1 1
      composer.json
  4. 22 0
      src/Builder.php

+ 5 - 0
CHANGELOG.md

@@ -2,6 +2,11 @@
 
 All Notable changes to `ixudra/curl` will be documented in this file
 
+## 6.21.0 - 2020-09-22
+### Added
+- withAuthorization utility method
+- withBearer utility method
+
 ## 6.20.0 - 2020-08-07
 ### Fixed
 - Data is now passed as POST parameters instead of GET for `DELETE` REQUESTS

+ 18 - 0
README.md

@@ -312,6 +312,24 @@ You can also use key-value when using the `withHeaders()` method:
         ->withHeaders( array( 'MyFirstHeader' => '123', 'MySecondHeader' => '456' ) )
         ->get();
 
+```
+
+For (bearer) authorization headers, you can also use specific utility methods:
+
+```php
+
+    use Ixudra\Curl\Facades\Curl;
+
+    // Send a GET request to: http://www.foo.com/bar with "Authorization: 123" header
+    $response = Curl::to('http://foo.com/bar')
+        ->withAuthorization('123')
+        ->get();
+
+    // Send a GET request to: http://www.foo.com/bar with "Authorization: Bearer 123" header
+    $response = Curl::to('http://foo.com/bar')
+        ->withBearer('123')
+        ->get();
+
 ```
 
  > Note that headers will override each other if you add the same header more than once.

+ 1 - 1
composer.json

@@ -1,7 +1,7 @@
 {
     "name": "ixudra/curl",
     "description": "Custom PHP Curl library for the Laravel framework - developed by Ixudra",
-    "version": "6.20.0",
+    "version": "6.21.0",
     "keywords": ["Ixudra", "Laravel", "Curl"],
     "homepage": "http://ixudra.be",
     "license": "MIT",

+ 22 - 0
src/Builder.php

@@ -260,6 +260,28 @@ class Builder {
         return $this;
     }
 
+    /**
+     * Add an HTTP Authorization header to the request
+     *
+     * @param   string $token       The authorization token that is to be added to the request
+     * @return Builder
+     */
+    public function withAuthorization($token)
+    {
+        return $this->withHeader( 'Authorization: ' . $token );
+    }
+
+    /**
+     * Add a HTTP bearer authorization header to the request
+     *
+     * @param   string $bearer      The bearer token that is to be added to the request
+     * @return Builder
+     */
+    public function withBearer($bearer)
+    {
+        return $this->withAuthorization(  'Bearer '. $bearer );
+    }
+
     /**
      * Add a content type HTTP header to the request
      *