Skip to content

[5.6] Allow user-defined Sparkpost endpoint#23910

Merged
taylorotwell merged 1 commit into
laravel:5.6from
Miguel-Serejo:patch-1
Apr 26, 2018
Merged

[5.6] Allow user-defined Sparkpost endpoint#23910
taylorotwell merged 1 commit into
laravel:5.6from
Miguel-Serejo:patch-1

Conversation

@Miguel-Serejo

Copy link
Copy Markdown

Fixes #23864.

This allows the user to set an endpoint when using the Sparkpost transport by setting the 'services.sparkpost.options.endpoint' configuration key.

Currently, the options array is empty by default, and not actually used in the class, but the TransportManager still passes it into the constructor.

To use this, a user must add the sparkpost.options.endpoint key in config/services.php, for example:

'sparkpost' => [
        'secret' => env('SPARKPOST_SECRET'),
        'options' => [
                'endpoint' => env('SPARKPOST_ENDPOINT');
        ]
    ],

If the config option isn't set, or if it is null (default return from the env() helper), the default 'https://api.sparkpost.com/api/v1/transmissions' endpoint is used.

This approach allows a user to modify the endpoint of an existing SparkPostTransport through setOptions(). It also allows a user to have more complex endpoint logic by simply extending the class and overriding getOptions() rather than having to override send() or the constructor.

This allows the user to set an endpoint when using the Sparkpost transport.
@taylorotwell taylorotwell merged commit 09aadce into laravel:5.6 Apr 26, 2018
$message->setBcc([]);

$response = $this->client->post('https://api.sparkpost.com/api/v1/transmissions', [
$endpoint = $this->getOptions()['endpoint'] ?? 'https://api.sparkpost.com/api/v1/transmissions';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you think it would be a breaking change if endpoint is not set in service config? https://github.com/laravel/laravel/blob/master/config/services.php#L29

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"??" does an "isset" behind the scenes. If so, there should be no issue.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did forget about that detail on my first attempt at solving this, but this approach should prevent any errors from undefined configurations.
If someone were to backport this to an older version of laravel that doesn't require PHP 7, they'd need to manually check the options. Here's a little 3v4l snippet: https://3v4l.org/No8Mm

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it should be set in service config just for the sake of making it obvious that it's customizable (which would mean moving the default endpoint from the transport class to the config array). It would save time for some developers.

@dmcbrn

dmcbrn commented Jul 9, 2018

Copy link
Copy Markdown

Can you please add this below to the Laravel 5.6 documentation - SparkPost Driver section:

'sparkpost' => [
    'secret' => env('SPARKPOST_SECRET'),
    'options' => [
        'endpoint' => env('SPARKPOST_ENDPOINT','https://api.eu.sparkpost.com/api/v1')
    ]
],

you can drop the .eu part for default value.

Not sure if there is repo for the documentation page.

@Miguel-Serejo

Copy link
Copy Markdown
Author

@dmcbrn PR submitted. Example was changed to match the style of every other example for services.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants