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

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’.

unnamed.png

 

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).

 

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: