How to pass multiple parameters in post request in karate framework? - api

I have a JSON data file which has data like
{
"Status": "Pending",
"role": "manager",
"client": "android",
"user": "test#abc.com",
"eTyres":
{
"Wheels": {
"title": "Alloy Wheel",
"value": "Yes"
}
}
}
Firstly, I want to read this data and when Wheels.value == Yes then I want to hit an API else hit another API
Also, I would like to know how i can pass multiple parameters in post request or from the file.
Post request data is as follows:
title:Alloy_wheel__Info
part:acCooling
partTitle:AC Cooling
partValue:No
Above data i'm passing through "form-data" in postman.
Thanks in advance

Your question is hard to understand and I will assume that you want to loop over some given JSON array and do some actions. Sounds like you are not "testing" and mis-using Karate !
To loop over a JSON array, use call. Refer the docs: https://github.com/intuit/karate#data-driven-features
To do conditionals, read this part of the documentation: https://github.com/intuit/karate#conditional-logic
To do "form-data" read this: https://github.com/intuit/karate#form-field
* def data = { "Status": "Pending", "role": "manager", "client": "android", "user": "test#abc.com", "eTyres": { "Wheels": { "title": "Alloy Wheel", "value": "Yes" } } }
* eval data.eTyres.Wheels.value == 'Yes' ? karate.call('api1.feature') : karate.call('api2.feature')
How to implement api1.feature and api2.feature is a homework for you. Keep in mind that in both you will still have access to the data variable. And please read the docs and examples !

Related

Shopware 6 Admin Api - Updating existing record through patch method. Not working

