Getting Started Console Example - Cloud This example shows how to use the 51Degrees Cloud service to determine details about a device based on its User-Agent and User-Agent Client Hint HTTP header values.
42 from pathlib
import Path
45 from fiftyone_pipeline_core.logger
import Logger
46 from fiftyone_pipeline_core.pipelinebuilder
import PipelineBuilder
48 from ..example_utils
import ExampleUtils
50 class GettingStartedConsole():
51 def run(self, config, logger, output):
58 pipeline = PipelineBuilder().add_logger(logger).build_from_configuration(config)
61 for values
in self.EvidenceValues:
62 self.analyseEvidence(values, pipeline, output)
64 def analyseEvidence(self, evidence, pipeline, output):
73 data = pipeline.create_flowdata()
78 message.append(
"Input values:\n")
80 message.append(f
"\t{key}: {evidence[key]}\n")
82 output(
"".join(message))
85 data.evidence.add_from_dict(evidence)
91 message.append(
"Results:\n")
103 self.outputValue(
"Mobile Device", device.ismobile, message)
104 self.outputValue(
"Platform Name", device.platformname, message)
105 self.outputValue(
"Platform Version", device.platformversion, message)
106 self.outputValue(
"Browser Name", device.browsername, message)
107 self.outputValue(
"Browser Version", device.browserversion, message)
108 output(
"".join(message))
110 def outputValue(self, name, value, message):
119 f
"\t{name}: {value.value()}\n" if value.has_value()
120 else f
"\t{name}: {value.no_value_message()}\n")
127 {
"header.user-agent":
128 "Mozilla/5.0 (Linux; Android 9; SAMSUNG SM-G960U) " 129 "AppleWebKit/537.36 (KHTML, like Gecko) " 130 "SamsungBrowser/10.1 Chrome/71.0.3578.99 Mobile Safari/537.36" },
132 {
"header.user-agent":
133 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " 134 "AppleWebKit/537.36 (KHTML, like Gecko) " 135 "Chrome/78.0.3904.108 Safari/537.36" },
138 {
"header.user-agent":
139 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " 140 "AppleWebKit/537.36 (KHTML, like Gecko) " 141 "Chrome/98.0.4758.102 Safari/537.36",
142 "header.sec-ch-ua-mobile":
"?0",
144 "\" Not A; Brand\";v=\"99\", \"Chromium\";v=\"98\", " 145 "\"Google Chrome\";v=\"98\"",
146 "header.sec-ch-ua-platform":
"\"Windows\"",
147 "header.sec-ch-ua-platform-version":
"\"14.0.0\"" }
153 resource_key = argv[0]
if len(argv) > 0
else ExampleUtils.get_resource_key()
156 logger = Logger(min_level=
"info")
159 configFile = Path(__file__).resolve().parent.joinpath(
"gettingstarted_console.json").read_text()
160 config = json5.loads(configFile)
163 resourceKeyFromConfig = ExampleUtils.get_resource_key_from_config(config)
164 configHasKey = resourceKeyFromConfig
and resourceKeyFromConfig.startswith(
"!!") ==
False 168 if configHasKey ==
False:
169 ExampleUtils.set_resource_key_in_config(config, resource_key)
172 if not ExampleUtils.get_resource_key_from_config(config):
174 "No resource key specified in the configuration file " +
175 "'gettingstarted_console.json' or the environment variable " +
176 f
"'{ExampleUtils.RESOURCE_KEY_ENV_VAR}'. The 51Degrees cloud " +
177 "service is accessed using a 'ResourceKey'. For more information " +
179 "https://51degrees.com/documentation/_info__resource_keys.html. " +
180 "A resource key with the properties required by this example can be " +
181 "created for free at https://configure.51degrees.com/1QWJwHxl. " +
182 "Once complete, populate the config file or environment variable " +
183 "mentioned at the start of this message with the key.")
185 GettingStartedConsole().
run(config, logger,
print)
187 if __name__ ==
"__main__":