elasticsearch-php create index returns \BadRequest400Exception - php

I have a fresh installation of elasticsearch 5.0.0 and elasticsearch-php . I am trying to create an index.
I run the code from this index management documentation:
$client = ClientBuilder::create()->build();
$params = [
'index' => 'my_index'
];
// Create the index
$response = $client->indices()->create($params);
and it works. I create an index successfully.
I try the next code snippet:
$client = ClientBuilder::create()->build();
$params = [
'index' => 'my_index',
'body' => [
'settings' => [
'number_of_shards' => 3,
'number_of_replicas' => 2
],
'mappings' => [
'my_type' => [
'_source' => [
'enabled' => true
],
'properties' => [
'first_name' => [
'type' => 'string',
'analyzer' => 'standard'
],
'age' => [
'type' => 'integer'
]
]
]
]
]
];
// Create the index with mappings and settings now
$response = $client->indices()->create($params);
and I get:
Elasticsearch\Common\Exceptions\BadRequest400Exception with message 'No handler found for uri [/my_index] and method [POST]'
any ideas why?
This code used to work when I used elasticsearch 2.0
EDIT: I found this question so either it is a problem with elasticsearch-php or I need to update it I guess
I am using elasticquent which I have just realized requires elasticsearch-php version <2.2 so this is what is causing the problem

Looking at the error message:
No handler found for uri [/my_index] and method [POST]
This means that your create index call is using an HTTP POST method under the hood. In previous versions (i.e. pre 5.0), the elasticsearch-php client used to create indices with an HTTP POST but since ES 5.0 only HTTP PUT is accepted to create a new index.
This change back in september made this create call compatible with ES 5.0 again.
The only possible explanation is that you have ES 5.0 installed but you don't have the 5.0 version of the elasticsearch-php client installed.
Since you're running Elasticquent which doesn't yet support ES 5, you can temporarily go around this issue by modifying the Endpoints/Indices/Create.getMethod() method to always return PUT instead of POST and your call will work again, but you might run into other incompatibilities.

Related

TYPO3 v10 - Getting feUser Object using context API in eID_include

Code that i used and need to update for V10
$this->feUser = EidUtility::initFeUser();
When using the following code (a random) controller, the context gives me the correct login feUser object.
$context = GeneralUtility::makeInstance(Context::class);
$user = $context->getAspect('frontend.user');
DebuggerUtility::var_dump($user);
When using the same code in an eID_include class No userObject is given.
Specificly in the following class
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['FileDumpEID.php']['checkFileAccess']['xxx'] = My\Class\Hooks\FileDumpHook:class
Is there a need of bootstrapping context?
Since the TYPO3\CMS\Frontend\Middleware\EidHandler middleware is executed before the TYPO3\CMS\Frontend\Middleware\FrontendUserAuthenticator middleware in the middlewares order i dont think, that this is possible.
If you need parts of the frontend handling you either can add an own middleware with depend of TYPO3\CMS\Frontend\Middleware\FrontendUserAuthenticator or use an page Object in typoscript.
I had the same problem. You may change the order of Middlewares: https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ApiOverview/RequestHandling/Index.html
I've created a new file RequestMiddlewares.php inside the "Configuration" directory of my extension:
<?php
return [
'frontend' => [
'typo3/cms-frontend/eid' => [
'disabled' => true
],
'typo3/cms-frontend/eid-new' => [
'target' => \TYPO3\CMS\Frontend\Middleware\EidHandler::class,
'after' => [
'typo3/cms-frontend/tsfe',
],
'before' => [
'typo3/cms-frontend/prepare-tsfe-rendering',
]
]
]
];
You have to flush TYPO3 and PHP Cache and check the ordering in "Configuration" backend module (select "HTTP Middlewares (PSR-15)").
With this setup it is possible to get the context property 'frontent.user'
$context = GeneralUtility::makeInstance(Context::class);
if($context->getPropertyFromAspect('frontend.user', 'isLoggedIn')) {

Post data and file together to Laravel API using Guzzle

I've two working project communicating between each other via an API built in a Laravel. So far there is only simple POST requests made with GuzzleHttp 6.
And I am currently trying to have a new POST request made from 1 to 2, which would send a couple of simple fields along with one file.
Project 1 has a form, on the form submit I handle the data and want to send them to project 2 via a POST request to this new API endpoint.
I've tried different guzzle options 'multipart', 'form_data' etc and realised they may not be combined together. Now I understood that this options are exclusive and using only "multipart" seems the way to go.
But when I send my request to Laravel no data nor file are there.
Here is the code for my request
$options = ‌[
'multipart' =>
[
[
'name' => 'data',
'contents' => '{"field_1":"Test","field_2":"Test","field_3":"Test"}',
'headers' =>
[
'Content-Type' => 'application/json',
],
],
[
'name' => 'file',
'filename' => 'test.pdf',
'Mime-Type' => 'application/pdf',
'contents' => file_get_contents($_FILEs['text_file']['temp_name']),
]
]
];
$this->client->request('POST', "api/test_post", $options)
I also gve this a try:
$options = ‌[
'multipart' =>
[
[
'name' => 'field_1',
'contents' => 'Test',
],
[
'name' => 'field_2',
'contents' => 'Test',
],
[
'name' => 'file',
'filename' => 'test.pdf',
'Mime-Type' => 'application/pdf',
'contents' => fopen($_FILEs['text_file']['temp_name'],'r'),
]
]
];
$this->client->request('POST', "api/test_post", $options)
If I look the request content on the receiving end, nothing is there. No field or file.
I've seen couples posts, some say to include headers some say not too. I kinda got lost and amd now running out of ideas.
I would expecet the infos to be as if they where form post I guess:
$request->inpust('field_1') -> 'test'
$request->inpust('field_2') -> 'test'
$request->inpust('field_3') -> 'test'
$request->file('file') -> my uploaded file
Also I should point out that I am not exactly sure how multipart/form-data works, so that might not help me.
If you can point me to the right direction, that would help a lot
Well I finally figured it out. The second example from above is the way to go also be sure to check the headers of the request and the client...
As this API has been running for quite some time and was only doing json type requests, the Client was instantiated with
$options = [
headers => [ 'Content-Type' => 'application/json']
]
Which, as stated in multiple answers across the internet, prevents Guzzle to automatically set the Content-Type depending of the request options.
In my case, removing this line made Guzzle enable to set it properly when provided with 'multipart' option.
Also, as all other requests are using the 'json' options, Guzzle also works it's magic and set 'Content-Type' => 'application/json' as well.

Elasticsearch-php 5.x - Can't put templates

Using the official elasticsearch-php client, version 5.x.
I'm trying to put a template inside my Elasticsearch instance, but it simply
doesn't return anything.
$data = [
'name' => 'testTemplate',
'body' => [
'template' => 'testTemplate-*',
'mappings' => [
'foo' => 'string',
'bar' => 'string',
],
],
];
$this->client = ClientBuilder::create()
->allowBadJSONSerialization()
->setHosts($host)
->build();
$this->client->putTemplate($data);
I must use allowBadJsonSerialization() because i'm running on PHP 5.4, but on 5.6 it does the same thing.
I don't know if it's a bug or i'm doing something wrong.
Can you help me? Thanks.
I solved this by using CURL according to documentation.
Unfortunately the library doesn't work for the 5.x branch.

The "access_key" option must be provided to use fixer.io

For the currency conversion i am using "florianv/laravel-swap": "^1.1" library. Florianv/Laravel-swap.
As Fixer.io has changed its implementation, it is necessary to pass the access_key with the request, and because of that i am getting this error: "InvalidArgumentException: The "access_key" option must be provided to use fixer.io in /var/www/project/project-files/vendor/florianv/exchanger/src/Service/Fixer.php:51".
I registered and got the access_key.
I updated the library using composer and now i can see three constants in the vendor/florianv/exchanger/src/Service/Fixer.php.
const ACCESS_KEY_OPTION = 'access_key';
const LATEST_URL = 'http://data.fixer.io/api/latest?base=%s&access_key=%s';
const HISTORICAL_URL = 'http://data.fixer.io/api/%s?base=%s&access_key=%s';
To pass the access key i tried this:
I have a swap.php in config folder which looks something like this:
return [
'options' => [
'cache_ttl' => 86400, // 24 hours.
'cache_key_prefix' => 'currency_rate'
],
'services' => [
'fixer' => true,
],
'currency_layer' => [
'access_key' => 'asdfas7832mw3nsdfa776as8dfa', // Your app id
'enterprise' => true, // True if your AppId is an enterprise one
],
'cache' => env('CACHE_DRIVER', 'file'),
'http_client' => null,
'request_factory' => null,
'cache_item_pool' => null,
];
This had one more option which was commented, i enabled and passed the access_key in it but it doesn't work.
I also added it in services block below 'fixer => true'.
'currency_layer' => [
'access_key' => 'asdfas7832mw3nsdfa776as8dfa'
]
Also in options block:
'options' => [
'cache_ttl' => 86400, // 24 hours.
'cache_key_prefix' => 'currency_rate',
'access_key'=>'7ca208e9136c5e140d6a14427bf9ed21'
],
I tried with adding access_key in config/services.php file but it also didn't work.
'fixer' => [
'access_key' => 'asdfas7832mw3nsdfa776as8dfa'
],
Even i tried, adding to env file and calling from there, but no success. How do i pass the access_key, can anyone help me on this, what should be the approach.
vendor/florianv/exchanger/src/Service/Fixer.php -> don't touch the constant (that was my own error).
Pass the options-array by creating the Builder:
$options = ['access_key' => 'YourGeneratedAPIKeyAtCurrencyLayer'];
$this->exchangeSwap = (new Builder($options))
->add('fixer', $options )
->build();
I hope I could help ;-)

Using the Craft CMS Element API V1

I am trying to use the Craft CMS Element API. Due to an older version of PHP I am using version 1 (there is a version 1 branch).
Per the installation instructions I:
1) Uploaded the elementapi/ folder to my craft/plugins/ folder:
2) Went to Settings > Plugins from my Craft control panel and enabled the Element API plugin:
I then followed the setup instructions and created a new elementapi.php file within my craft/config/ folder:
I currently just have the following in my elementapi.php file:
<?php
namespace Craft;
return [
'endpoints' => [
'api/news.json' => [
'elementType' => 'Entry',
'criteria' => ['section' => 'news'],
'transformer' => function(EntryModel $entry) {
return [
'title' => $entry->title,
'url' => $entry->url,
'jsonUrl' => UrlHelper::getUrl("news/{$entry->id}.json"),
'summary' => $entry->summary,
];
},
],
]
];
I tried navigating to http://myUrl/api/news.json, but received the following error: The requested URL /api/news.json was not found on this server.
Any ideas what I may be missing or how I can debug this?

Categories