Skip to content
Snippets Groups Projects
Commit 2607784a authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

push

parent 166270de
No related branches found
No related tags found
No related merge requests found
engines:
duplication:
enabled: true
config:
languages:
python:
fixme:
enabled: true
markdownlint:
enabled: true
pep8:
enabled: true
exclude_paths:
- tests/**
- setup.py
ratings:
paths:
- flask_minijson.py
checks:
argument-count:
config:
threshold: 15
method-complexity:
config:
threshold: 50
method-count:
config:
threshold: 85
file-lines:
enabled: true
config:
threshold: 700
[run]
branch=1
source=
rapid_minijson
[report]
exclude_lines=
pragma: no cover
language: python
stages:
- name: test
cache: pip
before_script:
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
- chmod +x ./cc-test-reporter
- ./cc-test-reporter before-build
- pip install -r requirements.txt
- pip install -U pytest-xdist pytest-cov pytest pytest-forked pluggy py
- python setup.py install
jobs:
include:
- stage: test
python: "3.5"
script:
- pytest -n 8 --cov=flask_minijson
after_script:
- coverage xml
- ./cc-test-reporter after-build -t coverage.py --exit-code $TRAVIS_TEST_RESULT
- stage: test
python: "3.6"
script:
- pytest -n 8 --cov=flask_minijson
after_script:
- coverage xml
- ./cc-test-reporter after-build -t coverage.py --exit-code $TRAVIS_TEST_RESULT
- stage: test
python: "3.7"
script:
- pytest -n 8 --cov=flask_minijson
after_script:
- coverage xml
- ./cc-test-reporter after-build -t coverage.py --exit-code $TRAVIS_TEST_RESULT
- stage: test
python: "3.8"
script:
- pytest -n 8 --cov=flask_minijson
after_script:
- coverage xml
- ./cc-test-reporter after-build -t coverage.py --exit-code $TRAVIS_TEST_RESULT
- stage: test
python: "3.9"
script:
- pytest -n 8 --cov=flask_minijson
after_script:
- coverage xml
- ./cc-test-reporter after-build -t coverage.py --exit-code $TRAVIS_TEST_RESULT
- stage: test
python: "pypy3.5"
script:
- pytest -n 8
include requirements.txt
# flask-minijson
An extension for Flask to allow clients to submit data using the application/minijson codec
flask-json is required to be initialized before `FlaskMiniJSON`, in such a way:
```python
from flask_minijson import FlaskMiniJSON
app = Flask(__name__)
FlaskJSON(app)
FlaskMiniJSON(app)
```
import typing as tp
import minijson
from flask import Flask
from flask_json import JsonRequest
class MiniJSONRequest(JsonRequest):
def get_json(self, force=False, silent=False, cache=True):
"""
Return JSON data, if content type is application/minijson it will be loaded
via minijson, else it will be loaded the normal way.
"""
if self.headers['Content-Type'] == 'application/minijson':
return minijson.loads(self.get_data())
else:
return super().get_json(force, silent, cache)
class FlaskMiniJSON(object):
"""Flask-MiniJSON extension class."""
def __init__(self, app: tp.Optional[Flask] = None):
self._app = app
self._error_handler_func = None
self._decoder_error_func = None
if app is not None:
self.init_app(app)
def init_app(self, app: Flask):
if 'json' not in app.extensions:
raise RuntimeError('flask-json must be initialized before MiniJSON!')
app.extensions['minijson'] = self
self._app = app
app.request_class = MiniJSONRequest
# coding: utf-8
[metadata]
version = 2.5a1
name = flask-minijson
long_description = file: README.md
long_description_content_type = text/markdown; charset=UTF-8
author = Piotr Maślanka
license_files =
LICENSE
author_email = piotr.maslanka@dronehub.ai
description = A Flask extension to allow client's to submit data using the MiniJSON codec
url = https://github.com/Dronehub/flask-minijson
project_urls =
Code = https://github.com/Dronehub/flask-minijson
Issue tracker = https://github.com/Dronehub/flask-minijson/issues
classifier =
Development Status :: 4 - Beta
Programming Language :: Python
Programming Language :: Python :: 3.5
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Programming Language :: Python :: Implementation :: CPython
Programming Language :: Python :: Implementation :: PyPy
Operating System :: OS Independent
License :: OSI Approved :: MIT License
Topic :: Software Development :: Libraries
[pycodestyle]
max-line-length = 100
[pep8]
max-line-length = 100
[bdist_wheel]
universal = 0
[options]
python_requires = !=2.7.*,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*
py_modules = flask_minijson
from setuptools import setup
setup()
from flask import Flask, request
from flask_json import FlaskJSON, as_json
from flask_minijson import FlaskMiniJSON
app = Flask(__name__)
FlaskJSON(app)
FlaskMiniJSON(app)
app.config['DEBUG'] = True
app.config['TESTING'] = True
@app.route('/v1', methods=['POST'])
@as_json
def example_point():
if request.get_json() == {'1': '2'}:
return {'status': 'ok'},
else:
return {'status': 'fail'}
import unittest
import minijson
from tests.app import app
class TestMiniJSON(unittest.TestCase):
def setUp(self):
self.client = app.test_client()
def test_minijson(self):
data = minijson.dumps({'1': '2'})
resp = self.client.post('/v1', data=data, headers={'Content-Type': 'application/minijson'})
self.assertEqual(resp.get_json(), {'status': 'ok'})
resp = self.client.post('/v1', json={'1': '3'})
self.assertEqual(resp.get_json(), {'status': 'fail'})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment