Metadata-Version: 2.1
Name: bfopublisher
Version: 0.0.1
Summary: Accesd a BFO Publisher web service
Home-page: https://publisher.bfo.com
Author: Chris Burdess
Author-email: chris@bfo.com
License: MIT
Download-URL: https://pypi.org/project/bfopublisher/
Platform: UNKNOWN
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3
Description-Content-Type: text/markdown
Requires-Dist: cbor2
Requires-Dist: websocket-client

bfopublisher
============

This is a Python module that allows you to easily connect to a [BFO Publisher](https://publisher.bfo.com) web service.

Example
---
```sh
pip install bfopublisher
```

```python
from bfopublisher import Publisher

html = '''<!doctype html>
<html>
<head>
    <title>Example Document</title>
    <style type="text/css">
    body {
        background-color: #f0f0f2;
    }
    </style>
    <link rel="stylesheet" href="my-stylesheet.css" type="text/css">
</head>
<body>
<div class="box">
    <h1>Example Document</h1>
    <p>This is an example HTML document that will be converted to PDF.</p>
    <p><a href="https://publisher.bfo.com">BFO Publisher</a></p>
</div>
</body>
</html>'''

my_stylesheet = '''
    .box {
        width: 600px;
        margin: 5em auto;
        padding: 2em;
        background-color: #fdfdff;
        border-radius: 0.5em;
        box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);
    }
'''

action = 'convert'
message = {
    'put': [
        {
            'content': html,
            'content_type': 'text/html',
            'path': 'test.html'
        },
        {
            'content': my_stylesheet,
            'content-type': 'text/css',
            'path': 'my-stylesheet.css'
        }
    ]
}

def callback(response):
    if 'content' in response:
        print('Retrieved content of type %s' % response['content_type'])
        content = response['content']
        output_stream = open('out.pdf', 'wb')
        output_stream.write(content)
        output_stream.close()

publisher = Publisher('ws://localhost:8080/ws')
task = publisher.build(action, message)
task.set_callback(callback)
task.send()
publisher.disconnect()
```

The module depends on the cbor2 and websocket-client external Python modules which should be installed by pip.

Detail
---
The bfopublisher module is a simple Python wrapper around the [BFO Publisher Web Service](https://publisher.bfo.com/live/help/#_web_service),
which can be downloaded and run (locally or on another server; all the examples on this page assume it's accessible
at `http://localhost:8080/`). Note the ws: scheme on the URL to access the web service in the code as well as the final "ws" in the path, to access it via the websockets protocol.

To use the API:

1. Create a new `Publisher` object
2. call `build(action, message)`, where `action` is one of the actions described in the web service documentation. Usually this will be `convert`, you can also use `status`. This returns a task object associated with that message.
3. set the callback method for that task. It takes a response object which will be a dictionary. Different tasks can be assigned different callbacks to handle their results.
4. call `send()` on the task to send the message and process the responses. A convert action will produce numerous responses. One of them will be of type `convert-response` and contain the content output, others may be progress log messages. You can run many tasks in parallel over the same websocket connection for lengthy conversions, and the responses will be multiplexed back to the callbacks you specify.
5. Ensure that `disconnect()` is called on the Publisher object when you're done, to close the connection.

The Publisher class takes a websockets URL in its constructor. You can also specify an `authorization` parameter, which is an [authorization key](https://publisher.bfo.com/live/help/#_access_control) for access control.



