CHAPTER 7

RESTful API

Now you know all about algorithms and how to benchmark them. Once you found the optimal algorithm and settings, however, what do you do with them?

One common way to make your findings, that is, a number of articles most highly recommended for a given customer, accessible to those who need to know is to provide your recommendations as a software service. More specifically, your recommendations are served up by a webserver, typically in JSON format and accessed by your clients via a RESTful API.

To see how that works, we first need to take care the serving-up part. This is what happens in the file 07_RESTfulAPI.py, which is not a jupyter notebook but a regular python script. So just open it in a text editor (or an IDE) to read the ample documentation and then execute it on the command line by typing

python 07_RESTfulAPI.py

on your shell prompt.

You then have a web server running, which you can access in your browser via the URL

http://localhost:<port>

where <port> is the number displayed on the command line once the server is up. Feel free to play around with what you see there. As usual, it pays off to keep an eye on the logfile to stay ahead of things.

The REST client

Now that the server is running, we still need a client that accesses it and retrieves the recommendations. Having played around with the server in our webbrowser, we know exactly what we have to do to get what we want.

Imports from the standard library


In [1]:
from urllib.parse import ParseResult
from urllib.request import urlopen
import json

Setup

First we assemble the URL pointing to where we will receive a JSON with some exemplary customer IDs.


In [2]:
base_path = '/user/'
base_query = 'n_recos='
url_params = {'scheme': 'http',
              'netloc': 'localhost:5002',
              'path': base_path, 
              'params': '',
              'query': 'n_recos=6',
              'fragment': ''}
rest = ParseResult(**url_params)

Read and print customer IDs

Then, we read a JSON from that URL and print what we got.


In [3]:
with urlopen(rest.geturl()) as url:
    users = json.loads(url.read())

print('User IDs retrieved from server')
print('==============================')
print(users)


User IDs retrieved from server
==============================
{'some user IDs': ['4', '1', '12', '11', '10', '7', '13', '16', '17', '19']}

Get recommendations for these users

Now that we have some customer IDs, we specify below how many recommendations we want for each one, retrieve and print them.

Note: If you want to do this again (because it is really cool, now, isnt't it), you can not simply re-run the cell below. Instead, you will have to start all over again and execute all three cells(starting below the imports, of course).


In [4]:
n_recos = 6  # Specify here how many recommendations you want for each customer!

query = base_query + str(n_recos)
rest = rest._replace(query=query)

for user_id in users.pop('some user IDs'):
    path = base_path + user_id
    rest = rest._replace(path=path)
    
    with urlopen(rest.geturl()) as url:
        list_of = json.loads(url.read())
        
    print('Recommendations for user', user_id)
    print('===========================')
    for article in list_of['recommended articles']:
        print(article)
    print()


Recommendations for user 4
===========================
BL152EL85CRUALID-1814
MA172TL16AQPANID-36449
NE739EL91DUIANID-38875
KI593EL69ASKANID-36520
NE739EL06ORLANID-27491
KI593EL68ASLANID-36521

Recommendations for user 1
===========================
NE739EL91DUIANID-38875
SO888EL82CKFALID-1617
BL152EL67KCUALID-6832
BL152EL92QAPANID-28441
SA848EL74DPHALID-2425
AP082EL01CFQALID-1498

Recommendations for user 12
===========================
SA848EL55JGEALID-6244
MA130HL31AQAANID-36434
SA848EL74DPHALID-2425
BL152EL67KCUALID-6832
NO749EL20DJRALID-2279
AP082EL03BMIALID-996

Recommendations for user 11
===========================
VI962EL69EEWALID-2830
SA848EL74DPHALID-2425
MO721EL43DIUALID-2256
NE739EL06ORLANID-27491
AP082EL37CPUALID-1762
NO749EL20DJRALID-2279

Recommendations for user 10
===========================
MA130HL31AQAANID-36434
SA848EL55JGEALID-6244
NE739EL91DUIANID-38875
AP082EL37CPUALID-1762
SA848EL83DOYALID-2416
BL152EL67KCUALID-6832

Recommendations for user 7
===========================
AP082EL01CFQALID-1498
NO749EL20DJRALID-2279
DO274EL91APSALID-408
BL152EL82CRXALID-1817
SA848EL83DOYALID-2416
AP082EL37CPUALID-1762

Recommendations for user 13
===========================
SA848EL74DPHALID-2425
AP082EL03BMIALID-996
NO749EL20DJRALID-2279
CA189EL29AGOALID-170
SA848EL83DOYALID-2416
NE739EL06ORLANID-27491

Recommendations for user 16
===========================
MI183ME53QZEANID-29126
SA395EL19ZEAANID-35321
TA236HB95VBGANID-32327
FU298HL77AKKANID-36272
BE129EL00TOPANID-31206
SA848EL74DPHALID-2425

Recommendations for user 17
===========================
NO749EL20DJRALID-2279
TE440HL28AXVANID-36669
AP082EL37CPUALID-1762
NE739EL06ORLANID-27491
MA130HL29AQCANID-36436
MA130HL31AQAANID-36434

Recommendations for user 19
===========================
PH789EL03ATCALID-496
NO749EL20DJRALID-2279
AS100EL41BOSALID-1058
WI981EL52EFNALID-2847
OL756EL65HDYALID-4834
VI962EL62EFDALID-2837

Clearly the way to go, eh? Make sure to terminate the webserver (by pressing Ctrl-C on the command line) once you are done exploring and playing.


In [ ]: