Query language to bridge the gap between REST API and ORM capability
Project description
BridgeQL
bridgeql
is part of VMware's support for open source development
and community.
A library which will add feature to serve your model over rest API
- This will allow users to make ORM query based on any models present in the django app
- This will ask user to provide request in defined format and will serve the API response as json data
- This will allow users to make filter, selection, ordering, slicing and count of model objects
As of today we only support for django, will add support for sqlalchemy soon.
License
bridgeql
is release under the BSD-2 license, see the LICENSE file.
SPDX-License-Identifier: BSD-2-Clause
Django Integration
Installation
You can install it directly from pypi.org using
pip install bridgeql
The bridgeql library can be integrated to the Django app by editing settings
file by including bridgeql
in the settings.INSTALLED_APPS
variable.
Another change required is to add a url to your existing project as
projectname/projectname/settings.py
INSTALLED_APPS = [
...
'bridgeql',
...
]
URLs Configuration
In your project you can edit urls.py
, to include the bridgeql
urls.
from bridgeql.django import urls as bridgeql_urls
...
urlpatterns = [
...
path('api/bridgeql/', include(bridgeql_urls)),
...
]
...
This way your app will be ready to serve the REST API to expose model query, you can request the API to perform basic CRUD operations using one of the following URLs
- Create:
create/db_name/app_name/model_name
- Read:
read/db_name/app_name/model_name/<?pk>
- Update:
update/db_name/app_name/model_name/pk
- Delete:
delete/db_name/app_name/model_name/pk
Example Usage:
import requests
params = {
'filter': {
'os__name': 'os-name-1'
},
'fields': ['ip', 'name', 'id'],
'exclude': {
'name': 'machine-name-11'
},
'order_by': ['ip'],
'limit': 5,
'offset': 10, # default 0
}
# db_name could be default
api_url = '<yoursite.com>/api/bridgeql/read/default/machine/Machine'
resp = requests.get(api_url, {'payload': json.dumps(params)})
result = resp.json()
The above parameters will translate into running the model query for Machine
model of machine
django app.
Machine.objects.using('default')
.filter(os__name = 'os-name-1')
.exclude(name = 'machine-name-11')
.values(['ip', 'name', 'id'])
.order_by('ip')[10:15] # offset: offset + limit
BridgeQL Settings
Settings available in BridgeQL
and their default values
BRIDGEQL_ALLOWED_APPS
Default: [list of local apps]
(list)
By default, it will try to lookup your local apps for the project, considering settings.BASE_DIR
or settings.SITE_ROOT
(whichever is found) as your project root directory. If none of this will be available, WARNING
will be there to setup BRIDGEQL_ALLOWED_APPS
as application starts up.
Adding values as list of desired apps to BRIDGEQL_ALLOWED_APPS
will allow you expose models only for the selected apps.
This way you can add django backend apps as well as third party apps e.g. django.contrib.auth
which can expose User
model.
You can combine next settings BRIDGEQL_RESTRICTED_MODELS
to restrict few columns of that particular model e.g password
.
BRIDGEQL_ALLOWED_APPS = ['machine', 'django.contrib.auth']
BRIDGEQL_RESTRICTED_MODELS
Default: {}
(Empty dictionary)
Dictionary mapping app_label.model_name strings to list/bool(True). The value represents list of fields to be restricted for the model as provided in key. If the value is set to True
, all the fields of that model will be restricted to lookup.
BRIDGEQL_RESTRICTED_MODELS = {
'auth.User': True,
'machine.OperatingSystem': ['license_key'],
}
BRIDGEQL_AUTHENTICATION_DECORATOR
Default:
{
'reader': '',
'writer': ''
}
Above setting allows you to use an authentication decorator to authenticate your client's requests. You can provide a custom authentication decorator, for reader and writer separately, whichever suits your application usecase, e.g login_required, same_subnet, etc.
Default value for BRIDGEQL_AUTHENTICATION_DECORATOR
will allow you to access API without authentication.
BRIDGEQL_AUTHENTICATION_DECORATOR = {
'reader': 'bridgeql.auth.basic_auth',
'writer': 'bridgeql.auth.basic_auth'
}
bridgeql.auth.basic_auth
is available as a basic authentication method where you can pass authorization header as
Authorization: Basic base64(username:password)
for each request.
Build & Run
- make test
- source venv/bin/activate && tox
- python -m pip install --upgrade build
- python -m build
Documentation
Contributing
The bridgeql project team welcomes contributions from the community. Before you start working with bridgeql, please read our Developer Certificate of Origin. All contributions to this repository must be signed as described on that page. Your signature certifies that you wrote the patch or have the right to pass it on as an open-source patch. For more detailed information, refer to CONTRIBUTING_DCO.md.
Authors
Created and maintained by
Piyus Kumar
Priyank Singh
Copyright © 2023, VMware, Inc. All rights reserved.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file bridgeql-0.2.2.tar.gz
.
File metadata
- Download URL: bridgeql-0.2.2.tar.gz
- Upload date:
- Size: 17.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3fe7d2c4a197cd8d91d2f28769c92dc133ca556f45c5d71afe049eb55f4c01a5 |
|
MD5 | fa2c9ea0025bf602bd406284212bdf58 |
|
BLAKE2b-256 | 7ff840524ca94ac7e180e8436e457b00a2ce693943d3b3cbaf28fb2ab5abeb0a |
File details
Details for the file bridgeql-0.2.2-py3-none-any.whl
.
File metadata
- Download URL: bridgeql-0.2.2-py3-none-any.whl
- Upload date:
- Size: 19.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6f9e1eb90050b3f06e878146064517d1a25949e2eaf9c2017d6f4a7122cade2b |
|
MD5 | b7f3f84ffe2f978852386fa3134c20cb |
|
BLAKE2b-256 | b2b837e06fbfff9e67d603204f3a25d24651e89da1a786ba2b92e6d8b44f2901 |