Permalink
Browse files

Merge pull request #3 from noise/passthrough

add details endpoint; support passthrough to ubuntu store for unknown snaps, allowing the core snap to be pulled on first snap install.
  • Loading branch information...
2 parents b59dcbc + 2bcc14a commit 3f51be71353a5f42396640c756fd6b867cfdc543 @noise committed on GitHub Jun 28, 2016
Showing with 58 additions and 10 deletions.
  1. +27 −0 Makefile
  2. +5 −7 README.md
  3. +2 −0 requirements.txt
  4. +1 −1 snapcraft.yaml
  5. +23 −2 store.py
View
@@ -0,0 +1,27 @@
+ENV = env
+
+snap: ## Snap it up
+ snapcraft clean snapstore
+ snapcraft snap
+
+$(ENV):
+ virtualenv $(ENV)
+
+deps: env ## install dependencies
+ $(ENV)/bin/pip install -q -r requirements.txt
+
+lint: env ## flake8 the source
+ $(ENV)/bin/flake8 store.py --max-line-length=99
+
+clean: ## tidy up the house
+ rm snapstore-example*.snap
+ snapcraft clean
+ rm -r $(ENV)
+
+run: env deps
+ $(ENV)/bin/python store.py
+
+help:
+ @grep -P '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'
+
+.PHONY: all deps lint clean snap help
View
@@ -55,11 +55,6 @@ E.g. on Ubuntu 16.04:
sudo apt install snapd
```
-Ensure core snap gets installed from the normal store, triggered on first snap install.
-```
-sudo snap install hello
-```
-
Edit /etc/environment, add your store URL, e.g.:
```
SNAPPY_FORCE_CPI_URL=http://localhost:5000/api/v1/
@@ -90,11 +85,14 @@ $ snap install bar
Name Version Rev Developer Notes
bar 2.5 1 testuser -
+
+$ snap refresh bar
+
+Name Version Rev Developer Notes
+bar 2.5 2 testuser -
```
# Known issues
- It's just an example, probably lots!
- snap refresh (bulk) not supported
-- issue above w/having to install core from canonical store.
-- newer details endpoint not implemented (will be needed in >2.0.9)
View
@@ -1 +1,3 @@
Flask==0.11.1
+requests==2.10.0
+flake8==2.6.2
View
@@ -1,5 +1,5 @@
name: snapstore-example
-version: 0.1
+version: 0.3
confinement: strict
summary: Minimalist example snap store
description: |
View
@@ -1,11 +1,15 @@
#!/usr/bin/env python
from flask import Flask, Response, request, send_from_directory, url_for, json
from flask.helpers import safe_join
+import requests
import os
FILES = os.environ.get('FILES', 'files')
-
+USTORE = 'https://search.apps.ubuntu.com/api/v1'
+HEADERS = ['X-Ubuntu-Release', 'X-Ubuntu-Series',
+ 'X-Ubuntu-Architecture', 'X-Ubuntu-Device-Channel',
+ 'X-Ubuntu-Wire-Format', 'Authorization']
app = Flask(__name__)
@@ -28,16 +32,33 @@ def read_meta(name):
return None
+@app.route('/api/v1/details/<name>')
+def details(name):
+ meta = read_meta(name)
+ if meta:
+ data = {'_embedded': {'clickindex:package': [meta]}}
+ return Response(json.dumps(data), mimetype='application/hal+json')
+ else:
+ # passthrough to upstream if we don't have that snap
+ f = request.args.get('fields')
+ h = {k: v for (k, v) in request.headers if k in HEADERS}
+ r = requests.get(USTORE + '/search?q=package_name:"%s"&fields=%s' % (name, f), headers=h)
+ return Response(json.dumps(r.json()), mimetype='application/hal+json')
+
+
@app.route('/api/v1/search')
def search():
''' note in 2.0.9 snap install uses the search endpoint
for package details as well as for snap find '''
q = request.args.get('q', '')
if 'package_name' in q:
- names = [q.split(':')[1].replace('"', '')]
+ name = q.split(':')[1].replace('"', '')
+ return details(name)
else:
names = [os.path.splitext(n)[0] for n in os.listdir(FILES)
if n.startswith(q) and n.endswith('.meta')]
+ if len(names) == 0:
+ names = [q]
data = {'_embedded': {'clickindex:package': []}}
data['_embedded']['clickindex:package'] = [m for m in [read_meta(n) for n in names] if m]
return Response(json.dumps(data), mimetype='application/hal+json')

0 comments on commit 3f51be7

Please sign in to comment.