Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
assignments_hootsuite
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
assignments_hootsuite
Commits
430dbab1
Commit
430dbab1
authored
6 months ago
by
Guillaume Pacilly
Browse files
Options
Downloads
Patches
Plain Diff
Replace config by state for hootsuite tokens
parent
f5d80726
No related branches found
No related tags found
1 merge request
!9
Replace config by state for hootsuite tokens
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
assignments_hootsuite.install
+20
-0
20 additions, 0 deletions
assignments_hootsuite.install
assignments_hootsuite.services.yml
+1
-1
1 addition, 1 deletion
assignments_hootsuite.services.yml
src/Service/HootsuiteAPIClient.php
+22
-14
22 additions, 14 deletions
src/Service/HootsuiteAPIClient.php
with
43 additions
and
15 deletions
assignments_hootsuite.install
0 → 100644
+
20
−
0
View file @
430dbab1
<?php
/**
* @file
* Contains install and update functions for the assignments_hootsuite module.
*/
/**
* Transfer hootsuite tokens value to state.
*
* And delete the config.
*/
function
assignments_hootsuite_update_10000
()
{
$config
=
\Drupal
::
configFactory
()
->
getEditable
(
'assignments_hootsuite.tokens'
);
$state
=
\Drupal
::
state
();
$state
->
set
(
'hootsuite_access_token'
,
$config
->
get
(
'access_token'
));
$state
->
set
(
'hootsuite_refresh_token'
,
$config
->
get
(
'refresh_token'
));
// Delete the config.
$config
->
delete
();
}
This diff is collapsed.
Click to expand it.
assignments_hootsuite.services.yml
+
1
−
1
View file @
430dbab1
services
:
assignments_hootsuite.client
:
class
:
Drupal\assignments_hootsuite\Service\HootsuiteAPIClient
arguments
:
[
'
@config.factory'
,
'
@logger.factory'
,
'
@http_client'
,
'
@messenger'
]
arguments
:
[
'
@config.factory'
,
'
@logger.factory'
,
'
@http_client'
,
'
@messenger'
,
'
@state'
]
assignments_hootsuite.post_manager
:
class
:
Drupal\assignments_hootsuite\Service\HootsuitePostManager
arguments
:
[
'
@assignments_hootsuite.client'
,
'
@token'
,
'
@config.factory'
,
'
@logger.factory'
,
'
@messenger'
]
This diff is collapsed.
Click to expand it.
src/Service/HootsuiteAPIClient.php
+
22
−
14
View file @
430dbab1
...
...
@@ -6,6 +6,7 @@ use Drupal\Component\Serialization\Json;
use
Drupal\Core\Config\ConfigFactory
;
use
Drupal\Core\Logger\LoggerChannelFactoryInterface
;
use
Drupal\Core\Messenger\Messenger
;
use
Drupal\Core\State\StateInterface
;
use
Drupal\Core\StringTranslation\StringTranslationTrait
;
use
GuzzleHttp\ClientInterface
;
use
GuzzleHttp\RequestOptions
;
...
...
@@ -33,11 +34,18 @@ class HootsuiteAPIClient implements HootsuiteAPIClientInterface {
private
$config
;
/**
* Editable Tokens Config.
*
* @var \Drupal\Core\Config\Config
* The hootsuite access token.
*
* @var string
*/
private
$accessToken
;
/**
* The hootsuite refresh token.
*
* @var string
*/
private
$
config
Token
s
;
private
$
refresh
Token
;
/**
* Create a new instance.
...
...
@@ -50,15 +58,17 @@ class HootsuiteAPIClient implements HootsuiteAPIClientInterface {
* The http client.
* @param \Drupal\Core\Messenger\Messenger $messenger
* The messenger.
* @param \Drupal\Core\State\StateInterface $state
* The state.
*/
public
function
__construct
(
ConfigFactory
$config
,
LoggerChannelFactoryInterface
$loggerFactory
,
protected
ClientInterface
$httpClient
,
protected
Messenger
$messenger
,
protected
StateInterface
$state
,
)
{
$this
->
config
=
$config
->
get
(
'assignments_hootsuite.settings'
);
$this
->
configTokens
=
$config
->
getEditable
(
'assignments_hootsuite.tokens'
);
$this
->
logger
=
$loggerFactory
->
get
(
'assignments_hootsuite'
);
}
...
...
@@ -112,14 +122,13 @@ class HootsuiteAPIClient implements HootsuiteAPIClientInterface {
}
$response
=
Json
::
decode
(
$token
->
getBody
());
if
(
$token
->
getStatusCode
()
==
200
&&
isset
(
$response
[
'access_token'
]))
{
$this
->
configTokens
->
set
(
'access_token'
,
$response
[
'access_token'
]);
$this
->
configTokens
->
set
(
'refresh_token'
,
$response
[
'refresh_token'
]);
$this
->
configTokens
->
save
();
$this
->
state
->
set
(
'hootsuite_access_token'
,
$response
[
'access_token'
]);
$this
->
state
->
set
(
'hootsuite_refresh_token'
,
$response
[
'refresh_token'
]);
return
TRUE
;
}
}
elseif
(
!
empty
(
$this
->
configTokens
->
get
(
'
refresh_token'
)))
{
elseif
(
!
empty
(
$this
->
state
->
get
(
'hootsuite_
refresh_token'
)))
{
$request_options
=
[
RequestOptions
::
HEADERS
=>
[
...
...
@@ -128,7 +137,7 @@ class HootsuiteAPIClient implements HootsuiteAPIClientInterface {
'Content-Type'
=>
'application/x-www-form-urlencoded'
,
],
RequestOptions
::
FORM_PARAMS
=>
[
'refresh_token'
=>
$this
->
configTokens
->
get
(
'
refresh_token'
),
'refresh_token'
=>
$this
->
state
->
get
(
'hootsuite_
refresh_token'
),
'redirect_uri'
=>
'http'
.
(
$_SERVER
[
'HTTP_HOST'
]
?
's'
:
''
)
.
'://'
.
$_SERVER
[
'HTTP_HOST'
]
.
'/assignments_hootsuite/callback'
,
'grant_type'
=>
'refresh_token'
,
'scope'
=>
'offline'
,
...
...
@@ -145,9 +154,8 @@ class HootsuiteAPIClient implements HootsuiteAPIClientInterface {
}
$response
=
Json
::
decode
(
$token
->
getBody
());
if
(
$token
->
getStatusCode
()
==
200
&&
$response
[
'access_token'
])
{
$this
->
configTokens
->
set
(
'access_token'
,
$response
[
'access_token'
]);
$this
->
configTokens
->
set
(
'refresh_token'
,
$response
[
'refresh_token'
]);
$this
->
configTokens
->
save
();
$this
->
state
->
set
(
'hootsuite_access_token'
,
$response
[
'access_token'
]);
$this
->
state
->
set
(
'hootsuite_refresh_token'
,
$response
[
'refresh_token'
]);
return
TRUE
;
}
}
...
...
@@ -159,7 +167,7 @@ class HootsuiteAPIClient implements HootsuiteAPIClientInterface {
*/
public
function
connect
(
string
$method
,
string
$endpoint
,
string
$query
=
NULL
,
array
$body
=
NULL
):
mixed
{
$accessToken
=
$this
->
configTokens
->
get
(
'
access_token'
);
$accessToken
=
$this
->
state
->
get
(
'hootsuite_
access_token'
);
$request_options
=
[
RequestOptions
::
HEADERS
=>
[
'Authorization'
=>
'Bearer '
.
$accessToken
,
...
...
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