Skip to content

Commit

Permalink
Update SDK with one time password option
Browse files Browse the repository at this point in the history
  • Loading branch information
nakedmcse committed Apr 2, 2024
1 parent ad0cab4 commit 065402a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 23 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ import waifuvault

To Upload a file, use the `upload_file` function. This function takes the following options as an object:

| Option | Type | Description | Required | Extra info |
|----------------|--------------------|-------------------------------------------------------------|----------------|----------------------------------|
| `target` | `string or buffer` | The target to upload can be a buffer, URL or filename | true | URL or file path |
| `target_name` | `string` | The filename of the target if it is a buffer | true if buffer | Filename with extension |
| `expires` | `string` | A string containing a number and a unit (1d = 1day) | false | Valid units are `m`, `h` and `d` |
| `hideFilename` | `boolean` | If true, then the uploaded filename won't appear in the URL | false | Defaults to `false` |
| `password` | `string` | If set, then the uploaded file will be encrypted | false | |
| Option | Type | Description | Required | Extra info |
|-------------------|--------------------|-----------------------------------------------------------------|----------------|----------------------------------|
| `target` | `string or buffer` | The target to upload can be a buffer, URL or filename | true | URL or file path |
| `target_name` | `string` | The filename of the target if it is a buffer | true if buffer | Filename with extension |
| `expires` | `string` | A string containing a number and a unit (1d = 1day) | false | Valid units are `m`, `h` and `d` |
| `hideFilename` | `boolean` | If true, then the uploaded filename won't appear in the URL | false | Defaults to `false` |
| `password` | `string` | If set, then the uploaded file will be encrypted | false | |
| `oneTimeDownload` | `boolean` | if supplied, the file will be deleted as soon as it is accessed | false | |


Using a URL:

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "waifuvault"
version = "1.3.2"
version = "1.3.6"
authors = [
{ name="Walker Aldridge", email="[email protected]" },
]
Expand Down
18 changes: 14 additions & 4 deletions src/waifuvault/waifumodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@


class FileUpload:
def __init__(self, target: any, target_name: str = "unknown", expires: str = None, password: str = None, hidefilename: bool = False):
def __init__(self, target: any, target_name: str = "unknown", expires: str = None, password: str = None, hidefilename: bool = False, oneTimeDownload: bool = False):
self.target = target
self.target_name = target_name
self.hidefilename = hidefilename
self.one_time_download = oneTimeDownload
self.expires = expires
self.password = password

Expand All @@ -25,13 +26,22 @@ def build_parameters(self):
if self.expires:
parameters['expires'] = self.expires
if self.hidefilename:
parameters['hide_filename'] = self.hidefilename
parameters['hide_filename'] = str(self.hidefilename).lower()
if self.one_time_download:
parameters['one_time_download'] = str(self.one_time_download).lower()
return parameters


class FileResponse:
def __init__(self, token: str = None, url: str = None, protected: bool = False, retention_period: any = None):
def __init__(self, token: str = None, url: str = None, retention_period: any = None, options: any = None):
self.token = token
self.url = url
self.protected = protected
self.retentionPeriod = retention_period
self.options = options


class FileOptions:
def __init__(self, hide_filename: bool = False, one_time_download: bool = False, protected: bool = False):
self.hideFilename = hide_filename
self.oneTimeDownload = one_time_download
self.protected = protected
10 changes: 7 additions & 3 deletions src/waifuvault/waifuvault.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import requests
from requests_toolbelt import MultipartEncoder

from .waifumodels import FileResponse, FileUpload
from .waifumodels import FileResponse, FileUpload, FileOptions


# Upload File
Expand Down Expand Up @@ -107,5 +107,9 @@ def __dict_to_obj(dict_obj: any):
return FileResponse(
dict_obj["token"],
dict_obj["url"],
dict_obj["protected"],
dict_obj["retentionPeriod"])
dict_obj["retentionPeriod"],
FileOptions(
dict_obj["options"]["hideFilename"],
dict_obj["options"]["oneTimeDownload"],
dict_obj["options"]["protected"]
))
16 changes: 8 additions & 8 deletions tests/test_waifuvault.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ def __init__(self, ok, text, content=None, code=None):

