How to specify constraint in Meteor Http call? - meteor

I need to pass a where constraint(where UserName = "User1") in Meteor http call for Parse Rest APIs. Currently, the result that I get after the below API call includes all the entries not just those where UserName is User1.
var authUrl = "https://api.parse.com/1/classes/ImageData";
Meteor.http.call("GET", authUrl, {
headers: {
"X-Parse-Application-Id": "2CMX1b4JY5xCOPrYEbSc69ucNDDh9pl5yFeqv3A3",
"X-Parse-REST-API-Key": "9UWpw6H7UuBaOEQgT7R3ANQ3rE67yxZxcMHJJaBu",
"content-type": "application/json"
},
params: {
"UserName": "User1",
}
}, function(error, result) {
console.log(JSON.parse(result.content));
}
);
The parse documentation for such an HTTP call in curl representation is:
curl -X GET \
-H "X-Parse-Application-Id: 2CMX1b4JY5xCOPrYEbSc69ucNDDh9pl5yFeqv3A3" \
-H "X-Parse-REST-API-Key: 9UWpw6H7UuBaOEQgT7R3ANQ3rE67yxZxcMHJJaBu" \
-G \
--data-urlencode 'where={"UserName":"User1"}' \
https://api.parse.com/1/classes/ImageData
How can I correctly write this in Meteor?

This seems like it works:
var util = Npm.require('util');
var url = 'https://api.parse.com/1/classes/ImageData';
var result = HTTP.get(url, {
headers: {
'X-Parse-Application-Id': '2CMX1b4JY5xCOPrYEbSc69ucNDDh9pl5yFeqv3A3',
'X-Parse-REST-API-Key': '9UWpw6H7UuBaOEQgT7R3ANQ3rE67yxZxcMHJJaBu',
'content-type': 'application/json'
},
query: 'where={"UserName":"User1"}'
});
console.log(util.inspect(result.data, {depth: null}));
Notes
Meteor.http.call is deprecated. Use the HTTP API instead. Note you will need to $ meteor add http.
Because you need a string and not a key/value pair, use query instead of params. For a GET, both are placed into the query string but your original code made the query ?Username=User1 rather than ?where....

Related

How to hot update parameters in the NebulaGraph database?

