Skip to content

Commit

Permalink
Merge pull request #19 from waifuvault/add-clear-restrictions
Browse files Browse the repository at this point in the history
Add clear_restrictions, fix cache isssue in tests, fix README
  • Loading branch information
nakedmcse authored Aug 25, 2024
2 parents 57bc1fc + a250d23 commit c34cc61
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pip install waifuvault

## Usage

This API contains 9 interactions:
This API contains 10 interactions:

1. [Upload File](#upload-file)
2. [Get file Info](#get-file-info)
Expand All @@ -26,6 +26,7 @@ This API contains 9 interactions:
7. [Delete Bucket](#delete-bucket)
8. [Get Bucket](#get-bucket)
9. [Get Restrictions](#get-restrictions)
10. [Clear Restrictions](#clear-restrictions)

The package is namespaced to `waifuvault`, so to import it, simply:

Expand Down Expand Up @@ -246,9 +247,22 @@ To get the list of restrictions applied to the server, you use the `get_restrict

This will respond with an array of name, value entries describing the restrictions applied to the server.

> **NOTE:** Restrictions are cached for 10 minutes
```python
import waifuvault
restricions = waifuvault.get_bucket("some-bucket-token")
restricions = waifuvault.get_restrictions()

print(restrictions.Restrictions) # Array of restriction objects
```

### Clear Restrictions<a id="clear-restrictions"></a>

To clear the cached restrictions in the SDK, you use the `clear_restrictions` function.

This will remove the cached restrictions and a fresh copy will be downloaded at the next upload.

```python
import waifuvault
waifuvault.clear_restrictions()
```
2 changes: 1 addition & 1 deletion src/waifuvault/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .waifumodels import FileResponse, FileUpload, BucketResponse, Restriction, RestrictionResponse
from .waifuvault import (upload_file, file_info, get_file, delete_file, file_update, create_bucket, get_bucket,
delete_bucket, get_restrictions)
delete_bucket, get_restrictions, clear_restrictions)
6 changes: 6 additions & 0 deletions src/waifuvault/waifuvault.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ def get_restrictions():
return RestrictionResponse(rest_obj=json.loads(response.text))


# Clear Restrictions
def clear_restrictions():
global __restrictions
__restrictions = None


# Create Bucket
def create_bucket():
url = f"{__base_url__}/bucket/create"
Expand Down
5 changes: 2 additions & 3 deletions tests/test_waifuvault.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,12 @@ def test_upload_buffer_error(mocker):

def test_upload_restriction_error(mocker):
# Given
waifuvault.clear_restrictions()
mock_put = mocker.patch('requests.put', return_value=bad_request)
mock_get = mocker.patch('requests.get', return_value=restrictions_small_response)
with open("tests/testfile.png", "rb") as fh:
buf = io.BytesIO(fh.read())

# When
upload_file = waifuvault.FileUpload(buf, "testfile_buf.png", expires="10m")
upload_file = waifuvault.FileUpload("tests/testfile.png", expires="10m")

# Then
with pytest.raises(Exception, match=re.escape('File size 97674 is larger than max allowed 100')):
Expand Down

0 comments on commit c34cc61

Please sign in to comment.