# Mocked responses
ok_response_numeric = response_mock(True,
'{"url":"https://waifuvault.moe/f/something", "token":"test-token", "protected":false, "retentionPeriod":100}')
'{"url":"https://waifuvault.moe/f/something", "token":"test-token", "retentionPeriod":100, "options":{"protected": false, "oneTimeDownload": false, "hideFilename": false}}')
ok_response_numeric_protected = response_mock(True,
'{"url":"https://waifuvault.moe/f/something", "token":"test-token", "protected":true, "retentionPeriod":100}')
'{"url":"https://waifuvault.moe/f/something", "token":"test-token", "retentionPeriod":100, "options":{"protected": true, "oneTimeDownload": false, "hideFilename": false}}')
ok_response_human = response_mock(True,
'{"url":"https://waifuvault.moe/f/something", "token":"test-token", "protected":false, "retentionPeriod":"10 minutes"}')
'{"url":"https://waifuvault.moe/f/something", "token":"test-token", "retentionPeriod":"10 minutes", "options":{"protected": false, "oneTimeDownload": false, "hideFilename": false}}')
bad_request = response_mock(False,
'{"name": "BAD_REQUEST", "message": "Error Test", "status": 400}',code=400)

Expand All @@ -42,7 +42,7 @@ def test_upload_url(mocker):
# Then
assert (upload_res.url == "https://waifuvault.moe/f/something"), "URL does not match"
assert (upload_res.token == "test-token"), "Token does not match"
assert (upload_res.protected is False), "Protected does not match"
assert (upload_res.options.protected is False), "Protected does not match"
assert (upload_res.retentionPeriod == 100), "Retention does not match"


Expand Down Expand Up @@ -70,7 +70,7 @@ def test_upload_file(mocker):
mock_put.assert_called_once()
assert (upload_res.url == "https://waifuvault.moe/f/something"), "URL does not match"
assert (upload_res.token == "test-token"), "Token does not match"
assert (upload_res.protected is False), "Protected does not match"
assert (upload_res.options.protected is False), "Protected does not match"
assert (upload_res.retentionPeriod == 100), "Retention does not match"


Expand Down Expand Up @@ -100,7 +100,7 @@ def test_upload_buffer(mocker):
mock_put.assert_called_once()
assert (upload_res.url == "https://waifuvault.moe/f/something"), "URL does not match"
assert (upload_res.token == "test-token"), "Token does not match"
assert (upload_res.protected is False), "Protected does not match"
assert (upload_res.options.protected is False), "Protected does not match"
assert (upload_res.retentionPeriod == 100), "Retention does not match"


Expand Down Expand Up @@ -131,7 +131,7 @@ def test_file_info(mocker):
params={'formatted': 'true'})
assert (upload_info.url == "https://waifuvault.moe/f/something"), "URL does not match"
assert (upload_info.token == "test-token"), "Token does not match"
assert (upload_info.protected is False), "Protected does not match"
assert (upload_info.options.protected is False), "Protected does not match"
assert (upload_info.retentionPeriod == "10 minutes"), "Retention does not match"


Expand All @@ -157,7 +157,7 @@ def test_update_info(mocker):
data={'password': 'dangerWaifu','hideFilename': 'false'})
assert (update_info.url == "https://waifuvault.moe/f/something"), "URL does not match"
assert (update_info.token == "test-token"), "Token does not match"
assert (update_info.protected is True), "Protected does not match"
assert (update_info.options.protected is True), "Protected does not match"
assert (update_info.retentionPeriod == 100), "Retention does not match"


Expand Down

0 comments on commit 065402a

Please sign in to comment.