$ show mautic/api-library
This repository is a PHP client library for the Mautic marketing automation REST API. It provides a clean, object-oriented interface for performing CRUD and custom operations on Mautic entities such as contacts, campaigns, emails, segments, and companies. The library is built on PSR-18 (HTTP client) and PSR-7 (HTTP messages) abstractions, making it compatible with any modern PHP HTTP client via php-http/discovery. It supports three authentication methods: OAuth 1.0a, OAuth 2.0, and BasicAuth, each handling token lifecycle and refresh automatically. The architecture follows a factory pattern: the `MauticApi` class instantiates context-specific API classes (e.g., `Contacts`, `Emails`) that extend a common `Api` base class. The base class implements generic `get()`, `getList()`, `create()`, `edit()`, `delete()`, and batch operations, while context classes add custom endpoints (e.g., `addContact()` for campaigns). A fluent `QueryBuilder` allows building complex WHERE/ORDER/SELECT clauses for list requests. The library also includes a backwards-compatibility layer that transparently falls back to deprecated API URLs when a 404 is returned. External dependencies include any PSR-18 HTTP client implementation (e.g., Guzzle, Symfony HttpClient) and a PSR-3 logger (optional). The library is designed for integration into PHP applications that need programmatic access to Mautic data. It includes a legacy web-based API tester (`apitester/`) for manual testing, which vendors Bootstrap 3.2.0 and jQuery. The test suite requires a live Mautic instance and is run in CI against temporary installations. While the core library is modern and well-structured, the apitester UI is outdated and the backwards-compatibility layer introduces some technical debt. Overall, this is a solid, production-ready API client with good separation of concerns and reusable patterns.
The LLM identified 4 files worth highlighting for reuse:
The base API class that all context-specific API classes extend. It implements generic CRUD operations (get, create, edit, delete, batch) and a flexible makeRequest() method that handles authentication, HTTP method selection, and response parsing. It also contains a backwards-compatibility layer using regex patterns to fall back to deprecated API URLs when a 404 is returned. This class is the core of the library and demonstrates a clean abstract pattern that can be reused for any REST API client.
Implements OAuth 1.0a and OAuth 2.0 authentication with full token lifecycle management, including token refresh and expiry handling. It builds signed requests using the OAuth protocol and stores tokens in session. This is a non-trivial implementation that can be extracted and reused in other PHP projects requiring OAuth integration.
A fluent query builder for constructing WHERE, ORDER, and SELECT clauses for API list requests. It supports complex nested conditions (AND/OR groups), comparison operators, and parameter binding. The companion WhereBuilder provides chainable methods like eq(), like(), between(), andX(), orX(). This component is self-contained and can be reused in any PHP project that needs to build dynamic query strings.
Wraps a PSR-7 response object and provides methods for validating HTTP status codes, decoding JSON or URL-encoded bodies, and saving zip file responses to disk. It also handles error responses by throwing appropriate exceptions. This is a clean, reusable response handler that can be adapted for any API client.