Paginating messages with Microsoft Graph API v.1.0 - microsoft-graph-api

I'm using Microsoft Graph API v.1.0 I'm trying to get the emails I only see 10 of them which I understand is the default pagination page limit but there is no #odata:nextLink property being returned to go to the next batch of results.
This is my code:
$graph = new Graph();
$graph->setAccessToken(session('access_token'));
$me = $graph->createRequest("get", "/me/messages")
->setReturnType(Model\User::class)
->execute();
return $me;
The result:
array:10 [▼
0 => User {#379 ▼
#_propDict: array:28 [▶]
}
1 => User {#365 ▶}
2 => User {#375 ▶}
3 => User {#368 ▶}
4 => User {#367 ▶}
5 => User {#366 ▶}
6 => User {#380 ▶}
7 => User {#373 ▶}
8 => User {#382 ▶}
9 => User {#383 ▼
#_propDict: array:28 [▼
"#odata.etag" => "W/"CQAAABYAAAC8b+tAO4nLRZCbkhud5CXFAANG4sRR""
"id" => "AAMkADdlZTBjNjQ4LWI0OGItNDFhZS05ZDNiLThiY2JkYzIzZWZkYwBGAAAAAABFX7lJCx7ZRLTJ6iI0yZK6BwC8b_tAO4nLRZCbkhud5CXFAAAAAAEKAAC8b_tAO4nLRZCbkhud5CXFAANGMK1oAAA="
"createdDateTime" => "2018-01-05T14:25:58Z"
"lastModifiedDateTime" => "2018-01-05T14:25:58Z"
"changeKey" => "CQAAABYAAAC8b+tAO4nLRZCbkhud5CXFAANG4sRR"
"categories" => []
"receivedDateTime" => "2018-01-05T14:25:58Z"
"sentDateTime" => "2018-01-05T14:25:58Z"
"hasAttachments" => false
"internetMessageId" => "<DB5PR05MB15093E653073F8E1A95F0EE2D11C0#DB5PR05MB1509.eurprd05.prod.outlook.com>"
"subject" => "test"
"bodyPreview" => "HTML"
"importance" => "normal"
"parentFolderId" => "AQMkADdlZQAwYzY0OC1iNDhiLTQxYWUtOWQzYi04YmNiZGMyM2VmZGMALgAAA0VfuUkLHtlEtMnqIjTJkroBALxv60A7ictFkJuSG53kJcUAAAIBCgAAAA=="
"conversationId" => "AAQkADdlZTBjNjQ4LWI0OGItNDFhZS05ZDNiLThiY2JkYzIzZWZkYwAQAN7fN0jAEi5LroDvBtBQMRs="
"isDeliveryReceiptRequested" => false
"isReadReceiptRequested" => false
"isRead" => true
"isDraft" => false
"webLink" => "https://outlook.office365.com/owa/?ItemID=AAMkADdlZTBjNjQ4LWI0OGItNDFhZS05ZDNiLThiY2JkYzIzZWZkYwBGAAAAAABFX7lJCx7ZRLTJ6iI0yZK6BwC8b%2BtAO4nLRZCbkhud5CXFAAAAAAEK ▶"
"inferenceClassification" => "focused"
"body" => array:2 [▶]
"sender" => array:1 [▶]
"from" => array:1 [▶]
"toRecipients" => array:1 [▶]
"ccRecipients" => []
"bccRecipients" => []
"replyTo" => []
]
}
]

Please see the PHP documentation for paging at https://github.com/microsoftgraph/msgraph-sdk-php/wiki/Example-calls#get-a-collection-of-items
It looks like you should change the return type to match the endpoint. In your case, your return type is User but you're querying for messages. That's why your result array has User objects. I think the model type you're looking for is Message and you can see the source for this at https://github.com/microsoftgraph/msgraph-sdk-php/blob/dev/src/Model/Message.php

I ended up using the API directly and formatting the next and previous links.
//get messages from inbox folder
$emails = Api::get("me/mailFolders/$inbox/messages?".http_build_query($messageQueryParams));
$previous = null;
$next = null;
$total = $emails['#odata.count'];
if (isset($emails['#odata.nextLink'])) {
$first = explode('$skip=', $emails['#odata.nextLink']);
$skip = explode('&', $first[1]);
$previous = $skip[0]-$offset;
$next = $skip[0];
if ($previous < 0) {
$previous = 0;
}
if ($next == $total) {
$next = null;
}
}

Related

Search list result null prevPageToken

I'm trying to get videos from my channel using search function. Response include only nextPageToken, but no prevPageToken. prevPageToken is null always, even if I'm listing other pages using nextPageToken. I use an access_token param from Google Oauth.
My request:
$searchResponse = $youtube->search->listSearch('snippet', array(
'forMine' => 'true',
'order' => 'date',
'pageToken' => $pageToken,
'type'=>'video',
'maxResults' => '10',
'q' => $searchQuery,
));
Result:
Google_Service_YouTube_SearchListResponse Object
(
[collection_key:protected] => items
[etag] => "XXX"
[eventId] =>
[itemsType:protected] => Google_Service_YouTube_SearchResult
[itemsDataType:protected] => array
[kind] => youtube#searchListResponse
[nextPageToken] => Cib3-ZrgSf____9uWHdEYVNIVnlOVQD_Af_-blh3RGFTSFZ5TlUAARAPIbmtEhE6-iWlOQAAAAC2H2UGSAFQAFoLCdIYcGeU2-YsEAJgyOjNygE=
[pageInfoType:protected] => Google_Service_YouTube_PageInfo
[pageInfoDataType:protected] =>
[prevPageToken] =>
[regionCode] =>
[tokenPaginationType:protected] => Google_Service_YouTube_TokenPagination
[tokenPaginationDataType:protected] =>
[visitorId] =>
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
[pageInfo] => Google_Service_YouTube_PageInfo Object
(
[resultsPerPage] => 5
[totalResults] => 55
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
)
Thank you for your ideas!
Petr
UPDATE
This problem is only if is set param forMine. But I need show only my videos...

Multiple array null value?

I try to insert mutiple arrays per job_id when i try this my array values out put as null value this is my store function
$input output :
array:7 [▼
"date" => "2016-01-10",
"job_id" => "2",
"charge" => array:2 [▼
0 => "ch1",
1 => "ch2"
],
"category" => array:2 [▼
0 => "se1",
1 => "se2"
],
"amount" => array:2 [▼
0 => "100"
1 => "200"
]
]
Full Code :
$input = Input::all();
$charge = Input::get('charge');
$date = Input::get('date');
$job = Input::get('job_id');
$amount = Input::get('amount');
$cat = Input::get('category');
$id = \Auth::id();
$data = array();
foreach ($charge as $key=>$value ){
$data[] = [
'date' => $date,
'charge'=>$key['charge'],
'job_id'=>$job,
'user_id'=>$id,
'amount'=>$key['amount'],
'category'=>$key['category'],
];
}
dd($data);
When i try to get out put i will get this out put
array:2 [▼
0 => array:6 [▼
"date" => "2016-01-10"
"charge" => null
"job_id" => "2"
"user_id" => 1
"amount" => null
"category" => null
]
1 => array:6 [▼
"date" => "2016-01-10"
"charge" => null
"job_id" => "2"
"user_id" => 1
"amount" => null
"category" => null
]
]
What i want is i want insert data to my database any one suggest me good idea ?
Answer for this question from Laracast Check this link
Your final output looks correct, try to use the following code to insert the $data to your DB :
Model::insert($data); // Eloquent
DB::table('table')->insert($data); // Query Builder
You should construct your array using $inputs['amount'][$key] for amount value and $inputs['category'][$key] for category, like following :
foreach ($charge as $key=>$value ){
$data[] = [
'date' => $date,
'charge'=>$value,
'job_id'=>$job,
'user_id'=>$id,
'amount'=>$inputs['amount'][$key],
'category'=>$inputs['amount'][$key],
];
}
Hope this helps.

How would I filter out sub documents?

1) How can I grab all songs with more than 50 likes? The result set should return an array of songs instead of the whole document.
2) How can I return the results of a specific song id instead of the whole document?
So far I've come across this concept of virtual collections for embedded objects. However, it hasn't been implemented since. Although, on the same link, someone suggests using the aggregation framework for such a thing. Is this a viable solution?
I have the following document structure:
document = {
'date' => Time.now.utc.strftime('%Y%m%d'),
'songs' => {
'1' => {
'views' => {
'non_uniques' => 2000,
'uniques' => 203
},
'countries' => {
'us' => 123,
'uk' => 11
},
'likes' => 123,
'comments' => 123
},
'2' => {
'views' => {
'non_uniques' => 2000,
'uniques' => 203
},
'countries' => {
'us' => 123,
'uk' => 11
},
'likes' => 123,
'comments' => 123
}
},
'sign_ins' => {
'non_uniques' => 2000,
'uniques' => 203
}
}
Expected output (as requested)
For question 1), somewhere within the lines of:
['1' => {
'views' => {
'non_uniques' => 2000,
'uniques' => 203
},
'countries' => {
'us' => 123,
'uk' => 11
},
'likes' => 123,
'comments' => 123
},
'2' => {
'views' => {
'non_uniques' => 2000,
'uniques' => 203
},
'countries' => {
'us' => 123,
'uk' => 11
},
'likes' => 123,
'comments' => 123
}]
For 2):
'1' => {
'views' => {
'non_uniques' => 2000,
'uniques' => 203
},
'countries' => {
'us' => 123,
'uk' => 11
},
'likes' => 123,
'comments' => 123
}
Use aggregation framework like this:
db.songs.aggregate({$unwind:"$songs"},{$match:{"songs.likes":{$gt:50}}})
Now you can add other stages like {$group} if you want things back in one doc, or $project to change the field names, etc.
Change the '=>' delimiter in the document. Then you will have a JSON file
document = JSON.parse(document)
You will get a hash. Then check if likes > 50 in the hash
document[].each do |song|
song[].each do |id|
if id[likes].to_i > 50
#Do what you want to do
end
end
end
Hope that helps
Try:
songs_arr = []
document["songs"].keys.each do |key|
songs_arr << "insert songs here" if document["songs"][key]["likes"] > 50
end

How to send custom parameter from jqgrid to rails controller?

I use jqgrid in my rails to show data in my web
and I want to decide which data to show depends on which page the user is surfing
here's the code
show.html.erb
<%=raw jqgrid("people_list", "people", "/people",
[ { :field => "pid", :label => "pid" ,:editable => true},
{ :field => "name", :label => "name" ,:editable => true},
{ :field => "gender", :label => "gender" ,:editable => true},
{ :field => "birthday", :label => "birthday" ,:editable => true}
] ,
{:autowidth=>true,:add => true,:edit => true,:delete => true,:view => true,:edit_url => post_data_people_url,:sortable_rows => 'true'
}
)%>
people_controller.rb
def index
index_columns ||= [:pid,:name,:gender,:birthday,:school]
current_page = params[:page] ? params[:page].to_i : 1
rows_per_page = params[:rows] ? params[:rows].to_i : 10
conditions={:page => current_page, :per_page => rows_per_page}
conditions[:order] = params["sidx"] + " " + params["sord"] unless (params[:sidx].blank? || params[:sord].blank?)
if params[:_search] == "true"
conditions[:conditions]=filter_by_conditions(index_columns)
end
#temp = Person.limit(0).all
KlassesPeople.where(:klass_id => 1).each do |stu|########
#temp.concat( Person.where(:id => stu.person_id) )
end
#people = #temp.paginate(conditions)
total_entries=#people.total_entries
respond_with(#people) do |format|
format.html
format.json { render :json => #people.to_jqgrid_json(index_columns, current_page, rows_per_page, total_entries)}
end
end
I want to pass the value of klass id from the jqgrid to the controller to determine the value of klass_id in the line with "########"
any idea?
I don't know how to format the code for you but you can use the postData parameter to pass a value to your controler every time the jqGrid would get data.
postData: { KeyName: KeyValue },
If you need to set this value dynamically you can set it via something like:
$('#gridName').jqGrid('setGridParam', { postData: { KeyName: KeyValue });

Dynamic hash - Ruby on Rails [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
the code so far:
def queryConnect
...
count = 0
#clients is an array of hashes.
#channels is an array of hashes.
channel_array = []
channels.each do |channel|
begin
if clients[count]["cid"].to_i == channel["cid"].to_i
channel_array << { "cname" => channel["channel_name"].transform_escape,
"name" => clients[count]["client_nickname"].transform_escape }
count = count + 1
else
channel_array << { "cname" => channel["channel_name"].transform_escape }
end
rescue
#transform_escape -> some elements are not type string.
end
end
count = 0
channel_array
end
channels sample input: [{"channel_name" => "game1", "cid" => 1}, {"channel_name" => "game2", "cid" => 2}, {"channel_name" => "game3", "cid" => 3}]
clients sample input: [{"client_nickname" => "greg", "cid" => 1}, {"client_nickname" => "bob", "cid" => 1}, {"client_nickname" => "george", "cid" => 3}]
expected output:[{"cname" => "game1", "name" => ["greg", "bob"]}]
The problem: I'm trying to loop through all the channels, if the client's channel id and channel's channel id (cid) match I want to create a hash with the channel name and client name(s) and then push it onto the channel_array.
The only issue I'm having now is when there are multiple clients in a channel... How can I dynamically add a key, value pair by iterating through the clients array and still maintaining the condition to see if the channel ids match?
I've been beating my head against my text editor for two days on this problem any help is appreciated.
channels = [{"channel_name" => "game1", "cid" => 1},
{"channel_name" => "game2", "cid" => 2},
{"channel_name" => "game3", "cid" => 3}]
clients = [{"client_nickname" => "greg", "cid" => 1},
{"client_nickname" => "bob", "cid" => 1},
{"client_nickname" => "george", "cid" => 3}]
channel_data = channels.inject({}){|m, c| m[c["cid"]] = c["channel_name"]; m}
=> {1 => "game1", 2 => "game2", 3 => "game3"}
client_data = clients.inject({}){ |m, c|
m[c["cid"]] ||= []
m[c["cid"]] << c["client_nickname"]
m
}
=> { 1 => ["greg", "bob"], 3 => ["george"] }
channel_data.map{|cid, cname|
{"cname" => cname, "name" => client_data[cid]} if client_data[cid]
}.compact
=> [{"cname" => "game1", "name" => ["greg", "bob"]}, {"cname" => "game3", "name" => ["george"]}]
Finally figured it out.
channel_array << { "cname" => channel["channel_name"], "name" => clients.inject([]){ |m, c| m << c["client_nickname"] if c["cid"] == channel["cid"]; m} }

Resources