I want to enable authentication on my test host deployed with Nebula Graph, but I cannot find a way to do it without shutting the service down. I really don't want to do this.
NebulaGraph leveraged the gflags as its configuration utils. And we exposed an HTTP interface per each process, which is configured with ws_http_port to listen on.
Note, not all configurations could be changed on the fly in this "hot update" way.
Read in-memory Configurations
$ curl graphd:19669/flags | head
check_plan_killed_frequency=8
cluster_id_path="cluster.id"
expired_time_factor=5
failed_login_attempts=0
heartbeat_interval_secs=10
meta_client_retry_interval_secs=1
meta_client_retry_times=3
meta_client_timeout_ms=60000
password_lock_time_in_secs=0
storage_client_retry_interval_ms=1000
$ curl graphd:19669/flags?flags=ws_http_port
ws_http_port=19669
Update in-memory configuration
$ curl graphd:19669/flags?flags=system_memory_high_watermark_ratio
system_memory_high_watermark_ratio=0.8
$ curl -X PUT graphd:19669/flags \
-H "Content-Type: application/json" \
-d '{"system_memory_high_watermark_ratio":"0.9"}'
$ curl graphd:19669/flags?flags=system_memory_high_watermark_ratio
system_memory_high_watermark_ratio=0.9
Ref:
We could check all webservice endpoints here
Status WebService::start(uint16_t httpPort) {
if (started_) {
LOG(INFO) << "Web service has been started.";
return Status::OK();
}
router().get("/flags").handler([](web::PathParams&& params) {
DCHECK(params.empty());
return new GetFlagsHandler();
});
router().put("/flags").handler([](web::PathParams&& params) {
DCHECK(params.empty());
return new SetFlagsHandler();
});
router().get("/stats").handler([](web::PathParams&& params) {
DCHECK(params.empty());
return new GetStatsHandler();
});
router().get("/status").handler([](web::PathParams&& params) {
DCHECK(params.empty());
return new StatusHandler();
});
router().get("/").handler([](web::PathParams&& params) {
DCHECK(params.empty());
return new StatusHandler();
});

gRPC-Gateway: Is there a way to return responses in form of JSON arrays?

I'm making an implementation of https://todobackend.com/ using gRPC-Gateway. https://todobackend.com/'s spec requires some responses to be in form of JSON arrays, like:
GET /todos
=> [{ "title": "...", ... }, { ... }]
But AFAIK by using gRPC-Gateway I must return objects, like { "todos": [{ ... }, { ... }] }. Can I return arrays instead of objects?
I found this thread and got it working with response_body option along with allow_repeated_fields_in_body CLI argument.
rpc Add(Todo) returns (Todo) {
option (google.api.http) = {
post: "/v1/todos",
body: "*"
};
};
protoc -I proto/ -I googleapis \
--go_out ./proto --go_opt paths=source_relative \
--go-grpc_out ./proto --go-grpc_opt paths=source_relative \
--grpc-gateway_out=allow_repeated_fields_in_body=true:./proto --grpc-gateway_opt paths=source_relative \
./proto/todo/todo.proto
# note "allow_repeated_fields_in_body=true"

Translating curl command to Meteor's HTTP call

I try to "translate" this curl command
curl --request POST --header "Content-Type: application/json" --url http://some-url --user userName:apiKey --data '{ "some": "JSON data as string" }'
into Meteor's HTTP call. I tried this:
const options = {
{ "some": "JSON data as object" },
headers: {
'Content-Type': 'application/json',
},
params: {
user: 'userName:apiKey',
},
// Or alternatively
//
// user: 'userName:apiKey',
};
HTTP.call('POST', 'http://some-url', options, (error, result) => {
if (error) {
reject(error);
} else {
resolve(result);
}
});
With curl command it works fine, with HTTP.call I get a 403, Forbidden. Authorization with userName:apiKey seems to fail. How do I specify the userName:apiKey in the HTTP.call example? Or maybe their is another problem?
If you need authentication, you need to add the auth parameter. The params parameter will actually set all the containing properties to be part of the POST request body.
const options = {
data: { "some": "JSON data as object" },
headers: {
'Content-Type': 'application/json',
},
auth: 'userName:apiKey'
}
Read: https://docs.meteor.com/api/http.html#HTTP-call

How to pass X-Auth-Token with Meteor's HTTP.call?

How to pass X-Auth-Token with Meteor's HTTP.call?
E.g. to do something like this:
curl -X GET \
--header "X-Auth-Token: 1234567890abcdeff978d137bc01a2" \
https://example.com/api/call
I found an answer on Meteor's forum:
options = {
headers: { 'X-Auth-Token' : '1234567890abcdeff978d137bc01a2' }
}
A bit more elaborated, in CoffeeScript:
res = HTTP.call 'GET', 'https://example.com/api/call',
headers:
'X-Auth-Token': auth_token
params:
a: 1
b: 2

How to send headers to an external API for authentication in Meteor

I'm a newbie trying to connect to my first API in Meteor.
The API docs (linked here) gave me a list of things I needed to generate (time-stamp, api-key & signature. I've generated them all and saved them as variables.) The docs say I need to make the following request:
curl -H "Request-Time: Wed, 06 Nov 2013 16:32:03 +0000" \
-H "Api-Key: 5d41402abc4b2a76b9719d911017c592" \
-H "Signature: 42d8824f24fb50e6793aa111c889b7df4d54bee9f5842a0d5fbca30cbfa469ae" \
http://wceaapi.org/v1.1/user/1234
How do I convert that in to Meteor Syntax?
Here's what I'm trying but I'm pretty sure it's totally wrong.
if (Meteor.isServer) {
Meteor.methods({
callAPI: function () {
this.unblock();
return Meteor.http.call("GET", "http://sandbox.wceaapi.org",
headers: {
"Request-Time" : timeStamp,
"Api-Key": key,
"Signature": hash});
}
});
}
//invoke the server method
if (Meteor.isClient) {
Meteor.call("callAPI", function(error, results) {
console.log(results.content); //results.data should be a JSON object
});
}

Resources