Another example of invoking a REST/JSON web service - resolving User-Agent strings

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Another example of invoking a REST/JSON web service - resolving User-Agent strings

Sven Van Caekenberghe-2
In one of our web applications, when a user logs in, we give some meta/context information, like this:

 You are user /users/4 in entity context /entities/0 for T3 using timezone Europe/Brussels [offset -1]
 Last login Fri Nov 10 2017 15:15:46 from 80.89.7.35 using Safari 11 on Mac OS X 10
 Your login count is 10,375

The last login info functions as a soft security measure. In it, the part after using, 'Safari 11 on Mac OS X 10', is computed from the (previous) User-Agent string of the web browser when the user last logged in. But these strings are quite complex. There exist web services that parse these complex user agent strings and extract all possible information out of them, including the high level description used above.

The previous solution stopped working and I configured a new one today [https://www.whatismybrowser.com], the core implementation of which I would like to share as yet another example.

(ZnClient new
  systemPolicy;
  url: 'https://api.whatismybrowser.com/api/v2/user_agent_parse';
  accept: ZnMimeType applicationJson;
  headerAt: 'x-api-key' put: '8cf7cee797f3c68d59055eac96778000';
  contentReader: [ :entity | entity ifNotNil: [ NeoJSONObject fromString: entity contents ] ];
  contentWriter: [ :object | ZnEntity json: object asString ];
  contents: ({ 'user_agent' -> 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/604.3.5 (KHTML, like Gecko) Version/11.0.1 Safari/604.3.5' } as: NeoJSONObject);
  post) parse at: 'simple_software_string'.



You have to create an account and insert your own API key. NeoJSON must be loaded.

Using #systemPolicy and #accept: makes this call throw an exception unless valid JSON is returned in a 2XX response. NeoJSONObject is a convenient way to use the NeoJSON infrastructure. The #parse message accesses the property by the same name (via a DNU), as does the #at: message (for keys that cannot be written as message sends).

Sven


Reply | Threaded
Open this post in threaded view
|

Re: Another example of invoking a REST/JSON web service - resolving User-Agent strings

Stephane Ducasse-3
Hi sven

I love this. With Guille we are thinking that it would be cool to have blog entries on Pharo how to do. 
And this post could be the first one. 

Stef

On Fri, Nov 10, 2017 at 4:43 PM, Sven Van Caekenberghe <[hidden email]> wrote:
In one of our web applications, when a user logs in, we give some meta/context information, like this:

 You are user /users/4 in entity context /entities/0 for T3 using timezone Europe/Brussels [offset -1]
 Last login Fri Nov 10 2017 15:15:46 from 80.89.7.35 using Safari 11 on Mac OS X 10
 Your login count is 10,375

The last login info functions as a soft security measure. In it, the part after using, 'Safari 11 on Mac OS X 10', is computed from the (previous) User-Agent string of the web browser when the user last logged in. But these strings are quite complex. There exist web services that parse these complex user agent strings and extract all possible information out of them, including the high level description used above.

The previous solution stopped working and I configured a new one today [https://www.whatismybrowser.com], the core implementation of which I would like to share as yet another example.

(ZnClient new
  systemPolicy;
  url: 'https://api.whatismybrowser.com/api/v2/user_agent_parse';
  accept: ZnMimeType applicationJson;
  headerAt: 'x-api-key' put: '8cf7cee797f3c68d59055eac96778000';
  contentReader: [ :entity | entity ifNotNil: [ NeoJSONObject fromString: entity contents ] ];
  contentWriter: [ :object | ZnEntity json: object asString ];
  contents: ({ 'user_agent' -> 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/604.3.5 (KHTML, like Gecko) Version/11.0.1 Safari/604.3.5' } as: NeoJSONObject);
  post) parse at: 'simple_software_string'.



You have to create an account and insert your own API key. NeoJSON must be loaded.

Using #systemPolicy and #accept: makes this call throw an exception unless valid JSON is returned in a 2XX response. NeoJSONObject is a convenient way to use the NeoJSON infrastructure. The #parse message accesses the property by the same name (via a DNU), as does the #at: message (for keys that cannot be written as message sends).

Sven