|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Superdesk Web Publisher Bridge for the Content API. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright 2015 Sourcefabric z.u. and contributors. |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please see the |
|
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* @copyright 2015 Sourcefabric z.ú. |
|
12
|
|
|
* @license http://www.superdesk.org/license |
|
13
|
|
|
*/ |
|
14
|
|
|
namespace SWP\Bundle\BridgeBundle\Controller; |
|
15
|
|
|
|
|
16
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
17
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
|
18
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
20
|
|
|
use SWP\Bundle\BridgeBundle\Client\GuzzleClient; |
|
21
|
|
|
use SWP\Bundle\BridgeBundle\Client\GuzzleApiClient; |
|
22
|
|
|
use Superdesk\ContentApiSdk\ContentApiSdk; |
|
23
|
|
|
use Superdesk\ContentApiSdk\Api\Authentication\OAuthPasswordAuthentication; |
|
24
|
|
|
use Superdesk\ContentApiSdk\Api\Request\RequestParameters; |
|
25
|
|
|
use Superdesk\ContentApiSdk\Client\ApiClientInterface; |
|
26
|
|
|
use Superdesk\ContentApiSdk\Exception\ContentApiException; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @Route("/bridge") |
|
30
|
|
|
*/ |
|
31
|
|
|
class BridgeController extends Controller |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @Route("/{endpoint}/") |
|
35
|
|
|
* @Route("/{endpoint}/{objectId}/") |
|
36
|
|
|
* @Method("GET") |
|
37
|
|
|
* |
|
38
|
|
|
* Index action for bridge controller |
|
39
|
|
|
* |
|
40
|
|
|
* @param Request $request |
|
41
|
|
|
* @param string $endpoint Endpoint of the api |
|
42
|
|
|
* @param string|null $objectId Identifier of object to retrieve |
|
43
|
|
|
* |
|
44
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
45
|
|
|
* |
|
46
|
|
|
* @throws ContentApiException |
|
47
|
|
|
*/ |
|
48
|
|
|
public function indexAction(Request $request, $endpoint, $objectId = null) |
|
49
|
|
|
{ |
|
50
|
|
|
$data = array(); |
|
51
|
|
|
$apiClient = $this->getClient(); |
|
52
|
|
|
$sdk = $this->getSDK($apiClient); |
|
53
|
|
|
|
|
54
|
|
|
$parameters = $request->query->all(); |
|
55
|
|
|
$requestParams = new RequestParameters(); |
|
56
|
|
|
$requestParams->setQueryParameterArray($parameters); |
|
57
|
|
|
$endpointPath = sprintf('/%s', $endpoint); |
|
58
|
|
|
|
|
59
|
|
|
if ($this->isValidEndpoint($endpointPath)) { |
|
60
|
|
|
throw new ContentApiException(sprintf('Endpoint %s not supported.', $endpoint)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
switch ($endpointPath) { |
|
64
|
|
|
case $sdk::SUPERDESK_ENDPOINT_ITEMS: |
|
65
|
|
|
|
|
66
|
|
|
if (!is_null($objectId)) { |
|
67
|
|
|
$data = $sdk->getItem($objectId); |
|
68
|
|
|
} else { |
|
69
|
|
|
$data = $sdk->getItems($requestParams); |
|
70
|
|
|
} |
|
71
|
|
|
break; |
|
72
|
|
|
|
|
73
|
|
|
case $sdk::SUPERDESK_ENDPOINT_PACKAGES: |
|
74
|
|
|
|
|
75
|
|
|
// TODO: Change this in the future to match the superdesk public api parameter name |
|
76
|
|
|
$resolve = (isset($parameters['resolveItems']) && $parameters['resolveItems']) ? true : false; |
|
77
|
|
|
|
|
78
|
|
|
if (!is_null($objectId)) { |
|
79
|
|
|
$data = $sdk->getPackage($objectId, $resolve); |
|
80
|
|
|
} else { |
|
81
|
|
|
$data = $sdk->getPackages($requestParams, $resolve); |
|
82
|
|
|
} |
|
83
|
|
|
break; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $this->render('SWPBridgeBundle:Default:data_dump.html.twig', array('data' => $data)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get an instance of the sdk. |
|
91
|
|
|
* |
|
92
|
|
|
* @param ApiClientInterface $apiClient Api HTTP Client |
|
93
|
|
|
* |
|
94
|
|
|
* @return ContentApiSdk |
|
95
|
|
|
*/ |
|
96
|
|
|
private function getSDK(ApiClientInterface $apiClient) |
|
97
|
|
|
{ |
|
98
|
|
|
return new ContentApiSdk( |
|
99
|
|
|
$apiClient, |
|
100
|
|
|
$this->container->getParameter('swp_bridge.api.host'), |
|
101
|
|
|
$this->container->getParameter('swp_bridge.api.port'), |
|
102
|
|
|
$this->container->getParameter('swp_bridge.api.protocol') |
|
103
|
|
|
); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Get an instance of the HTTP client. The returned class should implement |
|
108
|
|
|
* the \Superdesk\ContentApiSdk\Client\ApiClientInterface interface. |
|
109
|
|
|
* |
|
110
|
|
|
* @return GuzzleApiClient |
|
111
|
|
|
*/ |
|
112
|
|
|
private function getClient() |
|
113
|
|
|
{ |
|
114
|
|
|
$authentication = new OAuthPasswordAuthentication(new GuzzleClient()); |
|
115
|
|
|
$authentication |
|
116
|
|
|
->setClientId($this->container->getParameter('swp_bridge.auth.client_id')) |
|
117
|
|
|
->setUsername($this->container->getParameter('swp_bridge.auth.username')) |
|
118
|
|
|
->setPassword($this->container->getParameter('swp_bridge.auth.password')); |
|
119
|
|
|
|
|
120
|
|
|
$apiClient = new GuzzleApiClient(new GuzzleClient(), $authentication); |
|
121
|
|
|
$apiClient->setOptions($this->container->getParameter('swp_bridge.options')); |
|
122
|
|
|
|
|
123
|
|
|
return $apiClient; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Check if the supplied endpoint is supported by the SDK. |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $endpoint Endpoint url (/ will be automatically prepended) |
|
130
|
|
|
* |
|
131
|
|
|
* @return bool |
|
132
|
|
|
*/ |
|
133
|
|
|
private function isValidEndpoint($endpoint) |
|
134
|
|
|
{ |
|
135
|
|
|
return !in_array(sprintf('/%s', ltrim($endpoint, '/')), ContentApiSdk::getAvailableEndpoints()); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|