Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,13 @@ def __init__(self, settings = None, usage_sharing = True, **kwargs):
* enable_cookies - whether cookies should be enabled
* minify: Whether to minify the JavaScript
@type cache: Cache
@param cache: An instance of the fiftyone.pipeline.engines.cache, for the cloud engine only. The on-premise engine raises an exception if one is supplied
class
@param cache: An instance of the fiftyone.pipeline.engines.cache
class, for the cloud only. It is added to the Cloud Request Engine,
which keys it on the evidence it sends and applies its own cache
rules. One of them sends a request that carries a session id to the
cloud service every time, and every request carries one while the
JavaScript builder is on. The on-premise engine raises an exception
if one is supplied.

Cloud Engine Settings:

Expand Down Expand Up @@ -150,19 +155,22 @@ def __init__(self, settings = None, usage_sharing = True, **kwargs):
self._usage_sharing = None

if "data_file_path" in settings:
# The engine raises an exception if a cache is supplied.
del settings["settings"]
device = DeviceDetectionOnPremise(**settings)

if usage_sharing:
self._usage_sharing = ShareUsage()

else :
self.add(CloudRequestEngine(settings))
request_engine = CloudRequestEngine(settings)

if "cache" in settings:
request_engine.set_cache(settings["cache"])

self.add(request_engine)
device = DeviceDetectionCloud()

if "cache" in settings:
device.set_cache(settings["cache"])

self.add(device)

if self._usage_sharing is not None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ def __init__(self, settings = None, usage_sharing = True, **kwargs):
* minify: Whether to minify the JavaScript
@type cache: Cache
@param cache: An instance of the fiftyone.pipeline.engines.cache
class
class. It is added to the Cloud Request Engine, which keys it on
the evidence it sends and applies its own cache rules. One of them
sends a request that carries a session id to the cloud service
every time, and every request carries one while the JavaScript
builder is on.

Cloud Engine Settings:

Expand All @@ -81,11 +85,11 @@ def __init__(self, settings = None, usage_sharing = True, **kwargs):

# Add specific engines

self.add(CloudRequestEngine(settings))
device = DeviceDetectionCloud()
request_engine = CloudRequestEngine(settings)

if "cache" in settings:
device.set_cache(settings["cache"])

self.add(device)
request_engine.set_cache(settings["cache"])

self.add(request_engine)
self.add(DeviceDetectionCloud())

Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# *********************************************************************
# This Original Work is copyright of 51 Degrees Mobile Experts Limited.
# Copyright 2026 51 Degrees Mobile Experts Limited, Davidson House,
# Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
#
# This Original Work is licensed under the European Union Public Licence
# (EUPL) v.1.2 and is subject to its terms as set out below.
#
# If a copy of the EUPL was not distributed with this file, You can obtain
# one at https://opensource.org/licenses/EUPL-1.2.
#
# The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
# amended by the European Commission) shall be deemed incompatible for
# the purposes of the Work and the provisions of the compatibility
# clause in Article 5 of the EUPL shall not apply.
#
# If using the Work as, or as part of, a network application, by
# including the attribution notice(s) required under Article 5 of the EUPL
# in the end user terms of the application under an appropriate heading,
# such notice(s) shall fulfill the requirements of that article.
# *********************************************************************

"""!
The Device Detection Cloud Pipeline Builder adds its cache setting to the
Cloud Request Engine, which keys it on the evidence it sends. These tests
need no network connection and no resource key.
"""

import json
import unittest
from unittest.mock import Mock

from requests import Response

from fiftyone_pipeline_cloudrequestengine.requestclient import RequestClient
from fiftyone_pipeline_engines.lru_cache import LRUEngineCache

from fiftyone_devicedetection_cloud.devicedetection_cloud_pipelinebuilder import DeviceDetectionCloudPipelineBuilder

IPHONE = "Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X)"
ANDROID = "Mozilla/5.0 (Linux; Android 14; Pixel 8)"


class StandInCloud(RequestClient):
"""!
Stands in for the cloud service, answering each request for data with
the User-Agent it was sent as the browser name, and counting them.
"""

def __init__(self):
self.data_requests = 0

