Skip to main content
# django-microservices

## About
Simple django package to easily "connect" microservices.

The package loads the service configurations (id, name, host) into a DB table from a json file which can be stored on a shared drive, or hosted on URL.

## Installation
You can simply install the package with pip from **PyPI** or **GitHub**.

**Install with pip from github**

`pip install git+https://github.com/gabor-boros/django-microservices.git`

**Install from pypi**

`pip install djangomicroservices`

## Configuration
Add the `microservices` app to your `INSTALLED_APPS` like this:

```
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'microservices',
...
]
```

After you added it to the `INSTALLED_APPS`, you **must** configure the path where your service configuration exists.
The configuration file is a `json` file. Basically it is an output of `python manage.py dumpdata` command.
It doesn't matter where is your file located. It can be hosted on a website, or located on a *shared* drive.

```
SERVICE_CONFIGURATION_FILE = 'http://myserver.com/services.json'
```

or

```
SERVICE_CONFIGURATION_FILE = 'services.json'
```

**Service configuration example**
*sample.json*
```
[
{
"model": "microservices.service",
"pk": 1,
"fields": {
"name": "auth",
"host": "http://auth.example.com/",
}
},
{
"model": "microservices.service",
"pk": 2,
"fields": {
"name": "search",
"host": "http://search.example.com/",
}
}
]
```

## Usage
After you are done with the installation and configuration, and you loaded your services, you are good to go.
The usage is very simple. Query your service, and call the `remote_call` object method.

**Remote call example**
```
import json
from microservices.models import Service

def can_login(username, password):
authentication_data = json.loads({"username": username: "password": password})

authentication_service = Service.objects.get(name="auth")
response = authentication_service.remote_call(
method, api='/login/', data=authentication_data
)

if response.status_code == 200:
return True

return False

```

The package ships with an other object method called `update_availability`. This method can be used to update your service's status.
To do this you can call it for the object as shown in the example below.

**NOTE**
If the response code is 400, 404, 500 or 503, the value of `is_available` field will be `False`.

**Update service status**
```
from microservices.models import Service

for service in Service.objects.all():
service.update_availability()

```

## Management commands
Three management command ships with this package to help to manage your service configuration.

* `list_services` - Lists your services ordered by their status
* `load_services` - Loads the configuration from the given resource location, set in `settings.py`
* `check_services` - Going thorough your configuration and tries to reach them.
* If the host sends any response (which is not error related), the service will be marked available
* If the response is an error, or the service can not be reached, the service's status will be unavailable

## Tests
To run the tests for this package use the `python manage.py test` command as usual
In case you would like to generate a **coverage** report as well, run the `run_test_coverage.sh` file.

## Contributors
If you would like to help to develop this package please read the CONTRIBUTING guideline. Every PR is highly welcomed.

Metadata

Release files for djangomicroservices 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for djangomicroservices 0.1.2
File Size Uploaded
djangomicroservices-0.1.2.tar.gz 6.4 kB Details

Release files / djangomicroservices-0.1.2.tar.gz

Download URL djangomicroservices-0.1.2.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
a5b10098b40ec23c18ff60ef36bc1344cb2691254e15a4f11fe4c1fa2a175d55
BLAKE2b-256 checksum
How to use checksums
1f4dc342bfb89c8440d8fa9103d92439cd0019934ec3b8ae19349a9ef5e9e954
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.28.0 CPython/2.7.10

Release history Release notifications | RSS feed

This release

0.1.2 This release

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page