i can't nested add in elasticsearch - php

i have a problem with elasticsearch
when i send request for add nested object with elasticseach script in postman it work but when i wanna send request for add new object in elasticseach , All objects in my index disappear.
it not work , this eliminates all other objects in elasticsearch and only add itself:
$params = [
'script' => [
'source' => 'ctx._source.products.add(params.products)',
'lang'=>'painless',
'params' =>['id'=>1,'name'=>'john']
]
];
$http = new Client();
$result = $http->request('post','http://my_server/new_frotel/_update/123', [
'body' =>$params,
'headers'=>[
'content-type'=>'application/json'
]
])
but it work in postman
POST : http://my_server/new_frotel/_update/123
{
"script": {
"source": "ctx._source.products.add(params.products)",
"lang": "painless",
"params": {
"products": {
"id":1,
"name":"john"
}
}
}
}

Related

Unable to POST request using guzzle with the Amadeus API

Description
I am trying to integrate Amadeus Self-Service API within the Laravel Environment. I am successfully able to get content by GET request, but I am not able to get content by the POST request. I have set the exceptions to display the errors thrown by the guzzle in specific.
Here is the api reference, which has the data and the endpoint which I want to post to.
https://developers.amadeus.com/self-service/category/air/api-doc/flight-offers-search/api-reference
How to reproduce
This is the method which I call from my Client.php and pass the data through by calling the POST method.
public function __construct() {
throw_if(static::$instance, 'There should be only one instance of this class');
static::$instance = $this;
$this->client = new Client([
'base_uri' => 'https://test.api.amadeus.com/',
]);
}
public function get($uri, array $options = []) {
$this->authenticate();
return $this->client->request('GET', $uri, [
$options,
'headers' => [
'Authorization' => 'Bearer '.$this->access_token,
],
]);
}
public function post($uri, array $options = []) {
$this->authenticate();
return $this->client->request('POST', $uri, [
$options,
'headers' => [
'Authorization' => 'Bearer '.$this->access_token,
],
]);
}
After calling the POST method, I pass the 'X-HTTP-Method-Override' as 'GET', and pass the data as body.
$requests_response = $client->post('v2/shopping/flight-offers', [
'headers' => [
'X-HTTP-Method-Override' => 'GET',
],
'body' => [
[
"currencyCode" => "USD",
"originDestinations" => [
[
"id" => "1",
"originLocationCode" => "RIO",
"destinationLocationCode" => "MAD",
"departureDateTimeRange" => [
"date" => "2022-11-01",
"time" => "10:00:00",
],
],
[
"id" => "2",
"originLocationCode" => "MAD",
"destinationLocationCode" => "RIO",
"departureDateTimeRange" => [
"date" => "2022-11-05",
"time" => "17:00:00",
],
],
],
"travelers" => [
["id" => "1", "travelerType" => "ADULT"],
["id" => "2", "travelerType" => "CHILD"],
],
"sources" => ["GDS"],
"searchCriteria" => [
"maxFlightOffers" => 2,
"flightFilters" => [
"cabinRestrictions" => [
[
"cabin" => "BUSINESS",
"coverage" => "MOST_SEGMENTS",
"originDestinationIds" => ["1"],
],
],
"carrierRestrictions" => [
"excludedCarrierCodes" => ["AA", "TP", "AZ"],
],
],
],
],
],
]);
Additional context
Here is the error, which I caught in the log.
local.ERROR: Guzzle error {"response":{"GuzzleHttp\\Psr7\\Stream":"
{
\"errors\": [
{
\"code\": 38189,
\"title\": \"Internal error\",
\"detail\": \"An internal error occurred, please contact your administrator\",
\"status\": 500
}
]
}
"}}
local.ERROR: Server error: POST https://test.api.amadeus.com/v2/shopping/flight-offers resulted in a 500 Internal Server Error response:
{
"errors": [
"code": 38189,
(truncated...)
"exception":"[object] (GuzzleHttp\\Exception\\ServerException(code: 500): Server error: POST https://test.api.amadeus.com/v2/shopping/flight-offers resulted in a 500 Internal Server Error response:
"errors": [
"code": 38189,
(truncated...)
at C:\\xampp\\htdocs\\Application\\vendor\\guzzlehttp\\guzzle\\src\\Exception\\RequestException.php:113)
Please spare some time to have a look, help is really appreciated.
Do the POST calls actually work using a HTTP client such as Postman or Insomnia ?
I am noticing is that you are passing an array of $options and are nesting it inside the Guzzle options. The resulting call will look something like this:
$this->client->request('POST', $uri, [
['headers' => '...', 'body' => ['...']],
'headers' => ['...']
]);
That won't work, you are going to need to unpack them this way:
public function post($uri, array $options = []) {
$this->authenticate();
return $this->client->request('POST', $uri, [
...$options,
'headers' => [
'Authorization' => 'Bearer '.$this->access_token,
],
]);
}
Notice the dots ... to unpack the options array. Also notice that you are setting the headers key twice (once in your post method definition and once in the options parameter), so only one will actually be used (by the way why exactly are you using the X-HTTP-Method-Override header ?).
Another solution if you want to pass all header and body in the POST function parameters is this:
public function post($uri, array $options = []) {
$this->authenticate();
return $this->client->request('POST', $uri, [
'json' => $options['json'], // I would suggest 'json' because it looks like the API is looking for a JSON body, if that doesn't work go with 'body'
'headers' => [
'Authorization' => 'Bearer '.$this->access_token,
...$options['headers']
],
]);
}
Another thing you might try if this doesn't do it is using the Guzzle json option instead of body for the POST request.
when you are exploring any Amadeus Self Service API, I recommend to review the portal, because it will help you with one idea about how to make the http calls.
In your case:
https://developers.amadeus.com/self-service/category/air/api-doc/flight-offers-search/api-reference
Another help could be to review the coding examples:
https://github.com/amadeus4dev/amadeus-code-examples/blob/master/flight_offers_search/v2/post/curl/
https://github.com/amadeus4dev/amadeus-code-examples/tree/master/flight_offers_search/v2/get/curl
Maybe it's a little late but this example work for me:
$options = [
'headers' => [
'Authorization' => sprintf('Bearer %s', $this->getApiToken()),
'content-type' => 'application/vnd.amadeus+json',
'X-HTTP-Method-Override' => 'GET',
],
'body' => '{
"currencyCode": "XPF",
"originDestinations": [
{
"id": 1,
"originLocationCode": "PPT",
"originRadius": null,
"alternativeOriginsCodes": [],
"destinationLocationCode": "CDG",
"alternativeDestinationsCodes": [],
"departureDateTimeRange": {
"date": "2022-12-22",
"dateWindow": "I2D"
},
"includedConnectionPoints": [],
"excludedConnectionPoints": []
}
],
"travelers": [
{
"id": "1",
"travelerType": "ADULT",
"associatedAdultId": null
}
],
"sources": [
"GDS"
]
}'
];
try {
...
$response = $this->httpClient->post(self::AMADEUS_API_URL_FLIGHT_OFFER, $options);
$body = $response->getBody();
...
Note: don't forget the content-type, it's not very obvious at first sight in the documentation but without it doesnt work with Guzzle for me (but with insomnia no problem)
consts of the class:
private const AMADEUS_API_CLIENT_GRANT_TYPE = 'client_credentials';
private const AMADEUS_API_URL_AUTH = '/v1/security/oauth2/token';
private const AMADEUS_API_URL_FLIGHT_OFFER = '/v2/shopping/flight-offers';
Authentication:
/**
*
*/
public function authenticate()
{
if (!is_null($this->getApiToken())) {
return $this->getApiToken();
}
$options = [
'form_params' => [
'client_id' => $this->apiId, //setted in the parent construct
'client_secret' => $this->apiKey, //setted in the parent construct
'grant_type' => self::AMADEUS_API_CLIENT_GRANT_TYPE,
]
];
try {
$response = $this->httpClient->post(self::AMADEUS_API_URL_AUTH, $options);
} catch (ClientExceptionInterface $exception) {
...
}
if ($response->getStatusCode() != Response::HTTP_OK) {
throw new ApiException($errorMessage, [$response->getReasonPhrase()], $response->getStatusCode());
}
$body = $response->getBody();
//custom serializer, AmadeusAuthenticationResponse is a mapping based on Amadeus authentication response
$authenticationResponse = $this->serializer->convertSerializationToData($body->getContents(), AmadeusAuthenticationResponse::class);
$this->setApiToken($authenticationResponse->getAccessToken());
return $this->getApiToken();
}';

Trying to add contacts via ActiveCampain API (Laravel)

I´m trying to integrate the RESTFUL API of ActiveCampaing to my Laravel environment, but I haven’t been so luckier, I'm using GuzzleHttp to make the requests, this is the error image and my code:
$client = new \GuzzleHttp\Client([‘base_uri’ => ‘https://myaccount.api-us1.com/api/3/’]);
$response = $client->request('POST', 'contacts', [
'headers' => [
'Api-Token' => 'xxx',
'api_action' => 'contact_add',
],
'json' => [
'email' => 'test2021#test.com',
'first_name' => 'Julian',
'last_name' => 'Carax',
]
]);
echo $response->getStatusCode(); // 200
echo $response->getBody();
Hope you could help me! :D
you are not sending the data in correct format,
from the docs https://developers.activecampaign.com/reference#contact
{
"contact": {
"email": "johndoe#example.com",
"firstName": "John",
"lastName": "Doe",
"phone": "7223224241",
"fieldValues":[
{
"field":"1",
"value":"The Value for First Field"
},
{
"field":"6",
"value":"2008-01-20"
}
]
}
}
So create an array with key contact.
$contact["contact"] = [
"email" => "johndoe#example.com",
"firstName" => "John",
"lastName" => "Doe",
"phone" => "7223224241",
"fieldValues" => [
[
"field"=>"1",
"value"=>"The Value for First Field"
],
[
"field"=>"6",
"value"=>"2008-01-20"
]
]
];
Use try catch blocks as then you can catch your errors
try{
$client = new \GuzzleHttp\Client(["base_uri" => "https://myaccount.api-us1.com/api/3/"]);
$response = $client->request('POST', 'contacts', [
'headers' => [
'Api-Token' => 'xxx',
'api_action' => 'contact_add',
],
'json' => $contact
]);
if($response->getStatusCode() == "200" || $response->getStatusCode() == "201"){
$arrResponse = json_decode($response->getBody(),true);
}
} catch(\GuzzleHttp\Exception\ClientException $e){
$error['error'] = $e->getMessage();
if ($e->hasResponse()){
$error['response'] = $e->getResponse()->getBody()->getContents();
}
// logging the request
\Illuminate\Support\Facades\Log::error("Guzzle Exception :: ", $error);
// take other actions
} catch(Exception $e){
return response()->json(
['message' => $e->getMessage()],
method_exists($e, 'getStatusCode') ? $e->getStatusCode() : 500);
}
You can check at the API docs that the fields email, first_name, last_name are under a contact node.
So make a contact array, put these fields inside and you should be fine.
The fields for first and last name are written line firstName and lastName - camelCase, not snake_case like you did.
Official php client
You should probably use the official ActiveCampaign php api client - that would make your life easier.

How to configure Paypal in CodeIgniter

https://github.com/paypal/Checkout-PHP-SDK
I downloaded this and i have tested it with simple php but i don't know where to put these files in which folder for CodeIgniter
https://github.com/paypal/Checkout-PHP-SDK/blob/develop/samples/CaptureIntentExamples/RunAll.php
`Creating an Order
Code:
// Construct a request object and set desired parameters
// Here, OrdersCreateRequest() creates a POST request to /v2/checkout/orders
use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
$request = new OrdersCreateRequest();
$request->prefer('return=representation');
$request->body = [
"intent" => "CAPTURE",
"purchase_units" => [[
"reference_id" => "test_ref_id1",
"amount" => [
"value" => "100.00",
"currency_code" => "USD"
]
]],
"application_context" => [
"cancel_url" => "https://example.com/cancel",
"return_url" => "https://example.com/return"
]
];
try {
// Call API with your client and get a response for your call
$response = $client->execute($request);
// If call returns body in response, you can get the deserialized version from the result attribute of the response
print_r($response);
}catch (HttpException $ex) {
echo $ex->statusCode;
print_r($ex->getMessage());
}`.

Lumen / Laravel: Can't figure out how unittesting a json response works

I can't figure out how unit testing works.
I have controller which returns a JSON response
Controller.php
public function getDiscount(Request $request)
{
if (isset($request) && !empty($request)) {
return response()->json($this->discount->calculateDiscount($request->json()->all()))->getOriginalContent();
}
}
With postman, this is the result of this route:
Post by client:
{
"customer-id": "3",
"items": [
{
"product-id": "A101",
"quantity": "2",
"unit-price": "9.75",
"total": "19.50"
},
{
"product-id": "A102",
"quantity": "1",
"unit-price": "49.50",
"total": "49.50"
}
],
"total": "69.00"
}
Response by API
{
"applied_discounts": [
{
"id": 3,
"name": "Tools Discount",
"description": "Seems like you really like Tools, here's one for free!"
}
],
"discounted_items": [
{
"product-id": "A101",
"quantity": "2",
"unit-price": "9.75",
"total": "19.50",
"discounted_price": 15.6
}
],
"discounted_price": 65.1,
"original_price": "69.00"
}
Now when I try to make unit test, this is what I came up with:
public function testToolsDiscount()
{
$this->json('POST', '/discount',
[
'customer-id' => '3',
'items' => [
[
'product-id' => 'A101',
'quantity' => '2',
'unit-price' => '9.75',
'total' => '19.50'
],
[
'product-id' => 'A102',
'quantity' => '1',
'unit-price' => '49.50',
'total' => '49.50'
]
],
'total' => '69.00'
])
->seeJson(
[
'applied_discounts' => [
[
'id' => 3,
]
],
]);
}
However when I run it, this is the error I get
DiscountTest::testToolsDiscount Invalid JSON was returned from the
route. Perhaps an exception was thrown?
What am I doing wrong?
Make sure your route matches the specified '/discount' with any prefix it may have.
Define the proper route,
I recommend using action() Helper function to add url, the main benefits of this function is when you change some text or prefix in route,
Let say you change from /discount to /discounts in these case you don't need to change the route everywhere.
action('ControllerName#actionName');
It's possible your post-body needs to be an actual JSON string, rather than an associative array. It's also possible that the json() method requires a fully-qualified URL instead of a relative path. If either is the case, this solution may not actually expose the problem, you'll just have to try them and see. Otherwise, try this, it should at least offer some clues as to what's going wrong. Add the following to your unit-test class and dd() the results.
/**
* #param string $uri
* #param string $method
* #param array $body
* #param array $headers
* #param array $files
* #param array $cookies
* #return array
*/
public function callRoute(
$uri,
$method = 'GET',
array $body = [],
array $headers = [],
array $files = [],
array $cookies = []
) {
foreach ($cookies as $name => $cookie) {
$this->app->resolving(\App\Http\Middleware\EncryptCookies::class, function (\App\Http\Middleware\EncryptCookies $cookie) use ($name) {
$cookie->disableFor($name);
});
}
$uri = trim($uri, '/');
$uriParts = parse_url($uri);
//This value may be based on the APP_URL value in your .env, I'm not really sure.
$root = !array_get($uriParts, 'host') ? trim(app(\Illuminate\Http\Request::class)->root(), '/').'/' : '';
$uri = "$root$uri";
$server = $this->transformHeadersToServerVars($headers);
$response = $this->call($method, $uri, $body, $cookies, $files, $server);
$headers = $response->headers->all();
$code = $response->getStatusCode();
$json = $content = $response->getContent();
$json = json_decode($json, true);
$content = ($json && json_last_error() == JSON_ERROR_NONE) ? $json : $content;
return compact('code', 'headers', 'content');
}
I'd be willing to bet the following will expose an error message and stack trace. You'll likely have to follow it up with some other dd() statements in your target controller, or wherever else your logic lives after that error points you in the right direction:
$body = [
'customer-id' => '3',
'items' => [
[
'product-id' => 'A101',
'quantity' => '2',
'unit-price' => '9.75',
'total' => '19.50'
],
[
'product-id' => 'A102',
'quantity' => '1',
'unit-price' => '49.50',
'total' => '49.50'
]
],
'total' => '69.00'
];
$response = $this->callRoute('POST', '/discount', $body)['content'];
dd($response);

how to setup this simple Elasticsearch Mapping in Elasticsearch-PHP?

Given in the end of the question is an terminal command showing a simple Elasticsearch mapping. I need to set up this kind of mapping for an index using Elasticsearch-PHP. And I need to do this at the time when I am indexing the data.
I know how to index in Elasticsearch-PHP. It will be something like
for($i = 0; $i < 100; $i++) {
$params['body'][] = [
'index' => [
'_index' => 'my_index',
'_type' => 'my_type',
]
];
$params['body'][] = [
'my_field' => 'my_value',
'second_field' => 'some more values'
];
}
$responses = $client->bulk($params);
My question is that how will I set up a Mapping, corresponding to the particular Mapping given below in the elasticsearch-PHP format (I believe it will become an associative array, but I am not sure of further details)?
This is the example ES Mapping, that I want to convert to the format used in PHP:
PUT _template/packets
{
"template": "packets-*",
"mappings": {
"pcap_file": {
"dynamic": "false",
"properties": {
"timestamp": {
"type": "date"
},
"layers": {
"properties": {
"ip": {
"properties": {
"ip_ip_src": {
"type": "ip"
},
"ip_ip_dst": {
"type": "ip"
}
}
}
}
}
}
}
}
}
If you don't update your mapping - you don't have to put mapping each time you re-index data into elasticsearch. But if you do, of you create index with new name you can do this:
$put = [
'mappings' => [
// your mapping here
],
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://yourHost:9200/yourIndex');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($put));
curl_exec($ch);
or you can use elasticsearch package:
$params = [
'index' => 'yourIndex',
'body' => [
'mappings' => [
// your mapping here
]
]
];
$response = $client->indices()->create($params);

Categories