def request(self, type, url, content, originHeader, timeout_seconds=None):
if "accessibleProperties" in url:
body = {"Products": {"device": {
"DataTier": "CloudV4Free",
"Properties": [{"Name": "BrowserName", "Type": "String",
"Category": "Browser"}]}}}
elif "evidencekeys" in url:
body = ["header.user-agent", "query.user-agent"]
else:
self.data_requests += 1
body = {"device": {
"browsername": dict(content or {}).get("user-agent")}}
response = Mock(spec=Response)
response.status_code = 200
response.text = json.dumps(body)
response.content = response.text.encode("utf-8")
response.json = lambda: json.loads(response.text)
return response


class BuilderCacheTests(unittest.TestCase):

def pipeline(self, cloud, cache, **settings):
pipeline = DeviceDetectionCloudPipelineBuilder(
resource_key="a-resource-key", http_client=cloud, cache=cache,
**settings).build()
self.addCleanup(pipeline.close)
return pipeline

@staticmethod
def browser_name(pipeline, user_agent):
data = pipeline.create_flowdata()
data.evidence.add("header.user-agent", user_agent)
data.process()
return data.device.browsername.value()

def test_the_cache_is_added_to_the_cloud_request_engine(self):
cache = LRUEngineCache()
pipeline = self.pipeline(StandInCloud(), cache)

self.assertIs(cache, pipeline.get_element("cloud").cache)
self.assertFalse(hasattr(pipeline.get_element("device"), "cache"))

def test_two_user_agents_give_two_results(self):
pipeline = self.pipeline(StandInCloud(), LRUEngineCache())

self.assertEqual(IPHONE, self.browser_name(pipeline, IPHONE))
self.assertEqual(ANDROID, self.browser_name(pipeline, ANDROID))

def test_the_same_user_agent_again_is_answered_from_the_cache(self):
# With the JavaScript builder, the sequence element gives every
# request a session id, which the Cloud Request Engine never
# answers from its cache, so the JavaScript builder is off here.
cloud = StandInCloud()
pipeline = self.pipeline(
cloud, LRUEngineCache(), add_javascript_builder=False)

self.assertEqual(IPHONE, self.browser_name(pipeline, IPHONE))
self.assertEqual(ANDROID, self.browser_name(pipeline, ANDROID))
self.assertEqual(IPHONE, self.browser_name(pipeline, IPHONE))
self.assertEqual(2, cloud.data_requests)


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# *********************************************************************
# This Original Work is copyright of 51 Degrees Mobile Experts Limited.
# Copyright 2026 51 Degrees Mobile Experts Limited, Davidson House,
# Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
#
# This Original Work is licensed under the European Union Public Licence
# (EUPL) v.1.2 and is subject to its terms as set out below.
#
# If a copy of the EUPL was not distributed with this file, You can obtain
# one at https://opensource.org/licenses/EUPL-1.2.
#
# The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
# amended by the European Commission) shall be deemed incompatible for
# the purposes of the Work and the provisions of the compatibility
# clause in Article 5 of the EUPL shall not apply.
#
# If using the Work as, or as part of, a network application, by
# including the attribution notice(s) required under Article 5 of the EUPL
# in the end user terms of the application under an appropriate heading,
# such notice(s) shall fulfill the requirements of that article.
# *********************************************************************

"""
The Device Detection builder, given a resource key, adds its cache setting
to the Cloud Request Engine, which keys it on the evidence it sends. The
fiftyone_devicedetection package has no tests of its own, so its builder is
tested here. These tests need no network connection and no resource key.
"""

import json
import unittest
from unittest.mock import Mock

from requests import Response

from fiftyone_pipeline_cloudrequestengine.requestclient import RequestClient
from fiftyone_pipeline_engines.lru_cache import LRUEngineCache
from fiftyone_devicedetection.devicedetection_pipelinebuilder import DeviceDetectionPipelineBuilder

IPHONE = "Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X)"
ANDROID = "Mozilla/5.0 (Linux; Android 14; Pixel 8)"


class StandInCloud(RequestClient):
"""
Stands in for the cloud service, answering each request for data with
the User-Agent it was sent as the browser name, and counting them.
"""

def __init__(self):
self.data_requests = 0

def request(self, type, url, content, originHeader, timeout_seconds=None):
if "accessibleProperties" in url:
body = {"Products": {"device": {
"DataTier": "CloudV4Free",
"Properties": [{"Name": "BrowserName", "Type": "String",
"Category": "Browser"}]}}}
elif "evidencekeys" in url:
body = ["header.user-agent", "query.user-agent"]
else:
self.data_requests += 1
body = {"device": {
"browsername": dict(content or {}).get("user-agent")}}
response = Mock(spec=Response)
response.status_code = 200
response.text = json.dumps(body)
response.content = response.text.encode("utf-8")
response.json = lambda: json.loads(response.text)
return response


