District Core Developer DocsDistrict Core Developer Docs
Developers
Boilerplate
Modules
Bitbucket
Developers
Boilerplate
Modules
Bitbucket
  • Modules

    • ABN
    • ActivityLog
    • AnalyticsApi
    • ApiConnector
    • BlockApi
    • CategoryApi
    • CloneApi
    • CommentApi
    • ContentApi
    • Core
    • Documents
    • EmbedApi
    • Event
    • ExportApi
    • FeatureApi
    • FormApi
    • GTM
    • GalleryApi
    • HelpApi
    • Hotspot
    • IdeaSurvey
    • ImportApi
    • InteractionsApi
    • Intercom
    • MailApi
    • MapApi
    • MapSurvey
    • MediaApi
    • MenuApi
    • MetaTagApi
    • NlpApi
    • NotificationApi
    • Page
    • ParentableContent
    • PaymentApi
    • PermissionsApi
    • Postcode
    • ReCaptcha
    • Redirects
    • Renderer
    • ReportApi
    • RestrictionApi
    • RevisionApi
    • SearchApi
    • Settings
    • ShareableApi
    • Slack
    • SlugApi
    • SubscribableApi
    • Survey
    • Team
    • TenantApi
    • TestApi
    • ThemeApi
    • Timeline
    • TranslationApi
    • Update
    • Users
    • VisualisationApi
    • WorkflowApi
    • Wysiwyg

ApiConnector Module

Purpose

Provide a standard approach of connecting to third party API services.

Usage

  1. Create a new connector class that extends the BaseApiConnector class that will be used to make API calls through your API client of choice (either Saloon, Guzzle or third party SDK.)
  2. In the same connector, implement the ApiConnector interface.
  3. Configure your connect in YourModule/Config/district_api_connectors.php file.
'[apiName]' => [
    'connector' => YourConnector::class, // Extends BaseApiConnector.
    /*
    |--------------------------------------------------------------------------
    | Config used by Api client to populate default settings.
    |--------------------------------------------------------------------------
    */
    BaseApiConnector::CONNECTOR_SETTINGS_KEY => [
        'someHost' => env('SOME_HOST'),
        'someUser' => env('SOME_USER'),
        'secretToken' => env('SECRET_TOKEN'),
    ],
],
  1. Optional - recommended - create a new service class (e.g ApiNameService) that will be used to call your connector to wrap common API calls. e.g
    $optionalCustomSettings = [
        BaseApiConnector::CONNECTOR_SETTINGS_KEY => [
            'someHost' => 'http://somehost.com', 
            'secretToken' => 'abc123'
        ]
    ];
    $jiraService = JiraService::new($optionalCustomSettings);
    $issue = $jiraService->getIssue($issueKey);

    /**
     * Get a specific Jira issue.
     */
    public function getIssue($issueKey): Issue
    {
        return $this->getJiraConnector()->getIssueService()->get($issueKey);
    }

Exception handling

Thanks to the trait AlwaysThrowOnErrors you can always expect an exception to be thrown when an error occurs. See https://docs.saloon.dev/the-basics/handling-failures#response-exceptions for more information.

Edit this page
Prev
AnalyticsApi
Next
BlockApi