---
url: /developers/misc/coding-recipes.md
---
# Coding Recipes

## The Magic Incantation

Any time you want to import and test Open Library code that needs access to `web.ctx`, you'll need to run a magic incantation.

First, exec into a running container and launch python (`web` is the legacy web.py server; `fast_web` is the FastAPI server, and either works for this):

```sh
docker compose exec -it web python
```

Next, use the following code to load Open Library and launch a minimal headless app:

For production:

```py
import web
from openlibrary.setup import setup_for_script
setup_for_script('/olsystem/etc/openlibrary.yml')
from infogami import config
```

For the local dev environment:

```py
import web
from openlibrary.setup import setup_for_script
setup_for_script('conf/openlibrary.yml')
from infogami import config
```

The recipes below use `web.ctx.site`, as legacy web.py code does. In FastAPI code, use `site.get()` from `openlibrary.utils.request_context` instead.

## Fetching a Patron's S3 Keys

Most privileged actions/requests Open Library takes on behalf of a patron will require their s3 keys:

```py
from openlibrary import accounts
from openlibrary.accounts.model import OpenLibraryAccount

logged_in_user = accounts.get_current_user()
username = logged_in_user and logged_in_user.key.split('/')[-1]
account = username and OpenLibraryAccount.get(username=username)
s3_keys = web.ctx.site.store.get(account._key).get('s3_keys')
```

## Fetching Things in Bulk

```py
keys = ["/works/OL5285479W", "/works/OL257943W", "/works/OL27448W"]
docs = web.ctx.site.get_many(keys)
```

## Privacy Settings

How to determine, given a set of patron usernames, which have public reading logs / can be followed?

```py
usernames = ['mekBot', 'ScarTissue', 'seabelis', 'brewster', 'jachamp']
user_prefs = web.ctx.site.get_many([f'/people/{username}/preferences' for username in usernames])
followable = dict((user.key.split('/')[2], user.notifications.public_readlog == 'yes') for user in user_prefs)
```

## Currently Logged In Patron?

The following must be called within a controller that has access to `web.ctx`:

```py
user = accounts.get_current_user()
```

## Fetching Books from Open Library with Availability

See: <https://openlibrary.org/dev/docs/api/search>

## Adding new db table schema to Open Library

Add the table's DDL to [`openlibrary/core/schema.sql`](https://github.com/internetarchive/openlibrary/blob/master/openlibrary/core/schema.sql) and write its model with the async database pool. See [Understanding the Data Model](/developers/backend/understanding-the-data-model.md#adding-new-tables).

## Setting or Unsetting a Cookie on Login or Registration

You might want to set a cookie when a patron logs in or registers to persist information, such as a setting, across page views. For instance, if a patron logs in and they have print disabled access (something we'd need to check on every page view) we can save this using a session cookie:

`web.setcookie('sfw', 'yes', expires="")`

Setting cookies example:
<https://github.com/internetarchive/openlibrary/pull/7935/files#diff-ef23faa0d3112e7eef1f574a58b0710f9604f911f5a57e795e45c13c4b91fa19R342>

Clearing cookies example:
<https://github.com/internetarchive/openlibrary/pull/8490/files#diff-884f3a712a7e51cce625ca19060335684272f33a967702b19e66e48dbdd1be69R131-R133>

## Testing the affiliate API

```py
import web
import infogami
from openlibrary.config import load_config
load_config('/olsystem/etc/openlibrary.yml')
infogami._setup()
from infogami import config
from openlibrary.core.vendors import AmazonCreatorsAPI
creators = config.get('amazon_creators_api') or {}
web.amazon_api = AmazonCreatorsAPI(creators.get('key'), creators.get('secret'), creators.get('id'), throttling=0.9)
book = web.amazon_api.get_products(["1666568287"], serialize=True)
```

## Examine the affiliate queue

1. Log into the host `ol-home0`
2. `docker exec -it openlibrary-affiliate-server-1 bash`
3. `curl localhost:31337/status`

Or to monitor repeatedly during debugging:

% `cat > ~/affiliate_status.sh`

```sh
#!/bin/bash

while true
do
  curl --silent localhost:31337/status | cut -c 1-90
  sleep 1
done
```

## Topics which could still use recipes

* Fetching patron's lists