class BuilderCacheTests(unittest.TestCase):

def pipeline(self, cloud, cache, **settings):
pipeline = DeviceDetectionPipelineBuilder(
resource_key="a-resource-key", http_client=cloud, cache=cache,
**settings).build()
self.addCleanup(pipeline.close)
return pipeline

@staticmethod
def browser_name(pipeline, user_agent):
data = pipeline.create_flowdata()
data.evidence.add("header.user-agent", user_agent)
data.process()
return data.device.browsername.value()

def test_the_cache_is_added_to_the_cloud_request_engine(self):
cache = LRUEngineCache()
pipeline = self.pipeline(StandInCloud(), cache)

self.assertIs(cache, pipeline.get_element("cloud").cache)
self.assertFalse(hasattr(pipeline.get_element("device"), "cache"))

def test_two_user_agents_give_two_results(self):
pipeline = self.pipeline(StandInCloud(), LRUEngineCache())

self.assertEqual(IPHONE, self.browser_name(pipeline, IPHONE))
self.assertEqual(ANDROID, self.browser_name(pipeline, ANDROID))

def test_the_same_user_agent_again_is_answered_from_the_cache(self):
# With the JavaScript builder, the sequence element gives every
# request a session id, which the Cloud Request Engine never
# answers from its cache, so the JavaScript builder is off here.
cloud = StandInCloud()
pipeline = self.pipeline(
cloud, LRUEngineCache(), add_javascript_builder=False)

self.assertEqual(IPHONE, self.browser_name(pipeline, IPHONE))
self.assertEqual(ANDROID, self.browser_name(pipeline, ANDROID))
self.assertEqual(IPHONE, self.browser_name(pipeline, IPHONE))
self.assertEqual(2, cloud.data_requests)


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,13 @@ def __init__(self, settings = None, usage_sharing = True, **kwargs):
* enable_cookies - whether cookies should be enabled
* minify: Whether to minify the JavaScript
@type cache: Cache
@param cache: An instance of the fiftyone.pipeline.engines.cache, for the cloud engine only. The on-premise engine raises an exception if one is supplied
class
@param cache: An instance of the fiftyone.pipeline.engines.cache
class, for the cloud only. It is added to the Cloud Request Engine,
which keys it on the evidence it sends and applies its own cache
rules. One of them sends a request that carries a session id to the
cloud service every time, and every request carries one while the
JavaScript builder is on. The on-premise engine raises an exception
if one is supplied.

Cloud Engine Settings:

Expand Down Expand Up @@ -127,18 +132,21 @@ def __init__(self, settings = None, usage_sharing = True, **kwargs):
self._usage_sharing = None

if "data_file_path" in settings:
# The engine raises an exception if a cache is supplied.
del settings["settings"]
ip_engine = IpIntelligenceOnPremise(**settings)

if usage_sharing:
self._usage_sharing = ShareUsage()

else :
self.add(CloudRequestEngine(settings))
ip_engine = IpIntelligenceCloud()
request_engine = CloudRequestEngine(settings)

if "cache" in settings:
request_engine.set_cache(settings["cache"])

if "cache" in settings:
ip_engine.set_cache(settings["cache"])
self.add(request_engine)
ip_engine = IpIntelligenceCloud()

self.add(ip_engine)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ def __init__(self, settings = None, **kwargs):
* minify: Whether to minify the JavaScript
@type cache: Cache
@param cache: An instance of the fiftyone.pipeline.engines.cache
class
class. It is added to the Cloud Request Engine, which keys it on
the evidence it sends and applies its own cache rules. One of them
sends a request that carries a session id to the cloud service
every time, and every request carries one while the JavaScript
builder is on.

Cloud Engine Settings:

Expand All @@ -78,10 +82,10 @@ def __init__(self, settings = None, **kwargs):

# Add specific engines

self.add(CloudRequestEngine(settings))
ip_engine = IpIntelligenceCloud()
request_engine = CloudRequestEngine(settings)

if "cache" in settings:
ip_engine.set_cache(settings["cache"])
request_engine.set_cache(settings["cache"])

self.add(ip_engine)
self.add(request_engine)
self.add(IpIntelligenceCloud())
Loading
Loading