Custom drivers | laravel-newsletter-go | Spatie       

 SPATIE  

  Laravel Newsletter Go 
========================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-newsletter-go](https://spatie.be/docs/laravel-newsletter-go/v1)  Custom drivers

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-newsletter-go/v1) 

  Custom drivers    
- [ Introduction ](https://spatie.be/docs/laravel-newsletter-go/v1/introduction)
- [ Installation and setup ](https://spatie.be/docs/laravel-newsletter-go/v1/installation-and-setup)
- [ Usage ](https://spatie.be/docs/laravel-newsletter-go/v1/usage)
- [ Custom drivers ](https://spatie.be/docs/laravel-newsletter-go/v1/custom-drivers)
- [ Testing ](https://spatie.be/docs/laravel-newsletter-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/laravel-newsletter-go/v1/releasing)
- [ Changelog ](https://spatie.be/docs/laravel-newsletter-go/v1/changelog)
- [ Support us ](https://spatie.be/docs/laravel-newsletter-go/v1/support-us)

 Custom drivers
==============

###  On this page 

1. [ Customizing the HTTP client ](#content-customizing-the-http-client)

Implement `newsletter.Driver` to integrate another provider. The interface receives a context on every operation:

```
type Driver interface {
    Subscribe(context.Context, string, newsletter.Subscription) (*newsletter.Member, error)
    SubscribeOrUpdate(context.Context, string, newsletter.Subscription) (*newsletter.Member, error)
    Unsubscribe(context.Context, string, string) (*newsletter.Member, error)
    Delete(context.Context, string, string) error
    GetMember(context.Context, string, string) (*newsletter.Member, error)
}
```

Return `nil, nil` when a lookup finds no member. Return a `Member` with the original provider fields in `Data` and the correct `Subscribed` state. The service uses `GetMember` for `HasMember` and `IsSubscribed`.

Set the selected driver explicitly:

```
newsletter.New(func(cfg *newsletter.Config) {
    cfg.Driver = myDriver
})
```

Or construct a fresh driver per application using the final configuration:

```
newsletter.New(func(cfg *newsletter.Config) {
    cfg.Factory = func(config newsletter.Config) (newsletter.Driver, error) {
        return NewMyDriver(config)
    }
})
```

`NewMyDriver` is your constructor. The factory receives environment defaults followed by all configuration callbacks. `Config.FindList(name)` resolves a named or default list, including its ID and arbitrary `List.Properties`. `Subscription.Properties` and `Subscription.Options` retain arbitrary provider-specific fields. An explicitly supplied driver takes precedence over the factory.

Use these extension points rather than registering a duplicate Laravel service. Laravel Go v0.1.0 returns the first service of a given type.

You can wrap a built-in driver with your own implementation. Call `newsletter.NewDriver(config)` to construct one without registering it. Drivers and their configuration should be immutable after registration and safe for concurrent use.

Customizing the HTTP client
-----------------------------------------------------------------------------------------------------------------------------------------

All built-in drivers use Laravel's HTTP client. Customize the authenticated client with `Config.API`:

```
newsletter.New(func(cfg *newsletter.Config) {
    cfg.API = func(client *httpclient.Client) *httpclient.Client {
        return client.Timeout(10 * time.Second).Header("X-Application", "my-app")
    }
})
```

You can configure transport, proxy, TLS, redirects, headers or request hooks. Build from the supplied client rather than `httpclient.New` so application settings and test fakes continue to apply. Every provider supports an explicit API base URL through `Config.Endpoint`.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
