Metadata-Version: 2.4
Name: fiftyone_pipeline_cloudrequestengine
Version: 4.6.2
Summary: This package contains a shared engine that is used to make requests to the 51Degrees cloud service on behalf of other data services such as device detection or location.
Author: 51Degrees Engineering
Author-email: engineering@51degrees.com
License: EUPL-1.2
Project-URL: Homepage, https://51degrees.com/build-your-own?utm_source=pypi&utm_medium=package&utm_campaign=51degrees-python&utm_content=fiftyone_pipeline_cloudrequestengine&utm_term=homepage
Project-URL: Documentation, https://51degrees.com/documentation/_pipeline_api__index.html?utm_source=pypi&utm_medium=package&utm_campaign=51degrees-python&utm_content=fiftyone_pipeline_cloudrequestengine&utm_term=documentation
Project-URL: Source, https://github.com/51Degrees/51degrees-python
Classifier: Development Status :: 5 - Production/Stable
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: License :: OSI Approved :: European Union Public Licence 1.2 (EUPL 1.2)
Classifier: Operating System :: OS Independent
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.10
Description-Content-Type: text/markdown
Requires-Dist: fiftyone_pipeline_engines>=4.5.44
Requires-Dist: requests
Dynamic: author
Dynamic: author-email
Dynamic: classifier
Dynamic: description
Dynamic: description-content-type
Dynamic: license
Dynamic: project-url
Dynamic: requires-dist
Dynamic: requires-python
Dynamic: summary

# Cloud Request Engine

