def test_authentication_error():
    def request_callback(request):
        body = {
            "error": {
                "message": "Invalid API Key provided.",
                "type": "invalid_request_error"
            }
        }

        headers = {}

        return (401, headers, json.dumps(body))

    responses.add_callback(
        responses.POST, API_URL + '/previews/',
        callback=request_callback,
        content_type='application/json',
    )

    file_previews = FilePreviews(
        api_key='WRONG_API_KEY',
        api_secret='WRONG_SECRET_KEY'
    )

    with pytest.raises(exceptions.AuthenticationError) as exc:
        file_previews.generate('http://example.com/file.jpg')

    assert str(exc.value) == 'Invalid API Key provided.'
Beispiel #2
0
def cli(ctx, **kwargs):
    if ctx.obj is None:
        ctx.obj = {}

    ctx.obj['file_previews'] = FilePreviews(api_key=kwargs['api_key'],
                                            api_secret=kwargs['api_secret'],
                                            debug=kwargs['debug'])
def cli(ctx, **kwargs):
    if ctx.obj is None:
        ctx.obj = {}

    ctx.obj["file_previews"] = FilePreviews(
        api_key=kwargs["api_key"],
        api_secret=kwargs["api_secret"],
        debug=kwargs["debug"],
    )
def document_save(sender, instance, created, **kwargs):
    previous_file = instance.tracker.previous('file')
    file_changed = previous_file != instance.file
    should_generate_preview = False

    if created and file_changed:
        should_generate_preview = True

    if not created and file_changed:
        should_generate_preview = True

    if not should_generate_preview:
        return

    site = Site.objects.get(is_default_site=True)
    settings = FilePreviewsSettings.for_site(site)

    if settings.is_enabled:
        fp = FilePreviews(
            api_key=settings.api_key,
            api_secret=settings.api_secret
        )

        host_url = site.root_url
        document_url = '{}{}'.format(host_url, instance.url)
        callback_url = '{}{}'.format(host_url, reverse('filepreviews_webhook'))

        options = {
            'callback_url': callback_url,
            'data': {
                'document_id': instance.pk
            }
        }

        options.update(previews_options_callback(instance))
        fp.generate(document_url, **options)
import json

import pytest
import responses

from filepreviews import API_URL, FilePreviews, exceptions


file_previews = FilePreviews(
    api_key='DUMMY_API_KEY',
    api_secret='DUMMY_SECRET_KEY'
)


@responses.activate
def test_api_generate():
    def request_callback(request):
        body = {
            'id': '1',
            'status': 'pending',
            'preview': None,
            'thumbnails': None,
            'original_file': None,
            'user_data': None,
            'url': 'http://example.com/v2/previews/1/'
        }

        headers = {
            'content-type': 'application/json',
            'location': body['url']
        }
Beispiel #6
0
def filepreviews_client():
    return FilePreviews(api_key=settings.FILEPREVIEW_APIKEY,
                        api_secret=settings.FILEPREVIEW_SECRET)
Beispiel #7
0
import json

import pytest
import responses

from filepreviews import API_URL, FilePreviews, exceptions

file_previews = FilePreviews(api_key="DUMMY_API_KEY",
                             api_secret="DUMMY_SECRET_KEY")


@responses.activate
def test_api_generate():
    def request_callback(request):
        body = {
            "id": "1",
            "status": "pending",
            "preview": None,
            "thumbnails": None,
            "original_file": None,
            "user_data": None,
            "url": "http://example.com/v2/previews/1/",
        }

        headers = {"content-type": "application/json", "location": body["url"]}

        return (201, headers, json.dumps(body))

    responses.add_callback(
        responses.POST,
        API_URL + "/previews/",
Beispiel #8
0
from django.contrib.postgres.search import SearchVectorField
from django.core.files import File
from django.db import models
from django.utils.functional import cached_property
from django.utils.module_loading import import_string
from django_extensions.db.fields import AutoSlugField
from filepreviews import FilePreviews
from google.cloud import vision
from google.oauth2.service_account import Credentials
from google.protobuf.json_format import MessageToDict

from ..utils.models import BaseModel

if settings.FILEPREVIEWS_API_KEY and settings.FILEPREVIEWS_API_SECRET:
    filepreviews = FilePreviews(
        api_key=settings.FILEPREVIEWS_API_KEY,
        api_secret=settings.FILEPREVIEWS_API_SECRET,
    )
else:
    filepreviews = None

if settings.GOOGLE_APPLICATION_CREDENTIALS:
    service_account_info = json.loads(settings.GOOGLE_APPLICATION_CREDENTIALS)
    credentials = Credentials.from_service_account_info(service_account_info)
else:
    credentials = None

document_storage = import_string(settings.CONTRACTS_DOCUMENT_STORAGE)()


def get_filename_from_content_disposition(value):
    _, parsed_header = cgi.parse_header(value)