Coder Social home page Coder Social logo

smokills / laravel-http-client-default-options Goto Github PK

View Code? Open in Web Editor NEW
28.0 3.0 9.0 45 KB

Extends the native Laravel Http Client, so that you can define default options

License: MIT License

PHP 100.00%
laravel guzzle http laravel-package lumen lumen-package hacktoberfest

laravel-http-client-default-options's Introduction

Image of package

Latest Version on Packagist Build Status Total Downloads License

Set default available options to the Laravel Http Client.

Installation

Install the package via composer:

composer require smokills/laravel-http-client-default-options

Laravel

In a Laravel environment, the package will be autoregistered thanks to the Laravel Package Auto-Discovery

Lumen

If You would like use this package within a Lumen installation, you have to register the Service Provider in the app/bootstrap.php

$app->register(Smokills\Http\ServiceProvider::class);

Usage

You may define global options for the Http client in following way

// In a boot Service provider method (ex: the AppServiceProvider)

public function boot()
{
    ...

    Http::withDefaultOptions([
        'base_uri' => 'https://foo.com',
        'headers' => [
            'X-Bar-Header' => 'bar'
        ],
    ]);
}

From now on, all subsequent request will use the default options we have provided:

// Somewhere in the code

/**
 * Since we have defined the base_uri as default option, we can simply make a
 * request using only the uri.
 */
$response = Http::get('/baz');

We can still continue add other options or helpers if we need:

// Somewhere in the code

/**
 * The debug option and the basic auth will be used together the default options defined before.
 */
$response = Http::withOptions([
    'debug' => 'true'
])->withBasicAuth('username', 'password')->get('/baz');

If you need to remove several or even all of the the default options, in order to make other requests, you may use the withoutDefaultOptions method.

// Remove all of the default options...
$response = Http::withoutDefaultOptions()->get('https://bar.com');

// Remove some of the global options
$response = Http::withoutDefaultOptions([
    'option', 'another-option'
])->get('https://bar.com');

// You can pass options to remove as arguments as well
$response = Http::withoutDefaultOptions('option', 'another-option')->get('https://bar.com');

// If you would like to remove deeply nested options, you may use the the dot notation syntax
$response = Http::withoutDefaultOptions('header.X-Some-Header')->get('https://bar.com');

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.

laravel-http-client-default-options's People

Contributors

smokills avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

laravel-http-client-default-options's Issues

PHP 8 support

Hi,
Great package, any chance to have PHP 8 support?
Thanks,

How to set default options per instance?

In Guzzle I can do this:

        $this->client = new Client(
            [
                'base_uri' => 'https://www.example.com',
                'headers' => [
                    'some' => 'header',
                ],
            ]
        );

I do that in a base class common to several different subclasses that call APIs relative to this common base URL.

I'm looking at switching to the built-in Http Laravel class, and found your package while looking for how to do this. From your docs I can only see that you allow setting these options globally, across all instances of the Http client, which I don't want to do as I have other scripts that don't need those defaults, or per-request, which is messy. So I'm looking to create an instance on which I can set default properties.

I've tried using:

        $this->client = (new Http())->withOptions(
            [
                'base_uri' => 'https://www.example.com',
                'headers' => [
                    'some' => 'header',
                ],
            ]
        );

but that doesn't seem to work as it expects a request rather than a client instance. Is this possible? Is there another way? Or should I stick with native Guzzle?

it is not installing in laravel v9.x-dev

Your requirements could not be resolved to an installable set of packages.

Problem 1
- smokills/laravel-http-client-default-options[v2.0.0, ..., v2.0.2] require illuminate/http ^8.0 -> found illuminate/http[v8.0.0, ..., 8.x-dev] but these were not loaded, likely because it conflicts with another require.
- Root composer.json requires smokills/laravel-http-client-default-options ^2.0 -> satisfiable by smokills/laravel-http-client-default-options[v2.0.0, v2.0.1, v2.0.2].

You can also try re-running composer require with an explicit version constraint, e.g. "composer require smokills/laravel-http-client-default-options:*" to figure out if any version is installable, or "composer require smokills/laravel-http-client-default-options:^2.1" if you know which you need.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.