Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADS-4220 Add support for search requests #357

Merged
merged 13 commits into from
Nov 23, 2023
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
All notable changes to this project will be documented in this file.
This project adheres to Semantic Versioning (http://semver.org/).

### 7.2.0
* Add support for search requests

### 7.1.1
* Restore the DeleteProduct operation (from 6.2.2)
Expand Down
1 change: 1 addition & 0 deletions src/.env
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
NOSTO_SERVER_URL=connect.nosto.com
NOSTO_EMAIL_WIDGET_BASE_URL=https://connect.nosto.com
NOSTO_API_BASE_URL=https://api.nosto.com
NOSTO_SEARCH_BASE_URL=https://search.nosto.com
NOSTO_OAUTH_BASE_URL=https://my.nosto.com/oauth
NOSTO_WEB_HOOK_BASE_URL=https://my.nosto.com
NOSTO_GRAPHQL_BASE_URL=https://api.nosto.com
6 changes: 6 additions & 0 deletions src/Nosto.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class Nosto
const DEFAULT_NOSTO_OAUTH_BASE_URL = 'https://my.nosto.com/oauth';
const DEFAULT_NOSTO_API_BASE_URL = 'https://api.nosto.com';
const DEFAULT_NOSTO_GRAPHQL_BASE_URL = 'https://api.nosto.com';
const DEFAULT_NOSTO_SEARCH_BASE_URL = 'https://search.nosto.com';

const URL_PARAM_MESSAGE_TYPE = 'message_type';
const URL_PARAM_MESSAGE_CODE = 'message_code';
Expand Down Expand Up @@ -109,6 +110,11 @@ public static function getGraphqlBaseUrl()
return self::getEnvVariable('NOSTO_GRAPHQL_BASE_URL', self::DEFAULT_NOSTO_GRAPHQL_BASE_URL);
}

public static function getSearchBaseUrl()
{
return self::getEnvVariable('NOSTO_SEARCH_BASE_URL', self::DEFAULT_NOSTO_SEARCH_BASE_URL);
}

/**
* Throws a new HttpException exception with info about both the
* request and response.
Expand Down
4 changes: 2 additions & 2 deletions src/Operation/AbstractOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ abstract class AbstractOperation
* @param string|null $nostoAccount
* @param string|null $domain
* @param bool $isTokenNeeded
* @return ApiRequest|GraphqlRequest|HttpRequest
* @return ApiRequest|GraphqlRequest|SearchRequest|HttpRequest
* @throws NostoException
*/
protected function initRequest(
Expand Down Expand Up @@ -103,7 +103,7 @@ protected function initRequest(
/**
* Return type of request object
*
* @return HttpRequest|ApiRequest|GraphqlRequest
* @return HttpRequest|ApiRequest|GraphqlRequest|SearchRequest
*/
abstract protected function getRequestType();

Expand Down
149 changes: 149 additions & 0 deletions src/Operation/AbstractSearchOperation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php
/**
* Copyright (c) 2020, Nosto Solutions Ltd
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author Nosto Solutions Ltd <[email protected]>
* @copyright 2020 Nosto Solutions Ltd
* @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause
*
*/

namespace Nosto\Operation;

use Nosto\NostoException;
use Nosto\Request\Api\Token;
use Nosto\Request\Http\Exception\AbstractHttpException;
use Nosto\Request\Http\Exception\HttpResponseException;
use Nosto\Request\Graphql\SearchRequest;
use Nosto\Operation\SearchRequest as SearchQuery;
use Nosto\Types\Signup\AccountInterface;

abstract class AbstractSearchOperation extends AbstractOperation
{
/**
* @var AccountInterface Nosto configuration
*/
protected $account;

/**
* @var string active domain
*/
protected $activeDomain;

/**
* Constructor
*
* @param AccountInterface $account the account object.
* @param string $activeDomain
*/
public function __construct(AccountInterface $account, $activeDomain = '')
{
$this->account = $account;
$this->activeDomain = $activeDomain;
}

/**
* Returns the result
*
* @return mixed|null
* @throws AbstractHttpException
* @throws HttpResponseException
* @throws NostoException
*/
public function execute()
{
$request = $this->initRequest(
$this->account->getApiToken(Token::API_SEARCH),
$this->account->getName(),
);
$payload = new SearchQuery(
$this->getQuery(),
$this->getVariables()
);
$payload = $payload->getRequest();
$response = $request->postRaw(
$payload
);

return $request->getResultHandler()->parse($response);
}

/**
* Builds the recommendation API request
*
* @return string
*/
abstract public function getQuery();

/**
* @return array
*/
abstract public function getVariables();

/**
* @inheritDoc
*/
protected function initRequest(
Token $token = null,
$nostoAccount = null,
$domain = null,
$isTokenNeeded = true
) {
$request = parent::initRequest($token, $nostoAccount, $domain, false);

$request->setAuthBearer($token->getValue());

return $request;

}

/**
* @inheritdoc
*/
protected function getRequestType()
{
return new SearchRequest();
}

/**
* @inheritdoc
*/
protected function getContentType()
{
return self::CONTENT_TYPE_APPLICATION_JSON;
}

/**
* @inheritdoc
*/
protected function getPath()
{
return SearchRequest::PATH_SEARCH;
}
}
63 changes: 63 additions & 0 deletions src/Operation/SearchRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* Copyright (c) 2020, Nosto Solutions Ltd
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author Nosto Solutions Ltd <[email protected]>
* @copyright 2020 Nosto Solutions Ltd
* @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause
*
*/

namespace Nosto\Operation;

class SearchRequest
TobiasGraml11 marked this conversation as resolved.
Show resolved Hide resolved
{
/** @var array */
private $request;

/**
* GraphQLRequest constructor.
* @param string $query
* @param array $variables
*/
public function __construct($query, array $variables)
{
$this->request = ['query' => $query, 'variables' => $variables];
}

/**
* Returns the query together with the variables
*
* @return false|string
*/
public function getRequest()
{
return json_encode($this->request);
}
}
4 changes: 3 additions & 1 deletion src/Request/Api/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class Token extends AbstractObject implements ValidatableInterface
const API_EMAIL = 'email';
const API_CREATE = 'create'; // Special token related to the platform
const API_GRAPHQL = 'apps'; // Special token related to the platform
const API_SEARCH = 'search';

/**
* @var array list of valid api tokens to request from Nosto.
Expand All @@ -63,7 +64,8 @@ class Token extends AbstractObject implements ValidatableInterface
self::API_EXCHANGE_RATES,
self::API_SETTINGS,
self::API_EMAIL,
self::API_GRAPHQL
self::API_GRAPHQL,
self::API_SEARCH
];
/**
* @var string the token name, must be one of the defined tokens from self::$tokenNames.
Expand Down
56 changes: 56 additions & 0 deletions src/Request/Graphql/SearchRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Copyright (c) 2020, Nosto Solutions Ltd
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author Nosto Solutions Ltd <[email protected]>
* @copyright 2020 Nosto Solutions Ltd
* @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause
*
*/

namespace Nosto\Request\Graphql;

use Nosto\Nosto;
use Nosto\Request\Api\ApiRequest;

/**
* API request class for making API requests to Nosto.
*/
class SearchRequest extends ApiRequest
{
const PATH_SEARCH = '/v1/graphql';

/**
* @inheritdoc
*/
public function setPath($path)
{
$this->setUrl(Nosto::getSearchBaseUrl() . $path);
}
}
Loading
Loading