Skip to content

Commit

Permalink
New Moysklad API endpoint and gzip compression (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeek authored Sep 30, 2023
1 parent 09b6e0b commit f8baf02
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 26 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

Все существенные изменения в проекте будут задокументированы в этом файле. Формат основан на [Keep a Changelog](https://keepachangelog.com/), и этот проект придерживается семантического версионирования ([semver](https://semver.org/)).

## v0.9.0

### Changed

- В связи с [переездом API Моего Склада на новый домен](https://dev.moysklad.ru/doc/api/remap/1.2/#mojsklad-json-api-obschie-swedeniq-rekomendacii-po-pereezdu-na-nowyj-domen) внесены все необходимые правки: изменён url API, в заголовки добавлено gzip-сжатие. Пользователям библиотеки вносить какие-либо изменения в код в связи с этим не требуется. До 1 декабря 2023 года обновите версию библиотеки до `v0.9.0` или выше согласно [Upgrade guide](/UPGRADE.md).

## v0.8.2

### Fixed
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ $product->get();
$product = Product::make($ms, ['id' => '25cf41f2-b068-11ed-0a80-0e9700500d7e'])->get();
```

## Документация
## Полная документация

* [Настройка клиента](/docs/setup.md)
* [Взаимодействие с API](/docs/api_interaction.md)
Expand Down Expand Up @@ -78,7 +78,7 @@ Product::collection($ms)

![autocomplete](/docs/autocomplete.gif)

### Документация
### Встроенная документация

Публичные методы тщательно документированы: описание, примеры кода, ссылки на документацию API.

Expand Down
2 changes: 1 addition & 1 deletion src/Api/Query/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(ApiClient $api)
* Если $withParams === true, конструктор сохранит параметры из url, иначе - отбросит.
*
* <code>
* $productUrl = "https://online.moysklad.ru/api/remap/1.2/entity/product/3aba2611-c64f-11ed-0a80-108a00230a9c?expand=image";
* $productUrl = "https://api.moysklad.ru/api/remap/1.2/entity/product/3aba2611-c64f-11ed-0a80-108a00230a9c?expand=image";
* $product = $ms->query()
* ->fromUrl($productUrl, true)
* ->get();
Expand Down
5 changes: 4 additions & 1 deletion src/Http/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

class ApiClient
{
private array $headers = ['Content-Type' => 'application/json'];
private array $headers = [
'Content-Type' => 'application/json',
'Accept-Encoding' => 'gzip',
];

public function __construct(
array $credentials,
Expand Down
2 changes: 1 addition & 1 deletion src/Services/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

final class Url
{
public const API = 'https://online.moysklad.ru/api/remap/1.2';
public const API = 'https://api.moysklad.ru/api/remap/1.2';

public static function convertMixedValueToString(string|int|float|bool $value): string
{
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/Api/Query/ApiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ protected function makeExpectedDebug(array $path, HttpMethod $method = HttpMetho
'url_encoded' => $url,
'headers' => [
'Content-Type' => 'application/json',
'Accept-Encoding' => 'gzip',
'Authorization' => 'Bearer ' . static::TOKEN,
],
'body' => $body ?? [],
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Formatters/ArrayFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ public static function getEncodedObject(): array
return [
'param' => 'test_param',
'meta' => [
'href' => 'https://online.moysklad.ru/api/remap/1.2/endpoint/segment',
'href' => 'https://api.moysklad.ru/api/remap/1.2/endpoint/segment',
'type' => 'product',
],
'context' => [
'employee' => [
'meta' => [
'href' => 'https://online.moysklad.ru/api/remap/1.2/context/employee',
'href' => 'https://api.moysklad.ru/api/remap/1.2/context/employee',
'type' => 'employee',
],
],
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Formatters/MultiDecoderTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
abstract class MultiDecoderTestCase extends TestCase
{
protected const OBJECT_JSON_STRING = '{"param":"test_param","meta":{"href":' .
'"https:\/\/online.moysklad.ru\/api\/remap\/1.2\/endpoint\/segment","type":"product"},"context":' .
'{"employee":{"meta":{"href":"https:\/\/online.moysklad.ru\/api\/remap\/1.2\/context\/employee","type":"employee"}}},' .
'"https:\/\/api.moysklad.ru\/api\/remap\/1.2\/endpoint\/segment","type":"product"},"context":' .
'{"employee":{"meta":{"href":"https:\/\/api.moysklad.ru\/api\/remap\/1.2\/context\/employee","type":"employee"}}},' .
'"rows":[{"id":"id1","value":true},{"id":"id2","value":0},{"id":"id3","value":null},{"id":"id4","value":123.45}]}';
protected const ARRAYS_JSON_STRING = '[{"param":"value1","meta":"meta1"},{"param":"value2","meta":"meta2"}]';
protected const EMPTY_JSON_STRING = '';
Expand Down
18 changes: 9 additions & 9 deletions tests/Unit/Formatters/RecordFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,34 @@ class RecordFormatTest extends StdClassFormatTest
private const COMPLEX_CASE = [
[
'meta' => [
'href' => 'https://online.moysklad.ru/api/remap/1.2/entity/product',
'href' => 'https://api.moysklad.ru/api/remap/1.2/entity/product',
'type' => 'product',
],
'context' => [
'employee' => [
'meta' => [
'href' => 'https://online.moysklad.ru/api/remap/1.2/context/employee',
'href' => 'https://api.moysklad.ru/api/remap/1.2/context/employee',
'type' => 'employee',
],
],
],
'rows' => [
[
'meta' => [
'href' => 'https://online.moysklad.ru/api/remap/1.2/entity/product',
'href' => 'https://api.moysklad.ru/api/remap/1.2/entity/product',
'type' => 'product',
],
'id' => '25cf41f2-b068-11ed-0a80-0e9700500d7e',
'type' => 'unknown',
'owner' => [
'meta' => [
'href' => 'https://online.moysklad.ru/api/remap/1.2/entity/employee',
'href' => 'https://api.moysklad.ru/api/remap/1.2/entity/employee',
'type' => 'employee',
],
],
'fake_collection' => [
'meta' => [
'href' => 'https://online.moysklad.ru/api/remap/1.2/entity/fake_entity',
'href' => 'https://api.moysklad.ru/api/remap/1.2/entity/fake_entity',
'type' => 'fake_entity',
],
'rows' => [
Expand All @@ -65,7 +65,7 @@ class RecordFormatTest extends StdClassFormatTest
],
[
'meta' => [
'href' => 'https://online.moysklad.ru/api/remap/1.2/entity/fake_entity',
'href' => 'https://api.moysklad.ru/api/remap/1.2/entity/fake_entity',
'type' => 'fake_entity',
],
'id' => 'f731148b-a93d-11ed-0a80-0fba0011a6c6',
Expand Down Expand Up @@ -115,23 +115,23 @@ public static function arrayInputDataProvider(): array
[
[
'meta' => [
'href' => 'https://online.moysklad.ru/api/remap/1.2/context/employee',
'href' => 'https://api.moysklad.ru/api/remap/1.2/context/employee',
'type' => 'employee',
],
],
],
[
[[
'meta' => [
'href' => 'https://online.moysklad.ru/api/remap/1.2/context/employee',
'href' => 'https://api.moysklad.ru/api/remap/1.2/context/employee',
'type' => 'employee',
],
]],
],
[
[
'meta' => [
'href' => 'https://online.moysklad.ru/api/remap/1.2/entity/product',
'href' => 'https://api.moysklad.ru/api/remap/1.2/entity/product',
'type' => 'product',
],
'rows' => [],
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Formatters/StdClassFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ protected static function getEncodedObject(): stdClass
return (object) [
'param' => 'test_param',
'meta' => (object) [
'href' => 'https://online.moysklad.ru/api/remap/1.2/endpoint/segment',
'href' => 'https://api.moysklad.ru/api/remap/1.2/endpoint/segment',
'type' => 'product',
],
'context' => (object) [
'employee' => (object) [
'meta' => (object) [
'href' => 'https://online.moysklad.ru/api/remap/1.2/context/employee',
'href' => 'https://api.moysklad.ru/api/remap/1.2/context/employee',
'type' => 'employee',
],
],
Expand Down
9 changes: 5 additions & 4 deletions tests/Unit/Http/ApiClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public function testSendCreatesRequestAndReturnsFormattedResponse(): void
fn (RequestInterface $request) => strtolower($request->getMethod()) === 'post'
&& $request->getHeader('content-type')[0] === 'application/json'
&& $request->getUri()->getScheme() === 'https'
&& $request->getHeader('host')[0] === 'online.moysklad.ru'
&& $request->getUri()->getHost() === 'online.moysklad.ru'
&& $request->getHeader('host')[0] === 'api.moysklad.ru'
&& $request->getUri()->getHost() === 'api.moysklad.ru'
&& $request->getHeader('authorization')[0] === self::TOKEN_HEADER
&& $request->getUri()->getPath() === '/api/remap/1.2/entity/product'
&& $request->getUri()->getQuery() === http_build_query(self::PARAMS)
Expand Down Expand Up @@ -198,14 +198,15 @@ public static function limitOffsetDataProvider(): array
public function testDebug(): void
{
$payload = new Payload(HttpMethod::GET, self::PATH, self::PARAMS, self::BODY);
$url = 'https://online.moysklad.ru/api/remap/1.2/entity/product?limit=1&filter=filter_param_1=filter_value_1;filter_param_2=true';
$urlEncoded = 'https://online.moysklad.ru/api/remap/1.2/entity/product?limit=1&filter=filter_param_1%3Dfilter_value_1%3Bfilter_param_2%3Dtrue';
$url = 'https://api.moysklad.ru/api/remap/1.2/entity/product?limit=1&filter=filter_param_1=filter_value_1;filter_param_2=true';
$urlEncoded = 'https://api.moysklad.ru/api/remap/1.2/entity/product?limit=1&filter=filter_param_1%3Dfilter_value_1%3Bfilter_param_2%3Dtrue';
$expected = [
'method' => 'GET',
'url' => $url,
'url_encoded' => $urlEncoded,
'headers' => [
'Content-Type' => 'application/json',
'Accept-Encoding' => 'gzip',
'Authorization' => self::TOKEN_HEADER,
],
'body' => self::BODY,
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Http/GuzzleSenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testSenderUsesPassedClient(): void
->with($this->callback(
fn (RequestInterface $request) => $request->getMethod() === 'GET'
&& $request->getUri()->getScheme() === 'https'
&& $request->getUri()->getHost() === 'online.moysklad.ru'
&& $request->getUri()->getHost() === 'api.moysklad.ru'
&& $request->getUri()->getPath() === '/api/remap/1.2'
&& $request->getUri()->getQuery() === ''
&& $request->getUri()->getFragment() === ''
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Services/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/** @covers \Evgeek\Moysklad\Services\Url */
class UrlTest extends TestCase
{
private const API_URL = 'https://online.moysklad.ru/api/remap/1.2';
private const API_URL = 'https://api.moysklad.ru/api/remap/1.2';
private const GUID1 = '25cf41f2-b068-11ed-0a80-0e9700500d7e';
private const GUID2 = 'f731148b-a93d-11ed-0a80-0fba0011a6c6';

Expand Down

0 comments on commit f8baf02

Please sign in to comment.