\r\n

51Degrees Location Python  4.4

Reverse Geo-Location services for 51Degrees Pipeline

cloud/web.py

This example shows how to use 51Degrees on-premise device detection in a web context to determine details about a device based on its User-Agent and User-Agent Client Hint HTTP header values.

We demonstrate how to use the web integration feature to:

  • Pass the required information from the web context to the pipeline API.
  • Populate relevant response headers such as Accept-CH for User-Agent Client Hints
  • Acquire and process evidence from client-side JavaScript. This is used to determine iPhone / iPad models.

To run this example, you will need to create a resource key. The resource key is used as shorthand to store the particular set of properties you are interested in as well as any associated license keys that entitle you to increased request limits and/or paid-for properties.

You can create a resource key using the 51Degrees Configurator.

1 # *********************************************************************
2 # This Original Work is copyright of 51 Degrees Mobile Experts Limited.
3 # Copyright 2022 51 Degrees Mobile Experts Limited, Davidson House,
4 # Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
5 #
6 # This Original Work is licensed under the European Union Public Licence
7 # (EUPL) v.1.2 and is subject to its terms as set out below.
8 #
9 # If a copy of the EUPL was not distributed with this file, You can obtain
10 # one at https://opensource.org/licenses/EUPL-1.2.
11 #
12 # The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
13 # amended by the European Commission) shall be deemed incompatible for
14 # the purposes of the Work and the provisions of the compatibility
15 # clause in Article 5 of the EUPL shall not apply.
16 #
17 # If using the Work as, or as part of, a network application, by
18 # including the attribution notice(s) required under Article 5 of the EUPL
19 # in the end user terms of the application under an appropriate heading,
20 # such notice(s) shall fulfill the requirements of that article.
21 # *********************************************************************
22 
23 
28 
29 from fiftyone_location.location_pipelinebuilder import LocationPipelineBuilder
30 from fiftyone_pipeline_core.web import webevidence
31 import json
32 
33 # First create the location pipeline with the desired settings.
34 
35 # You need to create a resource key at https://configure.51degrees.com
36 # and paste it into the code, replacing !!YOUR_RESOURCE_KEY!! below.
37 # Alternatively, add a resource_key environment variable
38 import os
39 if "resource_key" in os.environ:
40  resource_key = os.environ["resource_key"]
41 else:
42  resource_key = "!!YOUR_RESOURCE_KEY!!"
43 
44 if resource_key == "!!YOUR_RESOURCE_KEY!!":
45  print("""
46  You need to create a resource key at
47  https://configure.51degrees.com and paste it into the code,
48  'replacing !!YOUR_RESOURCE_KEY!!
49  To get a resourcekey with the properties used in this example go to https://configure.51degrees.com/GCrtGh1L
50  """)
51 else:
52 
53  # Here we add some callback settings for the page to make a request with extra evidence from the client side, in this case the Flask /json route we will make below
54 
55  javascript_builder_settings = {
56  "endpoint": "/json"
57  }
58 
59  pipeline = LocationPipelineBuilder(resource_key=resource_key, settings={"javascript_builder_settings": javascript_builder_settings}).build()
60 
61  from flask import Flask, request
62 
63  app = Flask(__name__)
64 
65  # First we make a JSON route that will be called from the client side and will return
66  # a JSON encoded property database using any additional evidence provided by the client
67 
68  @app.route('/json', methods=['POST'])
69  def jsonroute():
70 
71  # Create the flowData object for the JSON route
72  flowData = pipeline.create_flowdata()
73 
74  # Add any information from the request (headers, cookies and additional
75  # client side provided information)
76 
77  flowData.evidence.add_from_dict(webevidence(request))
78 
79  # Process the flowData
80  flowData.process()
81 
82  # Return the JSON from the JSONBundler engine
83  return json.dumps(flowData.jsonbundler.json)
84 
85  # Helper function to get a property value if it exists and return
86  # the reason why if it doesn't
87  def getValueHelper(flowdata, engine, propertyKey):
88 
89  engineProperties = getattr(flowdata, engine)
90 
91  propertyValue = getattr(engineProperties, propertyKey)
92 
93  if propertyValue.hasValue():
94  return propertyValue.value()
95  else:
96  return propertyValue.no_value_message()
97 
98  # In the main route we dynamically update the screen's device property display
99  # using the above JSON route
100 
101  @app.route('/')
102  def server():
103 
104  # Create the flowData object for the JSON route
105  flowData = pipeline.create_flowdata()
106 
107  # Add any information from the request (headers, cookies and additional
108  # client side provided information)
109 
110  flowData.evidence.add_from_dict(webevidence(request))
111 
112  # Process the flowData
113 
114  flowData.process()
115 
116  # Generate the HTML
117  output = ""
118 
119  # Add the JavaScript created by the pipeline
120  output += "<script>"
121  output += flowData.javascriptbuilder.javascript
122  output += "</script>"
123 
124 
125  output += "<h1>Client side evidence</h1>"
126 
127  # Print a button which gets the user's location information
128  # This is then sent to the previously specified json endpoint
129  # Data comes back from the request and populates the country in the HTML
130 
131  output += """
132 
133  <p>After you select the "use my location" button below, client side JavaScript will
134  update the country field using this information.</p>
135 
136  <p>Country: <span id=country></span></p>
137 
138  <button type="button" onclick="getLocation()">Use my location</button>
139 
140  <script>
141 
142  let getLocation = function() {
143  fod.complete(function (data) {
144  document.getElementById("country").innerHTML = data.location.country
145  }, 'location');
146  }
147 
148  </script>
149 
150 
151  """
152 
153  return output