\r\n

51Degrees Device Detection Python  4.4

Device Detection services for 51Degrees Pipeline

cloud/metadata_console.py

The cloud service exposes meta data that can provide additional information about the various properties that might be returned. This example shows how to access this data and display the values available.

A list of the properties will be displayed, along with some additional information about each property. Note that this is the list of properties used by the supplied resource key, rather than all properties that can be returned by the cloud service.

In addition, the evidence keys that are accepted by the service are listed. These are the keys that, when added to the evidence collection in flow data, could have some impact on the result that is returned.

Bear in mind that this is a list of ALL evidence keys accepted by all products offered by the cloud. If you are only using a single product (for example - device detection) then not all of these keys will be relevant.

This example is available in full on GitHub.

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.

Required PyPi Dependencies:

1 # *********************************************************************
2 # This Original Work is copyright of 51 Degrees Mobile Experts Limited.
3 # Copyright 2023 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 
48 
49 from pathlib import Path
50 import sys
51 from fiftyone_devicedetection.devicedetection_pipelinebuilder import DeviceDetectionPipelineBuilder
52 from fiftyone_pipeline_core.logger import Logger
53 from fiftyone_pipeline_core.basiclist_evidence_keyfilter import BasicListEvidenceKeyFilter
54 # pylint: disable=E0402
55 from ..example_utils import ExampleUtils
56 
57 bgRed = "\u001b[41m"
58 fgWhite = "\u001b[37;1m"
59 colReset = "\u001b[0m"
60 
61 class MetaDataConsole():
62  def run(self, resource_key, logger, output):
63 
64  pipeline = DeviceDetectionPipelineBuilder(
65  resource_key = resource_key).add_logger(logger).build()
66 
67  self.outputProperties(pipeline.get_element("device"), output)
68  # We use the CloudRequestEngine to get evidence key details, rather than the
69  # DeviceDetectionCloudEngine.
70  # This is because the DeviceDetectionCloudEngine doesn't actually make use
71  # of any evidence values. It simply processes the JSON that is returned
72  # by the call to the cloud service that is made by the CloudRequestEngine.
73  # The CloudRequestEngine is actually taking the evidence values and passing
74  # them to the cloud, so that's the engine we want the keys from.
75  self.outputEvidenceKeyDetails(pipeline.get_element("cloud"), output)
76 
77 
78  def outputEvidenceKeyDetails(self, engine, output):
79  if (issubclass(type(engine.get_evidence_key_filter()), BasicListEvidenceKeyFilter)):
80 
81  # If the evidence key filter extends BasicListEvidenceKeyFilter then we can
82  # display a list of accepted keys.
83  filter = engine.get_evidence_key_filter()
84  output("Accepted evidence keys:")
85  for key in filter.list:
86  output(f"\t{key}")
87  else:
88  output("The evidence key filter has type " +
89  f"{type(engine.get_evidence_key_filter())}. As this does not extend " +
90  "BasicListEvidenceKeyFilter, a list of accepted values cannot be " +
91  "displayed. As an alternative, you can pass evidence keys to " +
92  "filter.filter_evidence_key(string) to see if a particular key will be included " +
93  "or not.")
94  output("For example, header.user-agent is " +
95  ("" if engine.get_evidence_key_filter().filter_evidence_key("header.user-agent") else "not ") +
96  "accepted.")
97 
98 
99  def outputProperties(self, engine, output):
100  for propertyName, property in engine.get_properties().items():
101  # Output some details about the property.
102  # If we're outputting to console then we also add some formatting to make it
103  # more readable.
104  output(f"{bgRed}{fgWhite}Property - {propertyName}{colReset}" +
105  f"[Category: {property['category']}] ({property['type']})")
106 
107 def main(argv):
108  # Use the command line args to get the resource key if present.
109  # Otherwise, get it from the environment variable.
110  resource_key = argv[0] if len(argv) > 0 else ExampleUtils.get_resource_key()
111 
112  # Configure a logger to output to the console.
113  logger = Logger(min_level="info")
114 
115  if (resource_key):
116  MetaDataConsole().run(resource_key, logger, print)
117 
118  else:
119  logger.log("error",
120  "No resource key specified in environment variable " +
121  f"'{ExampleUtils.RESOURCE_KEY_ENV_VAR}'. The 51Degrees " +
122  "cloud service is accessed using a 'ResourceKey'. " +
123  "For more detail see " +
124  "http://51degrees.com/documentation/4.3/_info__resource_keys.html. " +
125  "A resource key with the properties required by this " +
126  "example can be created for free at " +
127  "https://configure.51degrees.com/1QWJwHxl. " +
128  "Once complete, populate the environment variable " +
129  "mentioned at the start of this message with the key.")
130 
131 if __name__ == "__main__":
132  main(sys.argv[1:])