shopware 6 admin api patch - why it's failing? I get error "Only single write operations are supported"
Following is api for rule-condition entity in the database, I update it with Id.
For same api get method is working!
url: api/rule-condition/dbb0d904c7c14860a9a94cf26b94eca6
method: patch
json body
[
{
"op": "replace",
"path": "/data/attributes/value/email",
"value": "test#gmail.com"
}
]
response:
{
"errors": [
{
"code": "0",
"status": "400",
"title": "Bad Request",
"detail": "Only single write operations are supported. Please send the entities one by one or use the /sync api endpoint.",
.......
I also tried changing json body to following
{
"data": {
"attributes": {
"value": {
"email": "test#gmail.com"
}
}
} }
Still it's not updating. Can somebody check and let me know what am i missing?
Documentation I followed:
https://shopware.stoplight.io/docs/admin-api/ZG9jOjEyMzA4NTQ5-writing-entities
This website has all apis and example methods. https://swagger.docs.fos.gg/,
rule-condition entity can also be found there.
Btw : I used postman for testing api
You're passing an array of objects in the request body, suggesting you want to update multiple records, but the endpoint only supports updating a single record. The correct payload in your case should look like this:
{
"value": {
"operator": "=",
"email": "test#gmail.com"
}
}
Notice that value is a json field and not only includes a single value. The exact content and the names of the properties of value depend on the type of condition used and usually it also includes the operator used in the condition.

Why to use actions in Dialogflow

I am new to Dialogflow so my question may be too simple. However, I do not understand what is the purpose of naming actions in Dialogflow. I have watched videos on youtube and people in them are using actions when they have a webhook. For example they may have an if condition in their source code
(e.g. in python
if action == 'action_name':
...
)
which executes something particular in this case.
However the json output which is retrieved by the source code has the following form:
{
"id": "123d9e8e-314f-451b-8b15-5e3b55baa980",
"timestamp": "2018-03-16T17:03:05.987Z",
"lang": "en",
"result": {
"source": "agent",
"resolvedQuery": "Hello",
"action": "input.welcome",
"actionIncomplete": false,
"parameters": {},
"contexts": [],
"metadata": {
"intentId": "effe6b2b-3372-4f89-882f-ff937b2b2abb",
"webhookUsed": "false",
"webhookForSlotFillingUsed": "false",
"intentName": "Welcome"
},
"fulfillment": {
"speech": "Hello, how can I help you?",
"messages": [
{
"type": 0,
"speech": "Hello, how can I help you?"
}
]
},
"score": 1
},
"status": {
"code": 200,
"errorType": "success",
"webhookTimedOut": false
},
"sessionId": "491d57cb-0af2-45ac-a658-9e47ec6658ce",
"alternativeResultsFromKnowledgeService": {}
}
Since the json data contains the IntentName why to bother naming an unique action for this specific intent when you can get directly the name of the intent in your json?
I tend to think of this in two ways, depending on exactly what I'm building. (Or sometimes a combination of these two ways.)
The Intent Name is a human-usable name, while the Action is something that is more intended for use by the webhook and more directly maps to a function.
Since you can have more than one Intent use the same Action, it can be convenient to map a few different ways the user may say something (and the parameters they may send along with them) to the same method. While you could do that by listing all the different Intent names in your code, it is easier to do that on the Dialogflow side.
In truth - use whatever works best for you. I tend to name my Intents and my Actions very similarly, but do branching based on what makes the most sense for the code (which sometimes also includes other values that may be sent).

How to post event with metadata to stream through HTTP API

I'm using EventStore and want to post a message (event) to it. I use the HTTP API for testing purposes. I've managed to post the event itself, with an event type specified, but I can't figure out how to specify metadata for my event. (and I must provide this metadata because my consuming application on the other side expects it).
This is what my HTTP request looks like:
Content-Type: application/json
ES-EventType: My.own.event.type
POST http://10.0.75.2:2113/web/index.html#/streams/foobar
{
"props": "andvalues"
}
Do I specify metadata in the body in through headers? I can't find much docs about this, only the official that doesn't mention it.
The documentation mentions the full schema for an event being written. It looks like this:
[
{
"eventId" : "string",
"eventType" : "string",
"data" : "object",
"metadata" : "object"
}
]
For example:
[
{
"eventId": "fbf4a1a1-b4a3-4dfe-a01f-ec52c34e16e4",
"eventType": "event-type",
"data": { "a": "1" },
"metadata": { "b": "2" }
}
]
Note that it's an array, and that you must pass content-type as application/vnd.eventstore.events+json
Check this page, scroll to Event Store Events Media Type.

CakePHP3.4: How to send a json object response?

I try to migrate to 3.4 and I have a problem to send a json object.
Up to 3.3, I used the following code:
$jsonSites = json_encode([
'reqLocation' => [
'latitude' => $latitude,
'longitude' => $longitude
],
'sites' => $sitesList,
'discoveryBooks' => $discoveryBooksList,
'deleteSites' => !empty($inDeviceSites) ? [$inDeviceSites] : [],
'deleteBooks' => !empty($inDeviceBooks) ? [$inDeviceBooks] : []
]);
$this->response->type('application/json');
$this->response->body($jsonSites);
And my client received such kind of object:
{
"reqLocation": {
"latitude": 48.080563,
"longitude": 4.4649
},
"sites": [
{
"id": 5076,
"name": "...",
"modified": "2017-01-28T03:03:23+00:00",
"directory_name": "fr/26/26120_56cc30ea4d907",
"type": "portail",
"longitude": 7.031953,
"latitude": 47.939468,
"image_0": "jpg",
"picto_color": "#FFDDDDDD",
"agthemes": [],
"distance": 131.29188575851,
"category": 1281,
"category_name": "Jardin",
"sitecategories": [
{
"id": 10,
"code": 1281,
"name_fr": "Jardin",
"_joinData": {
"id": 1876,
"site_id": 5076,
"site_category_id": 10,
"authorized": true
}
},
{
"id": 33,
"code": 1283,
"name_fr": "Jardin botanique",
"_joinData": {
"id": 5693,
"site_id": 5076,
"site_category_id": 33,
"authorized": true
}
}
]
},
],
"discoveryBooks": [],
"deleteSites": [],
"deleteBooks": []
}
So now I just replaced the deprecated Response methods like that:
EDIT: Of course I return the response object but that cannot work like done here!
$this->response->withType('application/json');
$this->response->withStringBody($jsonSites);
return $this->response;
But now my client doesn't receive anything. I also tried with postman, nothing!
What's wrong?
Check the migration guide, the new reponse methods follow the PSR-7 immutability pattern.
Request & Response Deprecations
The bulk of deprecations for 3.4 are in the Request and Response
objects. The existing methods that modify objects in-place are now
deprecated, and superseded by methods that follow the immutable object
patterns described in the PSR-7 standard.
Cookbook > 3.x Migration Guide > 3.4 Migration Guide > Request & Response Deprecations
Adopting Immutable Responses
Before you migrate your code to use the new response methods you
should be aware of the conceptual differences the new methods have.
The immutable methods are generally indicated using a with prefix. For
example, withLocation(). Because these methods operate in an immutable
context, they return new instances which you need to assign to
variables or properties. If you had controller code that looked like:
$response = $this->response;
$response->location('/login')
$response->header('X-something', 'a value');
If you were to simply find & replace method names your code would break.
Instead you must now use code that looks like:
$this->response = $this->response
->withLocation('/login')
->withHeader('X-something', 'a value');
There are a few key differences:
The result of your changes is re-assigned to $this->response. This is
critical to preserving the intent of the above code. The setter
methods can all be chained together. This allows you to skip storing
all the intermediate objects.
Cookbook > 3.x Migration Guide > 3.4 Migration Guide > Adopting Immutable Responses
Long story short, in your case you must return the new request object created by the immutable methods:
return $this->response
->withType('application/json');
->withStringBody($jsonSites);
If you wouldn't return a response object, then you'd need to reassign the new reponse to $this->response as mentioned in the above quote.

How to get track ID from URL using the SoundCloud API

How can I get a track ID from a track URL using the SoundCloud API?
You didn't specify a language in your question, so I'm just going to provide a high-level answer.
Use the resolve endpoint. I'm using this URL as an example:
https://soundcloud.com/msmrsounds/ms-mr-hurricane-chvrches-remix
HTTP GET: https://api.soundcloud.com/resolve.json?url=https%3A%2F%2Fsoundcloud.com%2Fmsmrsounds%2Fms-mr-hurricane-chvrches-remix&client_id=[your_client_id]
The resulting JSON:
{
"status": "302 - Found",
"location": "https://api.soundcloud.com/tracks/147986343.json?client_id=[YOUR client_id]"
}
That numerical value is the track_id. You can just stop here or hit that endpoint you just got back for more information:
{
"kind": "track",
"id": 90787841,
"created_at": "2013/05/05 00:15:40 +0000",
"user_id": 7853935,
"duration": 188333,
"commentable": true,
"state": "finished",
"original_content_size": 35031144,
"last_modified": "2014/09/30 05:53:10 +0000",
"sharing": "public",
"tag_list": "MSMR CHVRCHES \"Secondhand Rapture\" Remix Hurricane",
"permalink": "ms-mr-hurricane-chvrches-remix",
"streamable": true,
"embeddable_by": "all",
"downloadable": true,
"purchase_url": null,
"label_id": null,
"purchase_title": null,
"genre": "",
"title": "MS MR - Hurricane (CHVRCHES remix)",
"description": "MS MR - Hurricane (CHVRCHES remix)\r\n\r\n\r\n",
"label_name": "",
"release": "",
"track_type": "",
"key_signature": "",
"isrc": "",
"video_url": null,
"bpm": null,
"release_year": null,
"release_month": null,
"release_day": null,
"original_format": "wav",
"license": "all-rights-reserved",
"uri": "https://api.soundcloud.com/tracks/90787841",
"user": {
"id": 7853935,
"kind": "user",
"permalink": "msmrsounds",
"username": "MSMR",
"last_modified": "2014/08/04 09:03:56 +0000",
"uri": "https://api.soundcloud.com/users/7853935",
"permalink_url": "http://soundcloud.com/msmrsounds",
"avatar_url": "https://i1.sndcdn.com/avatars-000037198343-2lptmy-large.jpg?86347b7"
},
"permalink_url": "http://soundcloud.com/msmrsounds/ms-mr-hurricane-chvrches-remix",
"artwork_url": null,
"waveform_url": "https://w1.sndcdn.com/56LCadzDEBZn_m.png",
"stream_url": "https://api.soundcloud.com/tracks/90787841/stream",
"download_url": "https://api.soundcloud.com/tracks/90787841/download",
"playback_count": 1672227,
"download_count": 18494,
"favoritings_count": 20426,
"comment_count": 464,
"attachments_uri": "https://api.soundcloud.com/tracks/90787841/attachments",
"policy": "ALLOW"
}
Since the SoundCloud API is indefinitely not accepting new registrations, here's one alternative solution. This URL will provide you with a wide/short embedded player:
https://w.soundcloud.com/player/?url= + URL of track
Example: https://w.soundcloud.com/player/?url=https://soundcloud.com/chrisbjerken/picking-up-the-pieces
This works for both a single track or a playlist.
So my final code looks like this. Thank you guys for the help to get this working.
include_once('../Services/Soundcloud.php');
$client = new Services_Soundcloud('CLIENT_ID', 'CLIENT_SECRET');
$track_url=$_POST['trackurl'];
$track_url=str_replace("https://soundcloud.com/USERNAME/","",$track_url);
$track = json_decode($client->get("tracks/".$track_url));
$track_id=$track->id;
echo $track_id;
In The Meantime Writing This Answer The CLIENT_ID Is Close
Due to the high amount of requests recently received, we will no longer be processing API application requests at this time. We are working to re-evaluate our process to make it more efficient.
See Here
I Solved This Issue With My Laravel Project Like This
first I Made This Function To Get The Real Content
public function getLessonUrl($lessonId = null)
{
//Get the SoundCloud URL
$validUrl = 'https://soundcloud.com/al-hazme';
$result = stripos($lessonId, $validUrl);
if (strpos($lessonId, $validUrl) !== false) {
//Get the JSON data details with embed code from SoundCloud oEmbed
$getValues = #file_get_contents('http://soundcloud.com/oembed?format=js&url=' . $lessonId . '&color=%234274e3&auto_play=false&hide_related=true&show_comments=false&show_user=false&show_reposts=false&show_teaser=true&show_artwork');
if ($getValues === false) {
return 'حدث خطأ اثناء تحميل الدرس نرجو منك التأكد من اتصال الانترنيت لديك او حدث الصفحة';
}
//Clean the Json to decode
$decodeiFrame = substr($getValues, 1, -2);
//json decode to convert it as an array
$jsonObj = json_decode($decodeiFrame);
//echo $jsonObj->html;
$lessons = str_replace('height="400"', 'height="90"', $jsonObj->html);
return $lessons;
} else {
return 'لايوجد استماع لهذا الدرس';
}
}
Then I Made Another Function To Crap The Id Using Str Facade Methods
use Illuminate\Support\Str;
public function soundCloudId($url) {
$do = $lesson->getLessonUrl($lesson->url_link);
$getTrickId = Str::after($do,'tracks%2F');
$substr = Str::substr($getTrickId,0,9);
echo $substr = // 367893857
}
It May Help Or Give You Clue
I know this is kinda late but i faced the same issue yesterday and found a new (undocumented) endpoint that you can use.
https://api-widget.soundcloud.com/resolve?url=SOUNDLCOUD_URL&format=json&client_id=CLIENT_ID
You can find your CLIENT_ID by looking at the Network Inspector in the DevTools and select the XHR filter while browsing the SoundCloud website.
I think this is sufficient until the official APIs are usable again.