Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This workflows will upload a Python Package using Twine when a release is created
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries

name: Publish on PyPI

on:
push:
branches: [ master ]

jobs:
deploy:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install poetry
- name: Fetch tags
run: git fetch --prune --unshallow --tags
- name: Build and publish
run: |
poetry version $(git describe --tags --abbrev=0)
poetry build
poetry publish --username ${{ secrets.PYPI_USERNAME }} --password ${{ secrets.PYPI_PASSWORD }}

8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,11 @@ dmypy.json

# Pyre type checker
.pyre/

#Custom ignore
.vscode/
poetry.lock
*.ipynb

#Sphinx docs
docs/build/
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ b.Purge.create(url='https://myzone.b-cdn.net/css/style.css')
zone.purge_file('css/style.css')
```

#### Purge Esntire Pull Zone
#### Purge Entire Pull Zone
```python
# From top-level
b.Zone.purge(
Expand All @@ -158,6 +158,16 @@ b.Zone.create_edge_rule(
Triggers = []
)
```

#### Hostnames
```python
# Create a hostname
b.Zone.create_hostname("myHostname")

# Delete a hostname
b.Zone.delete_hostname("myHostname")
```

## Storage

### Storage Zones
Expand Down Expand Up @@ -193,6 +203,7 @@ b.Storage.delete(1234)
```python
zone = b.Storage.get(1234)
# Returns: <StorageZone: example-a (id: 1234)>

```

### Storage Files
Expand All @@ -210,14 +221,18 @@ obj_list = zone.all()
obj = zone.get('index.html')
#Returns <StorageFile: index.html>

# Get a file as StorageObject
zone = b.Storage.get_object(1234)
# Returns: <StorageObject: example-a (id: 1234)>

# Download that File
obj.download()

# Delete a file
zone.delete('index.html')
zone.delete_file('index.html')

# Upload a File
zone.upload_file(dest_path='folder/path/error.html', local_path='/home/mj/work/error.html')
zone.upload_file(dest_path='folder/path/', file_name='error.html', local_path='/home/mj/work/')
# Returns: <StorageFile: error.html>

# Create a file from a string
Expand Down
42 changes: 33 additions & 9 deletions bunnyhop/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

from bunnyhop import base

try:
import brotli
BROTLI_ENABLED = True
except ImportError:
BROTLI_ENABLED = False


class Storage(base.BaseBunny):

Expand All @@ -20,7 +26,7 @@ def create(self, name, main_storage_region: str = None, replica_regions: list =
if response.get('Id', None):
return StorageZone(response.get('Password'), **response)
else:
raise Exception(f"Error: {response.get('ErrorKey', None)} Message: {response.get('Message','')}")
raise ValueError(f"Error: {response.get('ErrorKey', None)} Message: {response.get('Message','')}")

def all(self):
return [StorageZone(i.get('Password'), **i) for i in
Expand Down Expand Up @@ -51,7 +57,7 @@ class StorageZone(base.BaseStorageBunny):
ReadOnlyPassword = base.CharProperty(required=False)

def __str__(self):
return self.Name
return f"{self.Name} (id: {self.Id})"

def all(self, folder=''):
if not folder.endswith('/'):
Expand All @@ -61,27 +67,45 @@ def all(self, folder=''):
def get(self, file_path):
response = self.call_storage_api(f"/{self.Name}/{file_path}", "GET")
if isinstance(response,dict) and response.get('HttpCode',0) == 404:
raise Exception(f"Error:{response.get('Message','')}")
raise ValueError(f"Error:{response.get('Message','')}")
if file_path.endswith('.brotli'):
response = brotli.decompress(response)
return response

def get_object(self, file_path):
response = self.call_storage_api(f"/{self.Name}/{file_path}", "GET")
if isinstance(response,dict) and response.get('HttpCode',0) == 404:
raise Exception(f"Error:{response.get('Message','')}")
raise ValueError(f"Error:{response.get('Message','')}")
return [StorageObject(self.api_key, self, **i) for i in self.call_storage_api(f"/{self.Name}/", "GET") if file_path==i.get('ObjectName','') ][0]

def head_file(self, file_path):
return self.call_storage_api(f"/{self.Name}/{file_path}", "HEAD")

def upload_file(self, dest_path, file_name, local_path):
def upload_file(self, dest_path, file_name, local_path,use_brotli=False):
if use_brotli and BROTLI_ENABLED and file_name.endswith('.json'):
with open(os.path.join(local_path, file_name)) as json_file:
data = json.load(json_file)
data_str = json.dumps(data).encode('UTF-8')
compressed_data = brotli.compress(data_str)
file_name = os.path.splitext(file_name)[0]+".brotli"
compressed_file = open(os.path.join(local_path, file_name),"wb")
compressed_file.write(compressed_data)
compressed_file.close()

return self.call_storage_api(f"/{self.Name}/{dest_path}/{file_name}", "PUT",
data=open(local_path, 'rb').read())
data=open(os.path.join(local_path, file_name), 'rb').read())

def create_file(self, file_name, content):
pass

def create_json(self, key, data_dict):
f = BytesIO(json.dumps(data_dict).encode())
def create_json(self, key, data_dict, use_brotli=False):
data_json = json.dumps(data_dict).encode()

if use_brotli and BROTLI_ENABLED:
data_json= brotli.compress(data_json.encode('UTF-8'))
key += ".brotli"

f = BytesIO(data_json)
return self.call_storage_api(f"/{self.Name}/{key}", "PUT",
data=f.read())

Expand All @@ -91,7 +115,7 @@ def delete(self):
def delete_file(self, file_path):
response = self.call_storage_api(f"/{self.Name}/{file_path}", "DELETE")
if response.get('HttpCode',0) == 404:
raise Exception(f"Error:{response.get('Message','')}")
raise ValueError(f"Error:{response.get('Message','')}")
return response


Expand Down
20 changes: 20 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = source
BUILDDIR = build

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
35 changes: 35 additions & 0 deletions docs/make.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@ECHO OFF

pushd %~dp0

REM Command file for Sphinx documentation

if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=source
set BUILDDIR=build

if "%1" == "" goto help

%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.http://sphinx-doc.org/
exit /b 1
)

%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end

:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%

:end
popd
Loading