Skip to content

Commit

Permalink
Merge pull request #357 from Nosto/add_support_for_search_api
Browse files Browse the repository at this point in the history
ADS-4220 Add support for search requests
  • Loading branch information
TobiasGraml11 authored Nov 23, 2023
2 parents 8f14a91 + c444f95 commit 5584266
Show file tree
Hide file tree
Showing 31 changed files with 2,639 additions and 3 deletions.
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
5 changes: 3 additions & 2 deletions src/Operation/AbstractOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use Nosto\NostoException;
use Nosto\Request\Http\HttpRequest;
use Nosto\Request\Graphql\GraphqlRequest;
use Nosto\Request\Graphql\SearchRequest;
use Nosto\Result\ResultHandler;

/**
Expand Down Expand Up @@ -69,7 +70,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 +104,7 @@ protected function initRequest(
/**
* Return type of request object
*
* @return HttpRequest|ApiRequest|GraphqlRequest
* @return HttpRequest|ApiRequest|GraphqlRequest|SearchRequest
*/
abstract protected function getRequestType();

Expand Down
144 changes: 144 additions & 0 deletions src/Operation/AbstractSearchOperation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?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\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 = ['query' => $this->getQuery(), 'variables' => $this->getVariables()];
$response = $request->postRaw(
json_encode($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;
}
}
Loading

0 comments on commit 5584266

Please sign in to comment.