Examples / How To

Modify User-Agent In Request Headers

4min

Introduction

The UserAgentProvider is responsible for collating and returning all configured components separated by a space character. This class is a composite provider, that can be configured via DI to inject additional providers into the constructor which are included in the User-Agent header value used in API calls.

There should be no need to replace this class with your own implementation, and doing so may break other packages which inject their own providers. Instead, create additional class(es) implementing the UserAgentProviderInterface which return the user agent information (either new components or additional information for an existing component, where supported).

Inject Additional Components

First, create a new class which implements Provider\UserAgentProviderInterface and add the execute method, returning the desired string.

If you wish to allow other modules to inject their own system information into your component, you can should implement the Provider\ComposableUserAgentProviderInterface interface, which extends the base interface. You can also use theProvider\ComposableUserAgentProviderTrait to include implementation of child providers.



Basic
Composable


Once you have your user agent provider, create a new instance of UserAgentProvider and inject it into the constructor. Note: If you are integrating into a modern framework, you should implement this via your dependency injection system

This User Agent Provider instance can then be passed into any service which interacts with Klevu's APIs

PHP


In the above scenario, the expected user agent string result would look like klevu-php-sdk/0.0.0.1 (PHP 8.1.999) MyModule/1.0.0 or, if composble klevu-php-sdk/0.0.0.1 (PHP 8.1.999) MyModule/1.0.0 (MySystem)



Source Code Reference