Skip to content

Commit

Permalink
Fix remote url issue
Browse files Browse the repository at this point in the history
  • Loading branch information
jrrodri committed Jan 6, 2019
1 parent d616075 commit d134305
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 54 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![PyPI version](https://badge.fury.io/py/abraia.svg)](https://badge.fury.io/py/abraia)
[![PyPI](https://img.shields.io/pypi/v/abraia.svg)](https://pypi.org/project/abraia/)
[![Build Status](https://travis-ci.org/abraia/abraia-python.svg)](https://travis-ci.org/abraia/abraia-python)
[![Coverage Status](https://coveralls.io/repos/github/abraia/abraia-python/badge.svg)](https://coveralls.io/github/abraia/abraia-python)

Expand Down
2 changes: 1 addition & 1 deletion abraia/abraia.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def from_store(self, path):
return self

def from_url(self, url):
resp = self.remote(url, self.userid+'/')
resp = self.upload_remote(url, self.userid+'/')
self.path = resp['source']
self.params = {'q': 'auto'}
return self
Expand Down
55 changes: 27 additions & 28 deletions abraia/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ def check(self):
files, folders = self.list_files()
return folders[0]['name']

def load_user(self):
url = '{}/users'.format(config.API_URL)
resp = requests.get(url, auth=self.auth)
if resp.status_code != 200:
raise APIError('GET {} {}'.format(url, resp.status_code))
return resp.json()

def list_files(self, path=''):
url = '{}/files/{}'.format(config.API_URL, path)
resp = requests.get(url, auth=self.auth)
Expand All @@ -27,6 +34,15 @@ def list_files(self, path=''):
resp = resp.json()
return resp['files'], resp['folders']

def upload_remote(self, url, path):
json = {'url': url}
url = '{}/files/{}'.format(config.API_URL, path)
resp = requests.post(url, json=json, auth=self.auth)
if resp.status_code != 201:
raise APIError('POST {} {}'.format(url, resp.status_code))
resp = resp.json()
return resp['file']

def upload_file(self, file, path, type=''):
source = path + os.path.basename(file) if path.endswith('/') else path
name = os.path.basename(source)
Expand All @@ -40,26 +56,15 @@ def upload_file(self, file, path, type=''):
resp = requests.put(url, data=data, headers={'Content-Type': type})
if resp.status_code != 200:
raise APIError('PUT {} {}'.format(url, resp.status_code))
return {
'name': name,
'source': source,
'thumbnail': os.path.dirname(source) + '/tb_' + name
}

def remote(self, url, path):
name = os.path.basename(url)
path = os.path.join(path, name)
imgbuf = None
try:
response = requests.get(url)
content_type = response.headers['content-type']
if content_type in config.MIME_TYPES.values():
imgbuf = BytesIO(response.content)
except requests.exceptions.RequestException as e:
print(e)
if imgbuf is None:
raise APIError('Resource not found: {}'.format(url))
return self.upload_file(imgbuf, path)
return {'name': name, 'source': source}

def move_file(self, old_path, new_path):
url = '{}/files/{}'.format(config.API_URL, new_path)
resp = requests.post(url, json={'store': old_path}, auth=self.auth)
if resp.status_code != 201:
raise APIError('POST {} {}'.format(url, resp.status_code))
resp = resp.json()
return resp['file']

def download_file(self, path):
url = '{}/files/{}'.format(config.API_URL, path)
Expand All @@ -68,19 +73,13 @@ def download_file(self, path):
raise APIError('GET {} {}'.format(url, resp.status_code))
return resp

def move_file(self, old_path, new_path):
url = '{}/files/{}'.format(config.API_URL, new_path)
resp = requests.post(url, json={'store': old_path}, auth=self.auth)
if resp.status_code != 201:
raise APIError('POST {} {}'.format(url, resp.status_code))
return resp.json()

def remove_file(self, path):
url = '{}/files/{}'.format(config.API_URL, path)
resp = requests.delete(url, auth=self.auth)
if resp.status_code != 200:
raise APIError('DELETE {} {}'.format(url, resp.status_code))
return resp.json()
resp = resp.json()
return resp['file']

def transform_image(self, path, params=''):
url = '{}/images/{}'.format(config.API_URL, path)
Expand Down
6 changes: 3 additions & 3 deletions scripts/abraia
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ from datetime import datetime

import abraia

IMAGE_EXTS = ['.jpg', '.jpeg', '.png', '.gif', '.webp']
IMAGE_EXTS = ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.svg']


def process_info():
print('Abraia CLI v0.3.5')
print('Abraia CLI v0.4.0')


def process_configure():
Expand Down Expand Up @@ -155,7 +155,7 @@ def add_parser_aesthetics(subparser):

def parse_input():
parser = argparse.ArgumentParser(description='Abraia image optimization tool')
parser.add_argument('-V', '--version', action='version', version='0.3.5')
parser.add_argument('-V', '--version', action='version', version='0.4.0')
subparser = parser.add_subparsers(dest='command')
parser_info = add_parser_info(subparser)
parser_configure = add_parser_configure(subparser)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='abraia',
version='0.3.5',
version='0.4.0',
description='Abraia Python SDK',
long_description=long_description,
long_description_content_type='text/markdown',
Expand Down
6 changes: 4 additions & 2 deletions tests/test_abraia.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import pytest
from abraia import client
from abraia import abraia


Expand All @@ -15,14 +16,15 @@ def test_from_file():
source = abraia.from_file(os.path.join(
os.path.dirname(__file__), '../images/tiger.jpg'))
assert isinstance(source, abraia.Client)
assert source.path.endswith('tiger.jpg')


def test_from_url():
"""Test an API call to upload a remote file"""
url = 'https://abraia.me/images/random.jpg'
source = abraia.from_url(url)
assert isinstance(source, abraia.Client)
# assert source.path == 'random.jpg'
assert source.path.endswith('random.jpg')


def test_to_file():
Expand Down Expand Up @@ -59,6 +61,6 @@ def test_smartcrop():
# def test_exception():
# """Test an API exception to save no file"""
# source = abraia.from_url('https://abraia.me/images/tiger.jpg')
# with pytest.raises(APIError):
# with pytest.raises(client.APIError):
# source.to_file(os.path.join(
# os.path.dirname(__file__), '../images/error.jpg'))
49 changes: 31 additions & 18 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,51 @@
filename = 'tiger.jpg'


def test_load_user():
"""Test an API call to load user info"""
resp = client.load_user()
assert isinstance(resp, dict)


def test_list_files():
"""Test an API call to list stored files and folders"""
files, folders = client.list_files()
assert isinstance(files, list)
assert isinstance(folders, list)


def test_upload_remote():
"""Tests an API call to upload a remote file"""
url = 'https://api.abraia.me/files/demo/birds.jpg'
resp = client.upload_remote(url, userid+'/')
assert isinstance(resp, dict)
assert resp['name'] == 'birds.jpg'
assert resp['source'] == '0/birds.jpg'


def test_upload_file():
"""Tests an API call to upload a local file"""
resp = client.upload_file(
os.path.join('images', filename),
userid+'/', type='image/jpeg')
assert isinstance(resp, dict)


def test_download_file():
"""Tests an API call to download an stored file"""
resp = client.download_file(userid+'/'+filename)
assert resp.status_code == 200
assert resp['name'] == 'tiger.jpg'
assert resp['source'] == '0/tiger.jpg'


def test_move_file():
"""Test an API call to move a stored file"""
client.move_file(os.path.join(userid, filename), userid + '/test/' + filename)
resp = client.move_file(userid + '/test/' + filename, os.path.join(userid, filename))
client.move_file(os.path.join(userid, filename), userid + '/test/tiger.jpg')
resp = client.move_file(userid + '/test/tiger.jpg', os.path.join(userid, filename))
assert isinstance(resp, dict)
assert resp['file']['source'] == userid + '/' + filename
assert resp['name'] == 'tiger.jpg'
assert resp['source'] == '0/tiger.jpg'


def test_remote_file():
"""Test an API call to upload a remote file"""
url = 'https://abraia.me/images/random.jpg'
resp = client.remote(url, userid+'/')
assert isinstance(resp, dict)
def test_download_file():
"""Tests an API call to download an stored file"""
resp = client.download_file(userid+'/'+filename)
assert resp.status_code == 200


def test_transform_image():
Expand All @@ -55,10 +66,10 @@ def test_transform_image():
# assert isinstance(json, dict)


def test_aesthetics_image():
"""Test an API call to predict image aeshetics"""
json = client.aesthetics_image(os.path.join(userid, filename))
assert isinstance(json, dict)
# def test_aesthetics_image():
# """Test an API call to predict image aeshetics"""
# json = client.aesthetics_image(os.path.join(userid, filename))
# assert isinstance(json, dict)


def test_process_video():
Expand All @@ -71,3 +82,5 @@ def test_remove_file():
"""Test an API call to remove an stored file"""
resp = client.remove_file(os.path.join(userid, filename))
assert isinstance(resp, dict)
assert resp['name'] == 'tiger.jpg'
assert resp['source'] == '0/tiger.jpg'

0 comments on commit d134305

Please sign in to comment.