FAQ | This is a LIVE service | Changelog

Commit 2aa87d6d authored by Monty Dawson's avatar Monty Dawson
Browse files

Merge branch 'issue-3-data-url' into 'master'

add support for data URLs

Closes #3

See merge request !4
parents aeafc3ef 11cb6d8f
Pipeline #90396 waiting for manual action with stages
in 2 minutes and 58 seconds
......@@ -38,6 +38,9 @@ geddit('gs://my-bucket/some-object')
# Google Secret Manager secrets. Uses default application credentials.
geddit('sm://my-project/some-secret') # fetches latest version
geddit('sm://my-project/some-secret#3') # fetches version 3
# Data URLs.
geddit('data:application/x-yaml;charset=utf-8;base64,eyJmb28iOjF9') # == b'{"foo":1}'
```
## Requirements
......@@ -94,3 +97,9 @@ Non-TLS ("http") URLs are *not* supported.
Only default application credentials are supported. To use specific credentials,
set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to the absolute
path to some JSON-formatted credentials.
### data
Data URLs are _not_ good way of passing secrets since their values are
transparent. They can be useful for transferring non-secret configuration or for
use in development environments.
import logging
import os
import urllib.parse
from urllib.request import urlopen
from google.cloud import secretmanager, storage
import requests
......@@ -22,6 +23,7 @@ def geddit(url):
"gs://bucket/path/to/object".
* sm: fetch from a Google Secret Manager secret. The URL should have the form
"sm://project/secret[#version]". If no version is provided the "latest" version is used.
* data: parse RFC 2397 "data URLs"
For "gs" and "sm" URLs, application default credentials are used.
......@@ -91,10 +93,21 @@ def _fetch_storage_url(components):
return blob.download_as_string()
def _fetch_data_url(components):
"""
Fetch a data URL which has been parsed into components.
"""
# Top tip: urlopen() has supported data URLs since Python 3.4
with urlopen(urllib.parse.urlunsplit(components[:5])) as response:
return response.read()
# A table mapping URL schemes into the corresponding callable for that scheme.
_SCHEME_MAP = {
'file': _fetch_file_url,
'gs': _fetch_storage_url,
'https': _fetch_https_url,
'sm': _fetch_secret_manager_url,
'data': _fetch_data_url,
}
import unittest
from geddit import geddit
class DataTestCase(unittest.TestCase):
def test_basic(self):
self.assertEqual(
geddit('data:text/plain;charset=UTF-8;page=21,the%20data:1234,5678'),
b'the data:1234,5678'
)
def test_base64(self):
self.assertEqual(
geddit('data:text/vnd-example+xyz;foo=bar;base64,R0lGODdh'),
b'GIF87a'
)
def test_readme_example(self):
self.assertEqual(
geddit('data:application/x-yaml;charset=utf-8;base64,eyJmb28iOjF9'),
b'{"foo":1}'
)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment