How to pass parameters in https GET request for (Shopware) Rest API - api

As far as I understood, GET-Requests encode the parameters throught the url. I want to specify data that I get from the shopware REST-API (https://myshopurl/api/orders).
If I append ?limit=1, that works. But now I want to sort the results first.
The Shopware Rest API documentation says:
$params = [
'sort' => [
['property' => 'name']
]
];
$client->get('articles', $params);
or
$params = [
'sort' => [
['property' => 'orderTime'],
['property' => 'invoiceAmount', 'direction' => 'DESC']
]
];
$client->get('orders', $params);
but I am not sure how to build the URL from this information, because there are parameters within an array. Where do I have to write down the "sort" and do I have to use some brackets?
I hope somebody can help me :)

You simply need to put the filter in the url. Here is an example:
http://mydomain/api/orders?filter[0][property]=customer.email&filter[0][value]=my#domain.de
This is the exact example from here: https://developers.shopware.com/developers-guide/rest-api/#filter,-sort,-limit,-offset

Related

Magento2 Update products via async/bulk/V1/products/bySku

I have a problem with updating multiple products via API with the async/bulk/ option.
I call this URL with HTTP Method PUT: https://my-shop-domain.com/rest/employees_en/async/bulk/V1/products/bySku (employees_en is one of my store codes)
My payload before encoding to JSON is:
$body = [
0 => [
'product' => [my product data here],
'saveOptions' => true
],
1 => [
'product' => [my other product data here],
'saveOptions' => true
],
2 => ...
];
However, I always get the response: "product" is required. Enter and try again.
Which is weird, because using the same body for creating products works like expected. Does anyone of you know what is wrong here?

API Platform - Get collection where Author is User

I currently have an Offer entity which has an author property, like so :
#[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'offers')]
#[ORM\JoinColumn(nullable: false)]
private $author;
Currently, when I do a get request on /offers/, I get a collection of ALL the offers, which is normal.
I would want to only retrieve the offers where the author is the logged in user instead. (basically a findBy(['author'=>$this->getUser()]))
After searching on the internet I've been trying the following, which obviously is not working
#[ORM\Entity(repositoryClass: OfferRepository::class)]
#[ApiResource(
normalizationContext: [
'groups' => ['read'],
],
itemOperations: [
'get' => [
'security' => 'object.author == user',
]
],
collectionOperations: [
'get' => [
'security' => 'object.author == user',
]
]
)]
class Offer
{
...
This gives me the following error :
hydra:description: "Warning: Undefined property:
ApiPlatform\Core\Bridge\Doctrine\Orm\Paginator::$author"
Which tells me this is completely the wrong approach.
Kind of stuck here, any hint ?
Thank you.
Maybe a bit late but this is answered by implementing an "extension". That would allow the collection to be filtered to match only items with a specific condition (in your case, a User).
There's an old issue (here: https://github.com/api-platform/core/issues/1481).
And official documentation here: https://api-platform.com/docs/core/extensions/#example.

Amadeus API - issue with the 'Flight Inspiration Search' API in test mode

I am attempting to get the 'Flight Inspiration Search' API working (https://developers.amadeus.com/self-service/category/air/api-doc/flight-inspiration-search/api-reference) and I am receiving the following error:
{
"errors":[{
"status":500,
"code":141,
"title":"SYSTEM ERROR HAS OCCURRED",
"detail":"Featured Results option is mandatory"
}]
}
I am using the API in test mode and I am passing the required params (also using the data specified on Github: https://github.com/amadeus4dev/data-collection/blob/master/data/flightsearch.md)
Just to confirm, here is what I am passing:
$data = array(
'origin' => 'LON',
'departureDate' => '2020-10-01',
'oneWay' => "false",
'duration' => "3",
'nonStop' => "false",
'maxPrice' => "300",
'viewBy' => 'COUNTRY'
);
Please note: I had to put the boolean values in quote marks as it was showing an error when passing as a boolean value.
Where am I going wrong? Many thanks in advance.
EDIT: This is the API endpoint I am using
https://test.api.amadeus.com/v1/shopping/flight-destinations

How to send a patch api request using a variable

I am trying to update a user(s) type in the Zoom conference application using their API. I use PATCH as per their documentation, and this works when I hard code the userId in the URL, but I need to use an array variable instead because multiple users will need to be updated at once.
This code works with the manually entered userId.
The userId and bearer code are made up for the purpose of this question.
require 'vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client();
$response = $client->PATCH('https://api.zoom.us/v2/users/jkdflg4589jlmfdhw7', [
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer my token goes here',
],
'body' => json_encode([
'type' => '1',
])
]);
$body = $response->getBody() ;
$string = $body->getContents();
$json = json_decode($string);
This way the code works and changes my user's type to 1.
The following code is the one that doesn't work.
In the Zoom API reference there is a test section and the userId can be added in a tab called Settings under the field: Path Parameters.
https://marketplace.zoom.us/docs/api-reference/zoom-api/users/userupdate
Hence I can add the userId there and when I run it, it actually replaces {userId} in the URL with the actual userId into the url patch command.
Hence from this ->
PATCH https://api.zoom.us/v2/users/{userId}
It becomes this after all transformations, scripts,
and variable replacements are run.
PATCH https://api.zoom.us/v2/users/jkdflg4589jlmfdhw7
However, when I try it in my code it doesn't work, I don't know where to add the path params. I am more used to PHP but I'll use whatever I can to make it work. Also I would like userId to be a variable that may contain 1 or more userIds (array).
This is my code that doesn't work:
require 'vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client();
$response = $client->PATCH('https://api.zoom.us/v2/users/{userId}', [
'params' => [
'userId' => 'jkdflg4589jlmfdhw7',
],
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer my token goes here',
],
'body' => json_encode([
'type' => '1',
])
]);
$body = $response->getBody() ;
$string = $body->getContents();
$json = json_decode($string);
My code fails with error:
Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: `PATCH https://api.zoom.us/v2/users/%7BuserId%7D` resulted in a `404 Not Found` response: {"code":1001,"message":"User not exist: {userId}"}
in /home/.../Zoom_API_V2/guzzle_response/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113 Stack trace:
#0 /home/.../Zoom_API_V2/guzzle_response/vendor/guzzlehttp/guzzle/src/Middleware.php(66): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response))
#1 /home/.../Zoom_API_V2/guzzle_response/vendor/guzzlehttp/promises/src/Promise.php(203): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Response))
#2 /home/.../Zoom_API_V2/guzzle_response/vendor/guzzlehttp/promises/src/Promise.php(156): GuzzleHttp\Promise\Promise::callHandler(1, Object(GuzzleHttp\Psr7\Response), Array)
#3 /home/.../publ in /home/.../Zoom_API_V2/guzzle_response/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php on line 113
If I understood you correctly, then this is basic string concatenation in PHP that you are trying to do
$userId = 'jkdflg4589jlmfdhw7';
$response = $client->PATCH('https://api.zoom.us/v2/users/' . $userId, [
// other options
]);
However, when I try it in my code it doesn't work, I don't know where to add the path params.
You add URL path in the first argument, since path is part of the URL. You can however set query parameters (e.g. for GET requests) and form data (e.g. for POST form requests) through Guzzle options, but not the path.
Also I would like userId to be a variable that may contain 1 or more userIds (array).
Using just a simple implode to convert an array to a comma separated list should work, but the API point you linked to does not seem to support multiple user IDs.
$userId = ['jkdflg4589jlmfdhw7', 'asdfa123sdfasdf'];
$response = $client->PATCH('https://api.zoom.us/v2/users/' . implode(',', $userId), [
// other options
]);

How to request facets from AWS CloudSearch via the PHP v2 SDK?

I am using v2 of the PHP SDK for interfacing requests to AWS CloudSearch. The documentation is here, however details and examples are somewhat lacking. I need to include facets in my search. It is clearly asking for a string but it is not clear exactly how the string should be formatted.
$cloudSearchDomainClient->serach([
'filterQuery' => $filter,
'query' => $query,
'queryParser' => 'lucene',
'facet' => '???'
]);
I have tried, for example:
'facet' => 'field1,field2,field3'
'facet' => 'facet.fieldname={sort:'count',size:5}'
'facet' => 'fieldname={sort:'count',size:5}'
(Some permutations based on examples in the non-sdk request descriptions here)
The appropriate syntax appears to be a string representing a javascript object.
For the default sort/count:
'facet' => '{fieldname:{}}'
To specify sorting options:
'faceet' => '{fieldname:{'sort':'count',size:5}}'
To request multiple facets:
'facet' => '{field1:{},field2"{}}"
Etc.