IBM Worklight - Adapter procedure response returns "zero results" from Google Places - ibm-mobilefirst

I am getting status "ZERO RESULTS" when I invoke HTTP Adapter using Indian latitude and longitude to search for any service like food, malls etc.
Here is my adapter implementation:
function getGooglePlaces(location,name) {
var input = {
method : 'get',
returnedContentType : 'json',
path : 'maps/api/place/search/json',
headers: {
Host: 'maps.googleapis.com'
},
parameters : {
'key' : My Key,
'location' : location,
'radius' : '10000',
'sensor' : 'false',
'name' : name
}
};
var response = WL.Server.invokeHttp(input);
return response;
}
}
The JSON response for indian coordinates.
results is the array of places searched
{
"html_attributions": [
],
"isSuccessful": true,
"responseHeaders": {
"Alternate-Protocol": "443:quic",
"Cache-Control": "public, max-age=300",
"Content-Type": "application\/json; charset=UTF-8",
"Date": "Wed, 12 Feb 2014 15:06:33 GMT",
"Expires": "Wed, 12 Feb 2014 15:11:33 GMT",
"Server": "mafe",
"Transfer-Encoding": "chunked",
"Vary": "Accept-Language",
"X-Frame-Options": "SAMEORIGIN",
"X-XSS-Protection": "1; mode=block"
},
"responseTime": 609,
"results": [
],
"status": "ZERO_RESULTS",
"statusCode": 200,
"statusReason": "OK",
"totalTime": 609
}

