# CDN Manifest library

Lightweight python library that reads from the manifest and resolves url based
on the asset name, application name and environment. This library will be used
by python frontend applications to find javascript, css, images and other assets
that exists in our CDN.

## Installation

```bash
pip install -i https://pypi.theorchard.io/pypi/ cdnmanifest
```

## Overview

CDN Manifest library takes the content of the application's `manifest.json`
and generates urls on the fly for assets that have been deployed in our CDN.

```python
from cdnmanifest import manifest

current_manifest = manifest.Manifest('frontend-fireworks', 'qa')
bundle_url = current_manifest.get_url('bundle.js')
print(bundle_url)
# Gives: https://cdn.theorchard.io/frontend-fireworks/982827345/bundle.js
```

### Simple Manifest

Our static versioned files are currently being served from a CDN (
cdn.theorchard.io for production and qa-cdn.theorchard.io for qa). Each files
are stored per application in a s3 bucket (the application name being part of
the path). For example, “bundle.js” from “frontend-fireworks” will be stored
like this:

```bash
s3://bucket/frontend-fireworks/{timestamp}/bundle.js
```

To be able to know where the deployed files are, at every deploy, we override
the `manifest.json` (localized at the root of the application), with the latest
deployed timestamp. The file would contain the following:

```bash
cat s3://bucket/frontend-fireworks/manifest.json
{
  "timestamp": "20161019113904"
}
```

### Localized Files

The manifest can also contain localized files. For instance: if the build
process creates several javascript bundles, one per language, the manifest
would contain the following:

```json
{
  "main.bundle.js": {
    "de": "main.de.bundle.js",
    "es": "main.es.bundle.js",
    "fr": "main.fr.bundle.js",
    "it": "main.it.bundle.js",
    "ja": "main.ja.bundle.js",
    "pt": "main.pt.bundle.js",
    "ru": "main.ru.bundle.js",
    "tr": "main.tr.bundle.js",
    "zh-CN": "main.zh-CN.bundle.js",
    "zh-TW": "main.zh-TW.bundle.js",
    "en": "main.en.bundle.js"
  },
  "main.css": "main.css",
  "timestamp": "20161019113904"
}
```

To ask for a localized file:

```python
from cdnmanifest import manifest

current_manifest = manifest.Manifest('frontend-fireworks', 'prod')
bundle_url = current_manifest.get_url('main.bundle.js', localization='pt')
# https://cdn.theorchard.io/frontend-fireworks/20161019113904/main.pt.bundle.js
```

If you do not provide the localization, the English version will be provided
by default.

```python
from cdnmanifest import manifest

current_manifest = manifest.Manifest('frontend-fireworks', 'prod')
bundle_url = current_manifest.get_url('main.bundle.js')
# https://cdn.theorchard.io/frontend-fireworks/20161019113904/main.en.bundle.js
```

If you provide a locale code that is not supported, the system will default to
the English version.

```python
from cdnmanifest import manifest

current_manifest = manifest.Manifest('frontend-fireworks', 'prod')
bundle_url = current_manifest.get_url('main.bundle.js', localization='random')
# https://cdn.theorchard.io/frontend-fireworks/20161019113904/main.en.bundle.js
```

### Refreshing the Manifest

In case you want the manifest to auto-refresh, you can set the flag
`expires_in` on the `Manifest` constructor. This is useful if you want to
deploy updates without having the need to redeploy the server.

```python
from cdnmanifest import manifest

# the fetched manifest will expires after an hour.
current_manifest = manifest.Manifest(
  'frontend-fireworks', 'prod', expires_in=3600)
```
