I am trying to search videos, example here:
https://developer.vimeo.com/api/playground/videos
This is final query:
https://api.vimeo.com/videos?direction=desc&page=1&per_page=10&query=fashion&sort=likes
This returns 10 videos. However if I search using the code:
//Search for videos - https://developer.vimeo.com/api/playground/videos
$result = $vimeo->request("/videos", array(
'page'=> $page,
'per_page' => $perPage,
'fields' => 'uri,name,description,duration,width,height,privacy,pictures.sizes',
'sort' => $sort,
'direction' => $sortDirection,
'query' => $path
));
I only get 8 results back, I am not sure why.
The only thing I can see, maybe its not related is that 2 videos that are not returned have this settings:
"privacy": {
"view": "ptv",
"embed": "public",
"download": false,
"add": false,
"comments": "nobody"
},
while other have these (I dont know if this matters)
"privacy": {
"view": "anybody",
"embed": "public",
"download": false,
"add": true,
"comments": "anybody"
},
VimeoResponse vimeoResponseList = vimeo.get(VimeoConstants.BASE_URL + "videos?query=" + query + "&sort=date&direction=asc&page=" + connectionProfile.getAgentPropertiesList().get(0));
This is look like this --> https://api.vimeo.com/videos?query=asd&sort=date&direction=asc&page=5
This is for java and i use clickntap.
Related
I'm trying to fetch a collection of members through my model Agency (hasMany()).
members is related to an Agency through a hasMany() relationship, and I'm fetching them like so:
$members = $this->model->where("id", $agencyId)->firstOrFail()->members();
In my Member.php model class I'm implementing JSONSerializable with the following method:
public function jsonSerialize()
{
$agency = $this->agency()->first();
return [
'member_id' => $this->member_id,
'name' => $this->name(),
'surname' => $this->surname(),
'email' => $this->email(),
'active' => $this->active,
'agency' => [
'id' => $agency->id,
'name' => $agency->name,
]
];
}
However this seriazation is never being called when I try to fetch the members like so:
$members = $this->model->where("id", $agencyId)->firstOrFail()->members();
return $members->paginate(
$pageSize,
['*'],
'page',
$pageNumber
);
The response is:
"current_page": 1,
"data": [
{
"member_id": "0315c19f-8f5c-467a-97fd-a0422f5e14a5",
"user_id": "77b36a51-4b86-34be-9a76-856ca68c3a1e",
"agency_id": "d8c0cfab-686c-3b40-90ea-8bf52f07a3a0",
"active": true,
"deleted_at": null,
"created_at": "2020-04-30 15:20:38",
"updated_at": "2020-04-30 15:20:38"
},
{
"member_id": "f522914d-17cd-470d-a5d6-365930958a29",
"user_id": "994ca229-554e-3ae9-8459-24dc1479d75c",
"agency_id": "d8c0cfab-686c-3b40-90ea-8bf52f07a3a0",
"active": true,
"deleted_at": null,
"created_at": "2020-04-30 15:20:38",
"updated_at": "2020-04-30 15:20:38"
}
],
"first_page_url": "\/agency\/d8c0cfab-686c-3b40-90ea-8bf52f07a3a0\/members?page=1",
"from": 1,
"last_page": 1,
"last_page_url": "\/agency\/d8c0cfab-686c-3b40-90ea-8bf52f07a3a0\/members?page=1",
"next_page_url": null,
"path": "\/agency\/d8c0cfab-686c-3b40-90ea-8bf52f07a3a0\/members",
"per_page": 50,
"prev_page_url": null,
"to": 2,
"total": 2
}
Why are the timestamps etc. shown in the member data? Why isn't the jsonSerialize method being used?
I think Laravel uses the toArray() function for JSON serialization. You should look into Resources if you really want to control the output of your JSON.
https://laravel.com/docs/7.x/eloquent-resources
I want to create an API in the below format. But I am unable to do it. I am using the API resource. I have tried using different queries but I am not getting the exact solution.Please help me.
Thank you
what I have tried
$marks = StudentMarksResource::collection(StudentsMark::whereIn('academic_id',$ids)->whereIn('student_id',$studentid)->get());
my API resource file
public function toArray($request)
{
return [
'exam_type' => $this->exam_type,
'details' => [
/* 'class_id' => Course::find($this->class_id),
'batch_id' => Batch::find($this->batch_id),
'student_id' => Student::find($this->student_id), */
'subject' => $this->subject,
'marks' => $this->marks,
'marksgrade' => $this->marksgrade,
'total' => $this->total,
'grade' => $this->grade,
'percentage' => $this->percentage,
'year' => $this->year,
],
];
}
what I want is this
data": [
{
"exam_type": "Unit Test 1",
"details": {
//subject1 details
},
{
//subject2 details
},
{
//subject3 details
},
"exam_type": "Unit Test 2",
"details": {
//subject1 details
},
{
//subject2 details
},
{
//subject3 details
},
},
what I am getting for the above code
"data": [
{
"exam_type": "Unit Test 1",
"details": {
//subject1 marks details
}
},
{
"exam_type": "Unit Test 1",
"details": {
//subject2 marks details
}
},
Here is something you can try:
First get exam types
$exam_types = StudentsMark::select('exam_type', 'student_id', 'academic_id')->whereIn('academic_id',$ids)->whereIn('student_id',$studentid)->get();
$marks = StudentMarksResource::collection($exam_types);
Now inside resource that's when you can retrieve the details
#do not forget to import StudentsMark
#and you can make a resource for details, just to make clean code
return [
'exam_type' => $this->exam_type,
'details' => StudentsMark::where('academic_id', $this->academic_id)
->where('student_id', $this->student_id)
->get();
];
In my Symfony 4 project, I have 2 entities: Question and PossibleAnswer. A question can have many possible answers and an answer can be used in many different questions.
I want to POST a question, I use FOSRestBundle. This is how I would format the body of my request:
{
"title": "my question",
"possibleAnswers": [
{
"id": 1,
"title": "my first answer"
}
]
}
At least this is how FOSRestBundle formats the answer when I create the data directly in the database and GET the question.
But I keep getting the same error:
Validation Failed, possibleAnswers This value is not valid
I use Symfony forms to validate my POST and the fieldType I use for the possibleAnswers field of Question is an EntityType with the option multiple set to true. Maybe that's where the error comes from.
So my question is: How do I format the body of my request to add a valid possibleAnswers field ? and if I am doing it right then, which form type should I use for it to work ?
Sorry for not adding any code, the context is actually quite more complex but I'm pretty sure either the JSON Format of my field or the form type is wrong.
[edit]
here are all the form types I tried for the possibleanswers field
//->add('possibleAnswers', EntityType::class, array('data' => [], 'required' => false, 'multiple' => true, 'class' => PossibleAnswer::class))
->add('possibleAnswers', CollectionType::class, array('required' => false, 'entry_type' => EntityType::class, 'entry_options' => array('class' => PossibleAnswer::class)))
//->add('possibleAnswers', CollectionType::class, array('required' => false, 'entry_type' => PossibleAnswerType::class))
//->add('possibleAnswers', CollectionType::class, array('required' => false, 'entry_type' => PossibleAnswerType::class, 'entry_options' => array('class' => PossibleAnswer::class)))
here are all the JSON formats I tried to select the possibleanswers:
{
"title": "a postman created question",
"answerConditions": [
{
"id": 1,
"title": "already existing question",
}
]
}
{
"title": "a postman created question",
"answerConditions": [
{
"possibleanswer_id": 1
}
]
}
{
"title": "a postman created question",
"answerConditions": [
{
"id": 1
}
]
}
{
"title": "a postman created question",
"answerConditions": [
1
]
}
If you want to create the PossibleAnswer when posting the Question, you need to replace in your form the EntityType by a CollectionType.
I often use it in my POST request when I want to post the sub-object (here the PossibleAnswer) when I post the parent (Question) :
Question form
$builder
->add('possibleAnswers', CollectionType::class, array(
'entry_type' => PossibleAnswerType::class,
'allow_add' => true,
))
;
PossibleAnswer form :
$builder
->add('title')
;
And the json looks like :
{
"title": "my question",
"possibleAnswers": [
{
"title": "my first answer"
}
]
}
Hope it can help.
Edit :
If you want to link a PossibleAnswer to a Question while creating you Question you can use this :
Question form :
$builder
->add('possibleAnswer', EntityType::class, array(
'class' => YOUR_ENTITY::class,
'multiple' => true
));
And you json should only contains the id of the entity you want to link to. It should be like this :
{
"title": "a postman created question",
"answerConditions": [
1
]
}
I am trying to send some data along to Algolia through the toSearchableArray. Any strings I have stored in my DB are sending along fine, but I hit a roadblock when trying to push nested JSON data along—the information is being sent as a string with characters escaped.
This is a sample of the nested object that I am storing in my table (MySQL with a JSON data type):
[
{
"id": 19,
"name": "Mathematics",
"short": "Math"
},
{
"id": 23,
"name": "Science",
"short": "Science"
},
{
"id": 14,
"name": "Health and Life Skills",
"short": "Health"
}
]
My model looks like this:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class Resource extends Model
{
use Searchable;
protected $primaryKey = 'objectID';
public function toSearchableArray()
{
$data = $this->toArray();
$data['grades'] = explode(';', $data['grades']);
$data['units'] = explode(';', $data['units']);
return $data;
}
}
I get an output that looks like this:
array:22 [
"objectID" => 1
"name" => "Resource #1"
"slug" => "resource-1"
"write_up" => """
This is an example write up.
"""
"author" => "johnny"
"type_name" => "Lesson Plan"
"language" => "English"
"grades" => array:3 [
0 => "Kindergarten"
1 => "Grade 1"
2 => "Grade 4"
]
"subjects" => "[{"id": 19, "name": "Mathematics", "short": "Math"}, {"id": 23, "name": "Science", "short": "Science"}, {"id": 14, "name": "Health and Life Skills", "short": "Health"}]"
"units" => array:2 [
0 => "Unit A"
1 => "Unit B"
]
"main_image" => "https://dummyimage.com/250x325/000000/fff.png&text=Just+a+Test"
"loves" => 88
"downloads" => 280
"created_at" => "2018-01-01 13:26:47"
"updated_at" => "2018-01-02 10:10:32"
]
As you can see, the 'subjects' attribute is being stored as a string. I know there is attribute casting in 5.5 (I am running 5.5), but I am not too clear on how I would implement the example they have for Array & JSON Casting in my work above. https://laravel.com/docs/5.5/eloquent-mutators#attribute-casting
Would anyone be willing to show me an example?
I'd rely on Attribute Casting for this, add a $casts property in your model and it will be done automatically.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class Resource extends Model
{
use Searchable;
protected $primaryKey = 'objectID';
protected $casts = [
'subjects' => 'array',
];
public function toSearchableArray()
{
// Same function as you posted
}
}
You can also do it manually in your toSearchableArray method with $data['subjects'] = json_decode($this->subjects, true);
I answered with more details on this other posts: https://discourse.algolia.com/t/laravel-array-json-casting-to-algolia/4125/2
Situation
Using cake 3.2.4
and the plugin Crud 4.2
for the purpose of producing an API feed
What code did I use at first?
$this->Crud->on('beforePaginate', function(Event $event) use ($conditions) {
$this->paginate['conditions'] = $conditions;
$this->paginate['limit'] = 100;
$this->paginate['fields'] = [
'id', 'title',
'start_date',
'end_date',
'revenue',
'total_costs', 'collections'
];
});
What am I getting as json feed?
"success": true,
"data": [
{
"id": 789,
"title": "9143 - Asia Summit",
"start_date": "2016-03-02T09:00:00+0800",
"end_date": "2016-03-04T18:45:00+0800",
"revenue": 1000000.00,
"total_costs": 0,
"collections": 50000.00
},
{
"id": 15,
"title": "9144 - 10th Exhibition",
"start_date": "2016-03-21T09:00:00+0800",
"end_date": "2016-03-23T17:00:00+0800",
"revenue": 2000000.00,
"total_costs": 0,
"collections": 50000.00
}]}
What did I then do?
I wanted to return as aliases so I did the following
$this->Crud->on('beforePaginate', function(Event $event) use ($conditions) {
$this->paginate['conditions'] = $conditions;
$this->paginate['limit'] = 100;
$this->paginate['fields'] = [
'id', 'title',
'start' => 'start_date',
'end' => 'end_date',
'revenue',
'costs' => 'total_costs', 'collections'
];
});
What did I now get?
{
"success": true,
"data": [
{
"id": 789,
"title": "9143 - Asia Summit",
"start": "2016-03-02 09:00:00",
"end": "2016-03-04 18:45:00",
"revenue": 1000000.00,
"costs": "0.00",
"collections": 50000.00
},
{
"id": 15,
"title": "9144 - 10th Exhibition",
"start": "2016-03-21 09:00:00",
"end": "2016-03-23 17:00:00",
"revenue": 2000000.00,
"costs": "0.00",
"collections": 50000.00
},
So what's wrong?
I get the aliases I wanted, but then the costs has now become a string.
The datetime no longer shows the timezone.
How do I force the fields of the objects in the json feed to be of a certain type whilst keeping the aliases?
Realise that in 3.2 you can use addDefaultTypes to affect the query directly
See Cakephp-3.x: How to change the data type of a selected alias?
Realise that you can access the query from $event when using Crud
See http://crud-view.readthedocs.org/en/latest/basic-usage.html?highlight=query#providing-associations-to-be-displayed
Use the types datetime, and decimal
Put it altogther and you get:
$this->Crud->on('beforePaginate', function(Event $event) use ($conditions) {
$this->paginate['conditions'] = $conditions;
$this->paginate['limit'] = 100;
$this->paginate['fields'] = [
'id', 'title',
'start' => 'start_date',
'end' => 'end_date',
'revenue',
'costs' => 'total_costs', 'collections'
];
$paginationQuery = $event->subject()->query;
$paginationQuery
->selectTypeMap()
->addDefaults([
'start' => 'datetime',
'end' => 'datetime',
'costs' => 'decimal'
]);
});