Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
api_proxy
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
api_proxy
Commits
152f9ef2
Commit
152f9ef2
authored
2 years ago
by
m4olivei
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3272538
by m4olivei, markturnip, e0ipso: No configuration form
parent
3079f373
No related branches found
No related tags found
1 merge request
!4
Issue #3272538 Add an example module to put the class on the project page into code
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
modules/api_proxy_example/api_proxy_example.info.yml
+7
-0
7 additions, 0 deletions
modules/api_proxy_example/api_proxy_example.info.yml
modules/api_proxy_example/src/Plugin/api_proxy/Example.php
+64
-0
64 additions, 0 deletions
modules/api_proxy_example/src/Plugin/api_proxy/Example.php
with
71 additions
and
0 deletions
modules/api_proxy_example/api_proxy_example.info.yml
0 → 100644
+
7
−
0
View file @
152f9ef2
name
:
API Proxy Example
description
:
Illustrates the implementation of an HTTP API plugin.
core
:
8.x
core_version_requirement
:
^8 || ^9
type
:
module
dependencies
:
-
api_proxy:api_proxy
This diff is collapsed.
Click to expand it.
modules/api_proxy_example/src/Plugin/api_proxy/Example.php
0 → 100644
+
64
−
0
View file @
152f9ef2
<?php
namespace
Drupal\api_proxy_example\Plugin\api_proxy
;
use
Drupal\api_proxy\Plugin\api_proxy
\HttpApiCommonConfigs
;
use
Drupal\api_proxy
\Plugin\HttpApiPluginBase
;
use
Drupal\Core\Form\SubformStateInterface
;
use
Symfony\Component\HttpFoundation\Response
;
/**
* The Example API.
*
* @HttpApi(
* id = "api-slug",
* label = @Translation("Example API"),
* description = @Translation("Proxies requests to the Example API."),
* serviceUrl = "https://api.example.org/v1",
* )
*/
final
class
Example
extends
HttpApiPluginBase
{
use
HttpApiCommonConfigs
;
/**
* {@inheritdoc}
*/
public
function
addMoreConfigurationFormElements
(
array
$form
,
SubformStateInterface
$form_state
):
array
{
$form
[
'auth_token'
]
=
$this
->
authTokenConfigForm
(
$this
->
configuration
);
$form
[
'more_stuff'
]
=
[
'#type'
=>
'textfield'
,
'#title'
=>
$this
->
t
(
'Extra config'
),
'#default_value'
=>
$this
->
configuration
[
'more_stuff'
],
];
return
$form
;
}
/**
* {@inheritdoc}
*/
protected
function
calculateHeaders
(
array
$headers
):
array
{
$default_headers
=
parent
::
calculateHeaders
(
$headers
);
// Modify & add new headers. Here you can add the auth token. Refer to your
// APIs documentation for expected auth format.
return
array_merge
(
$default_headers
,
[
'authorization'
=>
[
'Basic '
.
base64_encode
(
$this
->
configuration
[
'auth_token'
]
.
':'
.
$this
->
configuration
[
'more_stuff'
])],
'accept'
=>
[
'application/json'
],
'content-type'
=>
[
'application/json'
],
]
);
}
/**
* {@inheritdoc}
*/
public
function
postprocessOutgoing
(
Response
$response
):
Response
{
// Modify the response from the API.
// A common problem is to remove the Transfer-Encoding header.
// $response->headers->remove('transfer-encoding');
return
$response
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment