Obtain specific data from Google Firestore using Rest API calls (HTTP-GET) - rest

Problem
I want to retrieve specific data from Google Firestore.
It's only possible to get all of the 'Fields' data. But no specific data within fields
Example of the GET-Request:
https://firestore.googleapis.com/v1beta1/projects/edubox-49528/databases/(default)/documents/nodes/EduBox-1234567?key=[My_API_KEY]&fields=fields
As you can see, It is possible to obtain all the items in the object 'Fields'. But it is not possible to get any further into detail to obtain more specific data (test, message, nodeID, ...)
Tryouts
I have already tried:
fields=fields/test
fields=fields.test
fields=fields(test)
fields=fields/test/integerValue
...
Expected Results
I want to obtain specific data like the String / integer value of my objects in 'Fields'.
This example should return the integerValue with 30
https://firestore.googleapis.com/v1beta1/projects/edubox-49528/databases/(default)/documents/nodes/EduBox-1234567?key=[My_API_KEY]&fields=fields/test
This example should return 30
https://firestore.googleapis.com/v1beta1/projects/edubox-49528/databases/(default)/documents/nodes/EduBox-1234567?key=[My_API_KEY]&fields=fields/test/integerValue

Solution
While browsing the web, I came across Google Api Explorer:
https://developers.google.com/apis-explorer/#search/firestore/firestore/v1beta1/
When trying out some possibilities, I came across this:
https://firestore.googleapis.com/v1/projects/edubox-49528/databases/(default)/documents/nodes/EduBox-1234567?mask.fieldPaths=nodeID&fields=fields&key={YOUR_API_KEY}
This gives me the right information
but I still need a more detailed form of this answer like only the 'EduBox-1234567'

The way to retrieve a specific field is to use mask.fieldPaths. For example the following GET method:
https://firestore.googleapis.com/v1beta1/projects/edubox-49528/databases/(default)/documents/nodes/EduBox-1234567?key=[My_API_KEY]&fields=fields&mask.fieldPaths=nodeID
is going do return:
{
"fields": {
"nodeID": {
"stringValue": "EduBox-1234567"
}
}
Documentation references here and here.

Related

How to use update mask to update a 'arrayValue' at an index?

I need to update a particular field inside a document in Firebase. My application is WatchOS 6 application and hence I am sort of forced to use the REST API and uploadMask.
I have the following URL that points to the document that needs to be updated along with the field mask name that I need to update:
https://firestore.googleapis.com/v1beta1/projects/{projectId}/databases/{databaseId}/documents/users/{documentID}/goals/{documentID}/tasks/{documentID}?updateMask.fieldPaths=is_complete
Now, my request body:
{
"fields": {
"instructions&steps": {
"arrayValue": {
"values": [
{
"mapValue": {
"fields": {
"is_complete": true
}
}
}
]
}
}
}
}
I'll explain how I came up with the request body above:
The document has multiple values, one of them is fields. Inside fields, again I have multiple values. The one I want to update is 'instructions&steps' which is an array of JSON objects.
I need to be able to update any index of that array and the field 'is_completed' on that particular index for my application.
I am not sure how to do that.
I tried figuring it out on the Google API Explorer with the above mentioned body and URL, I get a 400 error with message: document.fields[0].value.array_value.values[0].map_value.fields[0].value
Why does it say fields[0] ? That is not an array.
Ideally, it should be something like: document.fields.instructions&steps.array_value.values[0].map_value.fields.is_completed
And if I want to update for 2nd object, I simply need:
document.fields.instructions&steps.array_value.values[1].map_value.fields.is_completed
Any help is appreciated. Thanks.
EDIT 1:
Okay, so I was able to get the request working by changing "is_complete": true to "is_complete" : {booleanValue: true}. I now get a 200 status and the entire document in response, but the value of field is_complete is still false and isn't updated! Additionally, how do I specify the index of the JSON at which I want to update is_completed field ?
After doing some additional digging, apparently Firebase doesn't allow you to update individual values in arrayValues yet. Is this true ?
If so, is there some way to work around that since my platform(Apple Watch/ watchOS 6) doesn't have Firebase SDK yet and I have to use REST API because of that.

Ordering Server adding sshkeys

I want to order server with sshkeys using the api, but when I use the sshkey property in the structure it returns the result without the keys, I know my code is working fine becausw I orderwd before. I would like to check if my ids are correct, is there any form to check them by using my label names???
this is the structure for ssh keys:
"sshKeys": [
{
"sshKeyIds": [94206]
}
]
You can call http://sldn.softlayer.com/reference/services/SoftLayer_Account/getSshKeys method to get the IDs of your sshs keys and you can use object filters to get the ssks by label this is am example using Rest:
GET https://<USERNAME>:<APIKEY>#api.softlayer.com/rest/v3/SoftLayer_Account/getSshKeys?objectFilter={"sshKeys":{"label":{"operation":"tonny"}}}
here more information about object filters http://sldn.softlayer.com/article/object-filters

How to design calculation API in RESTFul way?

I'm trying to design an API to calculate a result based on inputs.
Real business:
The API compares two securities portfolios (source and target) and return the orders, the consumer gets the orders, so he/she can then places those orders to adjust portfolio from source to target.
If this is hard to be understood, then here's a similar scenario:
The API compare two text, then return the difference of the 2 texts.
It is a little bit different from the classic CRUD, because the inputs and output are different resources
My first thought is like this:
POST /api/difference
{
'source': { ... },
'target': { ... }
}
But, it will be conflict with the classic payload:
POST /api/difference
{
'lineNumber': ...,
'isAdded': ...
}
Questions:
Should I use a media-type to distinguish the the input payloads? What a 'resource' should be in this case?
What should the API look like if I also want to place the orders (or apply the text diff) in the same time when the API is called?
Iam not sure whether I understand your problem correctly, but in general it
depends on whether the resources are already persisted in the system. In case
both resources are already available in the system I would simply build an URI
like /portfolio/{source_id}/difference/{target_id} which returns the diff
result. If only the source exists I would probably use something like:
POST /portfolio/{source_id}/difference
{target}
If both resources are not available I would probably consider to first persist
such a resource and make then the comparison.
If I understood you correctly, there already exists the resource POST /api/difference and hence you are looking to change MIME type. Instead, why don't you go with the first approach and change the resource name? For example,
POST /api/compare
{
'source': { ... },
'target': { ... }
}

Elasticsearch query design via RESTful API

I'm trying to build a query that first lets me get a list of followers that are following a user, second it should take that list and then check to see if they are 'online'.
I have two 'indexes' or endpoints /channel and /following.
The channel endpoint JSON object looks like this (parts abbreviated)
{ channel: {"username":"username1", ... , "online":"true" } }
The following endpoint object looks a bit like this
{ following : {"username1":{"username2":"username2", "username3":"username3"} }
if I run a simple query /following/_search I get back hits like...
{
"_index": "following",
"_type": "following",
"_id": "_Liso_",
"_score": 1,
"_source": {
"Gabe": "Gabe",
"Gavin": "Gavin"
}
}
This result means that Gavin is following Gabe.
I believe the issue is how I'm storing the data.
In firebase my data looks like this
following
|---Gabe
|----Gavin:Gavin
so each child object of following object has key/value children of {username}:{username}
Now I can run queries that individually get the results I need. For example, if I ask ElasticSearch (ES) if channel "Gavin" is "online" I get back one result depending on if they are or are not online. And same with Following. However I can't seem to get the query to first see who is following Gavin and then see if they are online and return those users whom are online.
I've found a better solution (or maybe not). First you query the database for users whom are following a user.
From this list you send another query
{
"query":{
"filtered":{
"query":{
"match_all":{}
},
"filter":{
"bool":{
"must":{
"terms":{
"username":["username1"]
}
},
"must_not":{
"terms":{
"online":["true"]
}
}
}
}
}
}
}
This works however the username cannot be mixed with capitols. I don't know if this is an indexing issue on my part or terms have to be very specific. The solution I'm using on the client side is to lowercase the search terms before I submit them. It's crude and hacky but it works for now.
Issues that I may run into:
If a user has millions of followers pulling all that data from the
database will make the client sluggish.
a possible solution to this is to paginate the following results and run the query for every 20 returned results.
I'll continue to revise the answer as I develop / learn better query methods.

Graph API - get JSON data without "paging" key

I'm getting facebook data using graph api, adding fields in string and get JSON result.
Example:
https://graph.facebook.com/me?fields=music
But JSON returned contains a "paging" key and I do not I want this key.
{ "music":{
"data":[
{
"name":"",
"category":"",
"id":"",
"created_time":""
},
{
"name":"",
"category":"",
"id":"",
"created_time":""
}
],
"paging":{
"next":"https://graph.facebook.com/me?fields=music&method=GET&metadata=true&format=json&callback=___GraphExplorerAsyncCallback___&access_token=...&limit=5000&offset=5000&__after_id=..."
}}}
EDITED:
I'm using Java API (restfb.com) to get JSON.
The command in java is:
FacebookClient client = new DefaultFacebookClient("ACCESS_TOKEN_HERE");
JsonObject rMusic = client.fetchObject("ID_HERE", JsonObject.class, Parameter.with("fields", "id,name,religion,birthday,music"));
How do I avoid it or remove it?
When you have your Javascript object built from the JSON, just pay attention to the array of data: result.music.data
And forget about the paging property: result.music.paging
Remember, there's no law in coding that you have to look at every property in your scripts.
Based upon the edit to the question above, here's a new answer.
The Rest API is deprecated. You should upgrade your app to use the Graph API as this is the one being supported.
Also, if you see a property you don't like, you don't have to access it. Remember, there's no law in coding that you have to look at every property in your scripts.