Good Simple Demo App HTTP POST Login then HTTP GET and parse?

Is there a good simple example SmartApp that uses HTTP POST to send a login / pass to an cloud server, then stores the returned token to later make an HTTP GET request? Even better if it can also handle a refresh token.

Long of the short I’m working to build my own Whistle 3 Pet Presence solution, and I’ve done enough tests with Postman, that I know the actual requests and the data to expect, and now it is time to build the SmartApp. They don’t have a published API, so I’ve had to dig and test to even get this far. I’ve built groovy apps way back in the pre-CoRE days, but my expertise is very limited, so I am hoping to find an app with a similar flow to leverage.

Is an Ajax query the closest thing to Groovy?

Basically I need to:

  1. Send POST to https://app.whistle.com/api/login
    with some headers and my username and pass in the body
var form = new FormData(); form.append("email", "MyUsername"); form.append("password", "MyPassword");

var settings = {
“async”: true,
“crossDomain”: true,
“url”: “https://app.whistle.com/api/login”,
“method”: “POST”,
“headers”: {
“Connection”: “keep-alive”,
“Content-Type”: “application/json”,
“Accept”: “application/vnd.whistle.com.v4+json”,
“Accept-Language”: “en-us”,
“Accept-Encoding”: “br, gzip, deflate”,
“User-Agent”: “Winston/2.5.3 (iPhone; iOS 12.0.1; Build:1276; Scale/2.0)”,
“cache-control”: “no-cache”,
“Postman-Token”: “4f040cd7-59db-4bd8-90cd-71d6ef3c5fd6”
},
“processData”: false,
“contentType”: false,
“mimeType”: “multipart/form-data”,
“data”: form
}

$.ajax(settings).done(function (response) {
console.log(response);
});

  1. This returns an “auth_token” and “refresh_token” and a bunch of other crap I don’t care about.

  2. I need to then store and pass that as a barer token in a GET request. Could I theoretically just manually use the same auth token in perpetuity? Or does it need to be refreshed? How can I do that intelligently? Then to get the data I’m after:

var settings = { "async": true, "crossDomain": true, "url": "https://app.whistle.com/api/pets", "method": "GET", "headers": { "Connection": "keep-alive", "Content-Type": "application/json", "Accept": "application/vnd.whistle.com.v4+json", "Accept-Language": "en-us", "Accept-Encoding": "br, gzip, deflate", "User-Agent": "Winston/2.5.3 (iPhone; iOS 12.0.1; Build:1276; Scale/2.0)", "Authorization": "Bearer TOKEN", "cache-control": "no-cache", "Postman-Token": "bf3b6f3d-b9db-4462-9606-1dae45131189" } }

$.ajax(settings).done(function (response) {
console.log(response);
});

From the result, I am just looking to parse one value in a single json key that corresponds to the dog being home or not. If it matches I’ll set a simulated presence sensor as present, if not, not present.

Seems like it should be straight forward, but I’m starting at ground zero with all my Groovy experience leveraging the work of others.

Just wanted to point you in (hopefully) the right direction – you should be able to just make POST requests in your groovy smartapp. In our public repository of groovy smartapps, there’s plenty of examples of people using httpPost() as a frame of reference.

1 Like