Skip to content

Commit

Permalink
Merge pull request #6 from weglot/fix/urls
Browse files Browse the repository at this point in the history
Fix/Url placeholder replacement
  • Loading branch information
floranpagliai authored Jul 27, 2023
2 parents d3d45b6 + 779e3bc commit 7881295
Showing 1 changed file with 19 additions and 22 deletions.
41 changes: 19 additions & 22 deletions Service/TextMasterApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\RequestOptions;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class TextMasterApi
{
Expand Down Expand Up @@ -57,7 +56,7 @@ public function __construct(string $apiKey, string $apiSecret, string $textmaste
/**
* @throws GuzzleException
*/
public function createProject(array $project): Response
public function createProject(array $project): ResponseInterface
{
$routeParams = self::ROUTES['createProject'];

Expand All @@ -67,18 +66,18 @@ public function createProject(array $project): Response
/**
* @throws GuzzleException
*/
public function updateProject(string $textMasterProjectId, array $options): Response
public function updateProject(string $textMasterProjectId, array $options): ResponseInterface
{
$routeParams = self::ROUTES['updateProject'];
$url = $this->formatUrl($routeParams['url'], ['{projectId}' => $textMasterProjectId]);

return $this->request($routeParams['url'], $routeParams['method'], ['project' => $options]);
return $this->request($url, $routeParams['method'], ['project' => $options]);
}

/**
* @throws GuzzleException
*/
public function launchProject(string $textMasterProjectId): Response
public function launchProject(string $textMasterProjectId): ResponseInterface
{
$routeParams = self::ROUTES['launchProject'];
$url = $this->formatUrl($routeParams['url'], ['{projectId}' => $textMasterProjectId]);
Expand All @@ -89,7 +88,7 @@ public function launchProject(string $textMasterProjectId): Response
/**
* @throws GuzzleException
*/
public function addDocumentsToProject(string $textMasterProjectId, array $documents): Response
public function addDocumentsToProject(string $textMasterProjectId, array $documents): ResponseInterface
{
$routeParams = self::ROUTES['addDocument'];
$url = $this->formatUrl($routeParams['url'], ['{projectId}' => $textMasterProjectId]);
Expand All @@ -100,7 +99,7 @@ public function addDocumentsToProject(string $textMasterProjectId, array $docume
/**
* @throws GuzzleException
*/
public function completeDocument(string $documentId, string $textMasterProjectId): Response
public function completeDocument(string $documentId, string $textMasterProjectId): ResponseInterface
{
$routeParams = self::ROUTES['completeDocument'];
$url = $this->formatUrl($routeParams['url'], ['{projectId}' => $textMasterProjectId, '{documentId}' => $documentId]);
Expand All @@ -111,7 +110,7 @@ public function completeDocument(string $documentId, string $textMasterProjectId
/**
* @throws GuzzleException
*/
public function setProjectOptions(string $textMasterProjectId, array $options): Response
public function setProjectOptions(string $textMasterProjectId, array $options): ResponseInterface
{
$routeParams = self::ROUTES['setOptions'];
$url = $this->formatUrl($routeParams['url'], ['{projectId}' => $textMasterProjectId]);
Expand All @@ -122,7 +121,7 @@ public function setProjectOptions(string $textMasterProjectId, array $options):
/**
* @throws GuzzleException
*/
public function getProject(string $textMasterProjectId): Response
public function getProject(string $textMasterProjectId): ResponseInterface
{
$routeParams = self::ROUTES['getProject'];
$url = $this->formatUrl($routeParams['url'], ['{projectId}' => $textMasterProjectId]);
Expand All @@ -133,7 +132,7 @@ public function getProject(string $textMasterProjectId): Response
/**
* @throws GuzzleException
*/
public function getDocument(string $textMasterProjectId, string $textMasterDocumentId): Response
public function getDocument(string $textMasterProjectId, string $textMasterDocumentId): ResponseInterface
{
$routeParams = self::ROUTES['getDocument'];
$url = $this->formatUrl($routeParams['url'], ['{projectId}' => $textMasterProjectId, '{documentId}' => $textMasterDocumentId]);
Expand All @@ -144,7 +143,7 @@ public function getDocument(string $textMasterProjectId, string $textMasterDocum
/**
* @throws GuzzleException
*/
public function getProjectQuotation(array $project): Response
public function getProjectQuotation(array $project): ResponseInterface
{
$routeParams = self::ROUTES['getProjectQuotation'];

Expand All @@ -154,18 +153,17 @@ public function getProjectQuotation(array $project): Response
/**
* @throws GuzzleException
*/
public function getCategories(): Response
public function getCategories(): ResponseInterface
{
$routeParams = self::ROUTES['getCategories'];
$url = $this->formatUrl($routeParams['url'],[]);

return $this->request($url, $routeParams['method']);
return $this->request($routeParams['url'], $routeParams['method']);
}

/**
* @throws GuzzleException
*/
public function getAbilities(string $page): Response
public function getAbilities(string $page): ResponseInterface
{
$routeParams = self::ROUTES['getAbilities'];
$url = $this->formatUrl($routeParams['url'],['{page}' => $page]);
Expand All @@ -176,15 +174,15 @@ public function getAbilities(string $page): Response
/**
* @throws GuzzleException
*/
public function getAuthorsForProject(string $textMasterProjectId, string $status = 'my_textmaster'): Response
public function getAuthorsForProject(string $textMasterProjectId, string $status = 'my_textmaster'): ResponseInterface
{
$routeParams = self::ROUTES['getAuthorsForProject'];
$url = $this->formatUrl($routeParams['url'], ['{projectId}' => $textMasterProjectId, '{status}' => $status]);

return $this->request($routeParams['url'], $routeParams['method']);
return $this->request($url, $routeParams['method']);
}

public function extractErrorFromResponse(Response $response, string $format = 'html'): string
public function extractErrorFromResponse(ResponseInterface $response, string $format = 'html'): string
{
$errorMsg = '';
$lineBreaker = 'html' === $format ? '</br>' : "\n";
Expand All @@ -205,12 +203,11 @@ public function extractErrorFromResponse(Response $response, string $format = 'h
/**
* @throws GuzzleException
*/
private function request(string $url, string $method, array $payload = []): Response
private function request(string $url, string $method, array $payload = []): ResponseInterface
{
$request = new Request($method, $url);
$response = $this->client->send($request, [RequestOptions::JSON => $payload]);

return $response;
return $this->client->send($request, [RequestOptions::JSON => $payload]);
}

private function createGuzzleClient(array $options): Client
Expand Down

0 comments on commit 7881295

Please sign in to comment.