I have used the following and got a valid response, using a Worklight adapter.
Note that I passed name and location as part of the parameters object and not like you did (just for the sake of simplification)
I added a type parameter for "restaurant"
I increased the radius
With the above, I got results.
Looks to me like this is a matter of tweaking the request parameters. Nothing related to Worklight adapters here, as it works.
https://www.google.com/search?q=google+places+returns+zero+results
GooglePlaces-impl.js
function getPlaces() {
var input = {
method : 'get',
returnedContentType : 'json',
path : 'maps/api/place/search/json',
headers: {
Host: 'maps.googleapis.com'
},
parameters : {
'key' : 'make-sure-to-place-here-your-SERVER-KEY-generated-by-Google-GCM-console',
'location' : '27.173033, 78.042133',
'radius' : '30000',
'sensor' : 'false',
'name' : 'Taj Mahal',
'type' : 'restaurant',
}
};
var response = WL.Server.invokeHttp(input);
return response;
}
Procedure response
{
"html_attributions": [
],
"isSuccessful": true,
"responseHeaders": {
"Alternate-Protocol": "443:quic",
"Cache-Control": "public, max-age=300",
"Content-Type": "application\/json; charset=UTF-8",
"Date": "Fri, 14 Mar 2014 05:45:41 GMT",
"Expires": "Fri, 14 Mar 2014 05:50:41 GMT",
"Server": "mafe",
"Transfer-Encoding": "chunked",
"Vary": "Accept-Language",
"X-Frame-Options": "SAMEORIGIN",
"X-XSS-Protection": "1; mode=block"
},
"responseTime": 662,
"results": [
{
"geometry": {
"location": {
"lat": 27.175015,
"lng": 78.042155
}
},
"icon": "http:\/\/maps.gstatic.com\/mapfiles\/place_api\/icons\/generic_business-71.png",
"id": "dcc9586b99ab0f0471fccecbe2dbb40fdc1fc2b5",
"name": "Taj Mahal",
"photos": [
{
"height": 853,
"html_attributions": [
],
"photo_reference": "CnRnAAAAojrNNKpH2yqjmiph_kNBxiT5DaK_g3N05YtE_mPP5tBrYD8XjyAAz_xEvWAJymfGOeeOTlVgzFUxUeOMQspvoPGogDQyWomDeZsNP7XEW3JsmzmYakDk_vyJBwajiamhluypx6rCDqDCnBWb6JnCLBIQRuBibftSN9xruu2eeZEm5xoUksG5kyFbtZULWpZceNvNhyl72tQ",
"width": 1280
}
],
"rating": 4.6,
"reference": "CnRrAAAA8n6I_Dnlm9UzwTiaTntjcxR-FysL5Ya26Fdcsdb48XOIxiJDGdd3AiK6iUUti41d1BQ1XnBfZoVMKWZ5QOyVZAW8QyH-xqSY8eaQXuxH0atjzXtuaplht-ww76JtbxQLkJ4SUtFrmrs7ZjmZn-_RhBIQmYTB0_yGd_4hm2bHoIKt5xoULaBq-FsZo51jFdxLV377nHM0cCI",
"types": [
"establishment"
],
"vicinity": "Agra"
},
...
...
"status": "OK",
"statusCode": 200,
"statusReason": "OK",
"totalTime": 1088
}

Related

Vonage WhatsApp messages suddenly stopped working

I have recently joined a company that is using Vonage and we have WhatsApp communications which was working fine in both dev and production that have both suddenly stopped working.
The shape of the incoming json looks to have changed significantly, but I have changed the code that is reading this and am able to read the messages into the system.
The problem that I have now, is outgoing message is not being accepted.
If I send this json body over
{
"from": "4474183xxxxx",
"to": "4474719xxxxx",
"message": {
"Type": "text",
"content": "Hello! I’m CAI, A Virtual Chat-bot assistant. blurb, more blurb.... .\n\n1. Okay I understand\n\nChoose any one option. Type \"1\" to choose first option."
}
}
I get the following response
{
"Status": "fail", // custom one to my company
"e3": {
"Error": {
"body": {
"type": "https://developer.vonage.com/api-errors",
"title": "Your request parameters didn't validate.",
"detail": "Found errors validating 1 of your submitted parameters.",
"invalid_parameters": [
{
"name": "to",
"reason": "Malformed JSON body."
}
],
"instance": "cf4bce73-2db5-4102-b7af-xxxxxx"
},
"headers": {
"date": "Wed, 02 Nov 2022 14:02:08 GMT",
"content-type": "application/problem+json",
"content-length": "287",
"connection": "close",
"x-envoy-upstream-service-time": "3",
"x-frame-options": "deny",
"x-xss-protection": "1; mode=block",
"strict-transport-security": "max-age=31536000; includeSubdomains",
"x-content-type-options": "nosniff",
"server": "envoy"
},
"statusCode": 400
},
"StatCode": 400,
"Response": null
},
"a": {
"label": 6,
"trys": [
[
0,
6,
null,
7
]
],
"ops": []
}
}
However, looking at the shape of the message that is on the website (https://dashboard.nexmo.com/messages/sandbox) and send this message
{
"from": "xxxx",
"to": "xxxx",
"message_type": "text",
"text": "Hello! I’m CAI, blurb... .\n\n1. Okay I understand\n\nChoose any one option. Type \"1\" to choose first option.",
"channel" : "whatsapp"
}
I get this response
{
"Status": "fail",
"e3": {},
"a": {
"label": 6,
"trys": [
[
0,
6,
null,
7
]
],
"ops": []
},
"Message": "Cannot read property 'Type' of undefined"
}
I would be grateful if someone could help me shape the message that needs to be sent to vonage to that this can be sent out to the end user \ recipient correct.
Thanks
Simon

How to configure Krakend so it return http redirect response as-is instead of following the http redirect?

I am currently using Krakend (https://krakend.io) API Gateway to proxy request to my backend service. One of my backend service API response is a redirect response with http 303. The redirect response looks like this below :
HTTP/1.1 303 See Other
content-length: 48
content-type: text/plain; charset=utf-8
date: Thu, 16 Jul 2020 10:25:41 GMT
location: https://www.detik.com/
vary: Accept
x-powered-by: Express
x-envoy-upstream-service-time: 17
server: istio-envoy
The problem is that, instead of returning the http 303 response to client (with location response header) as-is, Krakend is actually following the http redirect and return the response of the redirect Url, which is the html response of https://www.detik.com/.
My current krakend configuration looks like this below :
{
"version": 2,
"extra_config": {
"github_com/devopsfaith/krakend-cors": {
"allow_origins": [],
"expose_headers": [
"Content-Length",
"Content-Type",
"Location"
],
"allow_headers": [
"Content-Type",
"Origin",
"X-Requested-With",
"Accept",
"Authorization",
"secret",
"Host"
],
"max_age": "12h",
"allow_methods": [
"GET",
"POST",
"PUT"
]
},
"github_com/devopsfaith/krakend-gologging": {
"level": "ERROR",
"prefix": "[GATEWAY]",
"syslog": false,
"stdout": true,
"format": "default"
},
"github_com/devopsfaith/krakend-logstash": {
"enabled": false
}
},
"timeout": "10000ms",
"cache_ttl": "300s",
"output_encoding": "json",
"name": "api-gateway",
"port": 8080,
"endpoints": [
{
"endpoint": "/ramatestredirect",
"method": "GET",
"extra_config": {},
"output_encoding": "no-op",
"concurrent_calls": 1,
"backend": [
{
"url_pattern": "/",
"encoding": "no-op",
"sd": "static",
"extra_config": {},
"method": "GET",
"host": [
"http://ramatestredirect.default.svc.cluster.local"
],
"disable_host_sanitize": false
}
]
}
]
}
So how can I make krakend to return original http 303 response unaltered from my backend service to the client ?
Thank You
I assume that you're calling this endpoint /ramatestredirect
To get backend http status code (as you said it return 303 http status code), you can use this way:
{
"endpoint": "/ramatestredirect",
"method": "GET",
"extra_config": {},
"output_encoding": "no-op",
"concurrent_calls": 1,
"backend": [
{
"url_pattern": "/",
"encoding": "no-op",
"sd": "static",
"extra_config": {
"github.com/devopsfaith/krakend/http": {
"return_error_details": "authentication"
}
},
"method": "GET",
"host": [
"http://ramatestredirect.default.svc.cluster.local"
],
"disable_host_sanitize": false
}
]
}
So, basically with this plugin you can get the original backend http status code
"github.com/devopsfaith/krakend/http": {
"return_error_details": "authentication"
}
If you use Lura Framework (formerly known as Kraken framework), then you may have to disable redirects for your http client.
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}

Karate: Get all users and delete with the reference id

After my tests are completed, I would like to delete all the users that were created. I am able to delete one user at a time. Can we do this using conditional logic?
Feature file:
Given path 'users'
And header Authorization = authId
And header Accept = 'application/json;version=2'
When method Get
Then status 200
Then print 'Response Time: '+ responseTime + ' milliseconds'
* def resp = $
* def size = karate.sizeOf(resp)
* print 'Number of users: ' + size
This gives me below response:
[
{
"firstName": "Edit User",
"lastName": "API Test",
"emailAddress": "edituserapitesting#gmail.com",
"ada": true,
"isDeleted": false,
"alerts": [],
"links": [
{
"href": "https://someurl/api/users/0219360d-5ca6-42af-9e9c-10be1e32d219",
"rel": "self"
}
]
},
{
"firstName": "Create",
"lastName": "Test",
"emailAddress": "api#test.com",
"ada": true,
"isDeleted": false,
"alerts": [],
"links": [
{
"href": "https://someurl/api/users/5e3c9be1-2863-4a2d-85e9-966582b127ac",
"rel": "self"
}
]
},
{
"firstName": "Create",
"lastName": "Test",
"emailAddress": "api#test.com",
"ada": true,
"isDeleted": false,
"alerts": [],
"links": [
{
"href": "https://someurl/api/users/b6feb126-bca2-43e6-ba2d-87aaae81fef0",
"rel": "self"
}
]
},
{
"firstName": "Create",
"lastName": "Test",
"emailAddress": "api#test.com",
"ada": true,
"isDeleted": false,
"alerts": [],
"links": [
{
"href": "https://someurl/api/users/4f07f234-9606-4cf5-94ce-3a42fb11a6d7",
"rel": "self"
}
]
}
]
I have to get the id [Eg:4f07f234-9606-4cf5-94ce-3a42fb11a6d7] from each json and pass it to delete path.
Please read about transforms: https://github.com/intuit/karate#json-transforms
Now in one line you can get an array of ids:
* def ids = karate.map(response, function(x){ var link = x.links[0].href; return link.substring(link.lastIndexOf('/') + 1) })
Now you can use this in a loop or data driven test and do what you want.

How to get access to user's google calendar from React Native (expo)?

I use Expo, and I want to add access to my app to users which have a Google account. Then I need to get info about Google calendars of the user which login into my app.
I implement login function using: Expo.Google.logInAsync(options) (https://docs.expo.io/versions/latest/sdk/google). My scope look like this:
scopes: ['https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/calendar.readonly']
When someone tries to login into my app, it asks about permission to see calendars list. In response I get:
Object {
"accessToken": "a",
"idToken": "b",
"refreshToken": "c",
"serverAuthCode": "d",
"type": "success",
"user": Object {
"email": "e",
"familyName": "f",
"givenName": "g",
"id": "h",
"name": "i",
"photoUrl": "j",
},
}
I received data about the user, but I don't have any data about its calendars.
I tried to get data about calendars (https://developers.google.com/calendar/v3/reference/calendarList) with this function:
getUsersCalendarList = async (accessToken) => {
let calendarsList = await fetch('https://www.googleapis.com/calenda/v3/users/me/calendarList', {
headers: { Authorization: `Bearer ${accessToken}`},
});
return calendarsList;
}
In response I got:
Response {
"_bodyBlob": Blob {
"_data": Object {
"blobId": "67b8b161-690f-4ff6-9cee-1dce12840ebd",
"offset": 0,
"size": 994,
},
},
"_bodyInit": Blob {
"_data": Object {
"blobId": "67b8b161-690f-4ff6-9cee-1dce12840ebd",
"offset": 0,
"size": 994,
},
},
"headers": Headers {
"map": Object {
"alt-svc": Array [
"quic=\":443\"; ma=2592000; v=\"44,43,39,35\"",
],
"cache-control": Array [
"public, max-age=0",
],
"content-type": Array [
"application/json; charset=UTF-8",
],
"date": Array [
"Thu, 17 Jan 2019 11:30:32 GMT",
],
"expires": Array [
"Thu, 17 Jan 2019 11:30:32 GMT",
],
"server": Array [
"GSE",
],
"vary": Array [
"X-Origin",
],
"x-content-type-options": Array [
"nosniff",
],
"x-frame-options": Array [
"SAMEORIGIN",
],
"x-xss-protection": Array [
"1; mode=block",
],
},
},
"ok": false,
"status": 403,
"statusText": undefined,
"type": "default",
"url": "https://www.googleapis.com/calendar/v3/users/me/calendarList",
}
How can I get a list of user's google calendars in Expo?
I find the solution here: React-Native JSON fetch from URL. One needs to use json() function on returned object.
The getUsersCalendarList schulde look like this:
getUsersCalendarList = async (accessToken) => {
let calendarsList = await fetch('https://www.googleapis.com/calenda/v3/users/me/calendarList', {
headers: { Authorization: `Bearer ${accessToken}`},
});
return calendarsList.json();
}
You can also add the access token as a parameter on the request.
https://www.googleapis.com/calenda/v3/users/me/calendarList?access_token={token}
I am not a react dev so not exactly sure how to fix your header. It looks ok.

Axios Request Does Not Return a Token

i'm trying to apply token for my request. So i tried to console.log the result of the request, and cannot find any token there inside the object array.
Object {
"config": Object {
"adapter": [Function xhrAdapter],
"data": "ktp=3578270708950002&member=199508070003",
"headers": Object {
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/x-www-form-urlencoded",
},
"maxContentLength": -1,
"method": "post",
"timeout": 0,
"transformRequest": Object {
"0": [Function transformRequest],
},
"transformResponse": Object {
"0": [Function transformResponse],
},
"url": "http://103.53.10.122/mobile/LoginCheck.php",
"validateStatus": [Function validateStatus],
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
},
"data": Array [
Object {
"status": "67457",
},
],
"headers": Object {
"connection": "keep-alive",
"content-type": "text/html; charset=UTF-8",
"date": "Fri, 22 Dec 2017 05:38:21 GMT",
"server": "nginx",
"transfer-encoding": "chunked",
"vary": "Accept-Encoding",
},
"request": XMLHttpRequest {
"DONE": 4,
"HEADERS_RECEIVED": 2,
"LOADING": 3,
"OPENED": 1,
"UNSENT": 0,
"_aborted": false,
"_cachedResponse": undefined,
"_hasError": false,
"_headers": Object {
"accept": "application/json, text/plain, */*",
"content-type": "application/x-www-form-urlencoded",
},
"_incrementalEvents": false,
"_lowerCaseResponseHeaders": Object {
"connection": "keep-alive",
"content-type": "text/html; charset=UTF-8",
"date": "Fri, 22 Dec 2017 05:38:21 GMT",
"server": "nginx",
"transfer-encoding": "chunked",
"vary": "Accept-Encoding",
},
"_method": "POST",
"_requestId": null,
"_response": "[{\"status\":\"67457\"}]",
"_responseType": "",
"_sent": true,
"_subscriptions": Array [],
"_timedOut": false,
"_trackingName": "unknown",
"_url": "http://103.53.10.122/mobile/LoginCheck.php",
"readyState": 4,
"responseHeaders": Object {
"Connection": "keep-alive",
"Content-Type": "text/html; charset=UTF-8",
"Date": "Fri, 22 Dec 2017 05:38:21 GMT",
"Server": "nginx",
"Transfer-Encoding": "chunked",
"Vary": "Accept-Encoding",
},
"responseURL": "http://103.53.10.122/mobile/LoginCheck.php",
"status": 200,
"timeout": 0,
"upload": XMLHttpRequestEventTarget {},
"withCredentials": true,
},
"status": 200,
"statusText": undefined,
}
Can someone point me how to add token to authenticate, because i'm still confuse about the concept even after reading it. So if i'm not mistaken, i should do the following in order:
Generate the token when user successfully login
Save the token in local storage
Use the token for each request (How does the backend check the validity of the token?)
Any help would be appreciated
Depending on the way the backend handles API requests you should either use axios.get and append the token to the URL or use axios.post and pass a object as body to the method with your token.
E.g.
axios.post('http://103.53.10.122/mobile/LoginCheck.php', {
username: "test",
password: "1234"
})
.then((res) => {
console.log(res);
/*
In this example I assume that res.data has the token returned from the backend
The res.data should look like this then:
{
token: "1234"
}
*/
let token = res.data.token;
AsyncStorage.setItem("token", token);
})
.catch((err) => {
console.log(err);
});
To use the token for each request save it in AsyncStorage for persistent storage or in the redux state if you are using redux.
On the server side you could generate a JSON web token which contains all data you need for authenticating the user and validating it with the data stored in a database. Due to the fact that you are using PHP I can recommend you this introduction to JSON web tokens in combination with PHP: https://www.sitepoint.com/php-authorization-jwt-json-web-tokens/