![51Degrees](https://raw.githubusercontent.com/51Degrees/51degrees-python/main/.github/images/logo-160.png "Data rewards the curious") **Python Pipeline Cloud Request Engine**

[Developer Documentation](https://51degrees.com/documentation/_pipeline_api__concepts__pipeline_basics.html?utm_source=pypi&utm_medium=package&utm_campaign=51degrees-python&utm_content=fiftyone_pipeline_cloudrequestengine&utm_term=cloud-request-engine "Developer Documentation")

## Introduction

The Pipeline is a generic web request intelligence and data processing solution with the ability to add a range of 51Degrees and/or custom plug ins (Engines) 

## Requirements

* Python 3.8+

## This package fiftyone_pipeline_cloudrequestengine

This package uses the `engines` class created by the `fiftyone-pipeline-engines`. It makes available:

* A `Cloud Request Engine` which calls the 51Degrees cloud service to fetch properties and metadata about them based on a provided resource key. Get a resource key at https://51degrees.com/configure/?utm_source=pypi&utm_medium=package&utm_campaign=51degrees-python&utm_content=fiftyone_pipeline_cloudrequestengine&utm_term=this-package-fiftyone_pipeline_cloudrequestengine
* A `Cloud Engine` template which reads data from the Cloud Request Engine.

## Credentials

The engine authenticates with a resource key, a licence key, or both, set
as `resource_key` and `license_key` in its settings. Which one is set
decides whether `requested_properties`, the list of fully qualified
property names to ask for, is also needed:

| `resource_key` | `license_key` | `requested_properties` | Accepted |
| --- | --- | --- | --- |
| Yes | No | No | Yes. The resource key states which properties it carries. |
| Yes | Yes | No | Yes. The answer holds what the resource key carries. |
| Yes | Yes | Yes | Yes. The service adds the listed properties the licence key grants. |
| Yes | No | Yes | Yes. The service adds the listed properties, as null with a reason where no licence grants one. |
| No | Yes | Yes | Yes. A licence key names no properties, so the list says which are wanted. |
| No | Yes | No | No. The service answers 400 to every such request. |
| No | No | Either | No. There is nothing to authenticate with. |

A combination that is not accepted raises `ValueError` when the engine is
built, so a deployment sees one configuration error rather than a failure
on every request. A value that is empty or only whitespace counts as
absent, and each value is trimmed, so a key read from an environment
variable with a trailing newline still works.

A resource key is public by design, because it travels to the browser in a
script URL. A caller that runs only on the server should use a licence
key and name the properties it wants, so the credential stays on the
server and each answer carries only what that caller needs. A licence key
is sent in the body of every request, never in a URL. Where a request
named properties and the answer left some out, the engine warns once that
they are not covered by the keys in use, and the properties that came back
are usable as normal. With a resource key and no licence key, a property no
licence grants comes back as null with a reason, so the warning says instead
that every property is answered and these names are not properties, and
asks the caller to check the spelling of each one.

See the [Credentials section of the cloud request engine specification](https://github.com/51Degrees/specifications/blob/main/pipeline-specification/pipeline-elements/cloud-request-engine.md#credentials)
and the rules in
[`cloudrequestengine.py`](src/fiftyone_pipeline_cloudrequestengine/cloudrequestengine.py).

## When the engine is built

The engine asks the cloud service for the properties the key carries and
the evidence keys the service accepts when it is constructed, so a
pipeline built with it has both before the first request. When the cloud
service refuses either request, with an answer from 400 to 499 such as
the one for a resource key that is not valid, the constructor raises
`CloudRequestException`. Any other failure, such as the service being
unreachable, a timeout or an answer of 500 or more, gives a warning and
the engine is still built, so a cloud outage does not stop the
application starting. The engine then asks again when it is first used,
and a failure at that point is an error of that request, which
`suppress_process_exceptions` decides whether to raise. Until then the
cloud engines after it have no property metadata.

Each entry in the `warnings` list of an answer from the cloud service is
given as a warning, with any licence key masked.

A cloud engine takes its properties from the Cloud Request Engine in its
pipeline. It can be in several pipelines that share one Cloud Request
Engine, and a pipeline with a different Cloud Request Engine refuses it,
so build a cloud engine for each Cloud Request Engine.

An engine whose discovery has completed gives what it discovered with
`discovery_state()`, as one JSON document. Keep it, and give it as the
`discovery_state` setting of an engine with the same credentials, for
example on the next start of a short-lived host, and that engine is built
without asking the cloud service. The document is the one the Java
package's `CloudDiscoveryState` writes.

See the [Start-up activity section of the cloud request engine specification](https://github.com/51Degrees/specifications/blob/main/pipeline-specification/pipeline-elements/cloud-request-engine.md#start-up-activity).

## Timeout

Each request to the cloud service waits at most `timeout_seconds` to
connect, and at most that long again for each part of the answer, before it
fails with a timeout. The default is 2 seconds, as the
[configuration options of the cloud request engine specification](https://github.com/51Degrees/specifications/blob/main/pipeline-specification/pipeline-elements/cloud-request-engine.md#configuration-options)
give. A value that is not a finite number above zero raises `ValueError` when the
engine is built. A request that times out while the engine is built is a
failure that may pass, so the engine is built with a warning and asks
again when it is first used.

A client given as `http_client` is passed the timeout when its `request`
method has a `timeout_seconds` parameter, as `RequestClient.request` does,
and is called without it otherwise.

## Redirects

No request to the cloud service follows a redirect, whatever credential it
carries. An answer of 301, 302, 303, 307 or 308 fails with a
`CloudRequestException` that names the endpoint and the status code and
shows no key. A client given as `http_client` should not follow redirects
either.

## Reading the answer

The engine's data holds the raw JSON answer of the cloud service as its
`cloud` property, and is read under `cloud-response`, the key the
[Element Data section of the cloud request engine specification](https://github.com/51Degrees/specifications/blob/main/pipeline-specification/pipeline-elements/cloud-request-engine.md#element-data)
gives, as in `flowdata.get("cloud-response").get("cloud")`. The old key,
`cloud`, gives the same data and is deprecated, so read under
`cloud-response` instead. The old key will be removed in a later release.
The engine's own data key is still `cloud`, so
`pipeline.get_element("cloud")` still finds the engine, and the JSON for the
browser leaves the engine's data out as before.

## Human Confidence from a sealed result

Add `DerivedCloud` after the engine to read `HumanConfidence` and
`HumanConfidenceSource` as `flowdata.derived`. On a request whose evidence
holds a non-empty `query.humanconfidence`, the engine redeems that sealed
result in its one call, asking for both properties, and `DerivedCloud`
gives the value and the source, `Passive` or `Active`, that the cloud
service's response holds. Where the sealed result isn't redeemed, it gives
`Medium`, and `HumanConfidenceSource` has no value, with a message that says
why. It adds no error of its own, and a read doesn't raise.

`DerivedCloud` also works in a pipeline with no Cloud Request Engine, such
as an on-premise one. There, nothing is redeemed: a request carrying a
sealed result gives `Medium` and no source, and any other request gives the
two properties with no value and a reason. `DerivedCloud` is in
`fiftyone_pipeline_cloudrequestengine.humanconfidence`, and is added as any
other element, e.g. `PipelineBuilder().add(DerivedCloud()).build()`.

The page loads the client script from the address the package gives it,
`page.script_url(...)` from `HumanConfidenceTransactions.issue()`, and its
own code always sends the sealed result and the page id back to the
server, whatever HumanConfidenceSource is. These are the lines a consumer
writes, where `PAGE` is the page's `id`:

```javascript
fod.complete(function (data) {
  var body = new URLSearchParams();
  body.set("humanconfidence", data.derived.humanconfidence || "");
  body.set("humanconfidencepage", PAGE);
  fetch("humanconfidence", { method: "POST", body: body });
});
```

The server adds both as `query.` evidence and reads the two properties
from `flowdata.derived`. A server that works outside a Pipeline can redeem
a sealed result with `HumanConfidenceClient` in `fiftyone_pipeline_did`
instead.

A page whose request names HumanConfidence also gets the browser tests, so the
client script's complete handlers run later than they otherwise would, by the
time the tests take and one more request. A page can read the first
response's properties sooner through its change handlers.

Where a page sets a Content Security Policy, it must allow these for the
browser tests to run. Otherwise HumanConfidenceSource stays `Passive`.

| Directive | Must allow |
|-----------|------------|
| `script-src` | The cloud service's host, and `'unsafe-eval'` |
| `connect-src` | The cloud service's host |
| `worker-src` | `blob:` |

The rules are on the
[Human Confidence page of the pipeline specification](https://github.com/51Degrees/specifications/blob/main/pipeline-specification/features/human-confidence.md).

## Pointing the engine at another host

The engine calls `https://cloud.51degrees.com/api/v4/` unless told
otherwise. Set the `_51DEGREES_CLOUD_ENDPOINT` environment variable, or pass
`cloud_endpoint` in the engine settings, to the API base of another
host including the `/api/v4/` segment. `Constants.CLOUD_ENDPOINT` holds
the name of that variable. A host other than
cloud.51degrees.com would be used to (a) use an on premise web server,
or (b) use a privately hosted version of the 51Degrees cloud for
performance reasons. This is the private hosting option of the cloud
service, and both run the same service, so code written against one
works unchanged against the other.

## Examples

The [examples](examples/readme.md) folder holds examples that use this
engine to get device detection and IP intelligence results from one request
to the 51Degrees cloud service, with a resource key or a licence key. Its
readme says how to run them.

## Engines that use it

It is used by the cloud versions of the following 51Degrees engines:

- [**fiftyone_devicedetection**](https://pypi.org/project/fiftyone-devicedetection/) - Get details about the devices accessing your web page
- [**fiftyone_location**](https://pypi.org/project/fiftyone-location/) - Get postal address details from the location of devices accessing your web page

## Find out more

- [Build your own](https://51degrees.com/build-your-own?utm_source=pypi&utm_medium=package&utm_campaign=51degrees-python&utm_content=fiftyone_pipeline_cloudrequestengine&utm_term=find-out-more-build-your-own)
- [Pipeline API documentation](https://51degrees.com/documentation/_pipeline_api__concepts__pipeline_basics.html?utm_source=pypi&utm_medium=package&utm_campaign=51degrees-python&utm_content=fiftyone_pipeline_cloudrequestengine&utm_term=find-out-more-documentation)
- [Pipeline API examples](https://51degrees.com/documentation/_pipeline_api__examples__index.html?utm_source=pypi&utm_medium=package&utm_campaign=51degrees-python&utm_content=fiftyone_pipeline_cloudrequestengine&utm_term=find-out-more-examples)
- [Cloud service documentation](https://51degrees.com/documentation/_services__cloud__index.html?utm_source=pypi&utm_medium=package&utm_campaign=51degrees-python&utm_content=fiftyone_pipeline_cloudrequestengine&utm_term=find-out-more-cloud-service)
- https://github.com/51Degrees/51degrees-python
