diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | python-bgtasks.spec | 847 | ||||
-rw-r--r-- | sources | 1 |
3 files changed, 849 insertions, 0 deletions
@@ -0,0 +1 @@ +/bgtasks-0.0.44.tar.gz diff --git a/python-bgtasks.spec b/python-bgtasks.spec new file mode 100644 index 0000000..610adac --- /dev/null +++ b/python-bgtasks.spec @@ -0,0 +1,847 @@ +%global _empty_manifest_terminate_build 0 +Name: python-bgtasks +Version: 0.0.44 +Release: 1 +Summary: Microservice with django +License: MIT License +URL: https://github.com/myrubapa/bgtasks +Source0: https://mirrors.nju.edu.cn/pypi/web/packages/16/bc/c0b92604658465a531a69a467b9bac25c5cee1abb8a77d95582ad46ef43f/bgtasks-0.0.44.tar.gz +BuildArch: noarch + +Requires: python3-pika + +%description +# bgtasks +bgtasks is python library for dealing with data exchange between +micro services using rabbitmq protocol. Moreover, you can use it as trigger for events which belongs to another services. + +## Installation +Use the package manager [pip](https://pip.pypa.io/en/stable/) to install bgtasks. +```shell +pip install bgtasks +``` + + +## Configuration + +##### settings.py +```python +AMQP = { + 'USERNAME': 'guest', + 'PASSWORD': 'guest', + 'VHOST': '/', + 'HOST': 'localhost', + 'PORT': 5672, + 'RPC_SLEEP_TIME': 0.005, + 'RPC_TIMEOUT': 5, +} +``` +#### Default values +``` +'RPC_TIMEOUT': 60 +'RPC_SLEEP_TIME': 0.05, +``` + +## Usage + +### Creating bgtask view for handle actions through `route` +#### app1 tasks.py +```python +from bgtasks import rpc_tasks +from bgtasks import Response + +@rpc_tasks('message') +def handle(data): + print(data) + return Response('I received your message %s' % data) +``` +To get response +```python +from bgtasks import RPCClient + +rpc_client = RPCClient() + +try: + response = rpc_client.call('message', 'Hi') + print(response) +except TimeoutError: + print('Service is not responding') +``` +In order to avoid conflicts between remote procedure calls you should pass parameters **explicitly with keywords** + +To run rpc task run command below +```bash +python manage.py tasks +``` +## RestFramework +### service1 +#### Model +models.py +```python +from django.db import models + +class Category(models.Model): + name = models.CharField(max_length=255) +``` +In this case your `add` should receive arguments' list with explicit variable name `ids` +#### Tasks +tasks.py +```python +from bgtasks import rpc_tasks +from bgtasks import Response +from bgtasks import serializer_class +from testapp.models import Category +from rest_framework import serializers + + +class CategorySerializer(serializers.ModelSerializer): + class Meta: + model = Category + fields = ('id', 'name') + + +@rpc_tasks('add') +@serializer_class(CategorySerializer, many=True) +def handle(serializer): + serializer.save() + return Response(serializer.data) + + +@rpc_tasks('get') +@serializer_class() +def handle(serializer): + queryset = Category.objects.filter(id__in=serializer.validated_data['ids']) + serializer = CategorySerializer(queryset, many=True) + return Response(serializer.data) +``` +### service2 +#### app1 models.py +```python +from django.db import models +from bgtasks.models import RemoteField + +class Product(models.Model): + name = models.CharField(max_length=255) + category = RemoteField() # in our case, it is in another service id + +``` +#### `RemoteField` + +```python +from rest_framework import serializers +from bgtasks.rest_framework.fields import RemoteField +from app1.models import Product + + +class ProductSerializer(serializers.ModelSerializer): + category = RemoteField(route='get') + + class Meta: + model = Product + fields = ('id', 'name', 'category', ) +``` +#### Format +And make sure that returned response should be formed as below format. +```python +{ + 'status': 'success/fail', + 'data': [ + { + 'id': 1, + # 'data' + }, + { + 'id': 2, + # data + } + ] +} +``` + +#### Handling list serializer +In order to avoid from sending many rpc requests at first level of serializer we added RPCSerializerMixin +```python +from bgtasks import RemoteField +from bgtasks.rest_framework.serilaizers import RPCSerializerMixin +from rest_framework import serializers +from app1.models import Product + +class ProductListSerializer(RPCSerializerMixin, serializers.ModelSerializer): + category = RemoteField(route='get') + + class Meta: + model = Product + fields = '__all__' + +users = Product.objects.all() +serializer = ProductListSerializer(users, many=True) +print(serializer.data) +``` +It will send to `route` **one** request with gathered pks in body as `[1,2,3,4,5]`, after which will be iterated to merge current serializer data +which maps to `id` field in rpc response +###### Output +```python +[ + { + 'id': 1, + 'name': 'IPhone', + 'category': { + 'id': 5, + 'name': 'Phone', + } + }, + { + 'id': 2, + 'name': 'LG Smart Tv', + 'category': { + 'id': 3, + 'name': 'TV', + } + }, +] +``` + +### Merge methods +To handle `many=True` in serializer we introduce `RPCSerializerMixin` which uses merge functions. +You can import them as below, and to understand can look to function profile. +```python +from bgtasks.utils.merge import merge, merge_dict, merge_obj +``` + +## Testing +Add `ENVIRONMENT = 'test'` on settings.py in order to imitate response from +another service +```python +import json +from django.test import TestCase +from django.test import Client +from bgtasks import rpc_tasks +from bgtasks import RPCClient +from bgtasks import SUCCESS +from bgtasks.amqp import register_tasks + + +@rpc_tasks('user.add') +def add_user(data): + return 1 + +class RPCTestCase(TestCase): + def setUp(self): + register_tasks() # If you want to run your tasks to test them out, not only rpc tasks which are registered inside of your test file + + def test_add_user(self): + data = {'username': 'john', 'password': 'smith'} + c = Client() + response = c.post('/user/add/', data) + data = json.loads(response.content) + self.assertEqual(response.status_code, 201) + self.assertEqual(data['user_id'], 1) + + + def test_your_tasks(self): + data = RPCClient().call('mytasks', {}) + self.assertEqual(data['status'], SUCCESS) + +``` + + +## Contributing +Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. + +Please make sure to update tests as appropriate. + +## How to deploy +Create config file in home directory `~/.pypirc` +``` +[distutils] +index-servers=pypi +[pypi] +repository = https://upload.pypi.org/legacy/ +username = myrubapa +``` +After run command for build and deploy +```shell +python3 setup.py sdist bdist_wheel +python3 -m twine upload dist/* +``` + +for more detail read [packaging-projects](https://packaging.python.org/tutorials/packaging-projects/) +## License +[MIT](https://choosealicense.com/licenses/mit/) + + + +%package -n python3-bgtasks +Summary: Microservice with django +Provides: python-bgtasks +BuildRequires: python3-devel +BuildRequires: python3-setuptools +BuildRequires: python3-pip +%description -n python3-bgtasks +# bgtasks +bgtasks is python library for dealing with data exchange between +micro services using rabbitmq protocol. Moreover, you can use it as trigger for events which belongs to another services. + +## Installation +Use the package manager [pip](https://pip.pypa.io/en/stable/) to install bgtasks. +```shell +pip install bgtasks +``` + + +## Configuration + +##### settings.py +```python +AMQP = { + 'USERNAME': 'guest', + 'PASSWORD': 'guest', + 'VHOST': '/', + 'HOST': 'localhost', + 'PORT': 5672, + 'RPC_SLEEP_TIME': 0.005, + 'RPC_TIMEOUT': 5, +} +``` +#### Default values +``` +'RPC_TIMEOUT': 60 +'RPC_SLEEP_TIME': 0.05, +``` + +## Usage + +### Creating bgtask view for handle actions through `route` +#### app1 tasks.py +```python +from bgtasks import rpc_tasks +from bgtasks import Response + +@rpc_tasks('message') +def handle(data): + print(data) + return Response('I received your message %s' % data) +``` +To get response +```python +from bgtasks import RPCClient + +rpc_client = RPCClient() + +try: + response = rpc_client.call('message', 'Hi') + print(response) +except TimeoutError: + print('Service is not responding') +``` +In order to avoid conflicts between remote procedure calls you should pass parameters **explicitly with keywords** + +To run rpc task run command below +```bash +python manage.py tasks +``` +## RestFramework +### service1 +#### Model +models.py +```python +from django.db import models + +class Category(models.Model): + name = models.CharField(max_length=255) +``` +In this case your `add` should receive arguments' list with explicit variable name `ids` +#### Tasks +tasks.py +```python +from bgtasks import rpc_tasks +from bgtasks import Response +from bgtasks import serializer_class +from testapp.models import Category +from rest_framework import serializers + + +class CategorySerializer(serializers.ModelSerializer): + class Meta: + model = Category + fields = ('id', 'name') + + +@rpc_tasks('add') +@serializer_class(CategorySerializer, many=True) +def handle(serializer): + serializer.save() + return Response(serializer.data) + + +@rpc_tasks('get') +@serializer_class() +def handle(serializer): + queryset = Category.objects.filter(id__in=serializer.validated_data['ids']) + serializer = CategorySerializer(queryset, many=True) + return Response(serializer.data) +``` +### service2 +#### app1 models.py +```python +from django.db import models +from bgtasks.models import RemoteField + +class Product(models.Model): + name = models.CharField(max_length=255) + category = RemoteField() # in our case, it is in another service id + +``` +#### `RemoteField` + +```python +from rest_framework import serializers +from bgtasks.rest_framework.fields import RemoteField +from app1.models import Product + + +class ProductSerializer(serializers.ModelSerializer): + category = RemoteField(route='get') + + class Meta: + model = Product + fields = ('id', 'name', 'category', ) +``` +#### Format +And make sure that returned response should be formed as below format. +```python +{ + 'status': 'success/fail', + 'data': [ + { + 'id': 1, + # 'data' + }, + { + 'id': 2, + # data + } + ] +} +``` + +#### Handling list serializer +In order to avoid from sending many rpc requests at first level of serializer we added RPCSerializerMixin +```python +from bgtasks import RemoteField +from bgtasks.rest_framework.serilaizers import RPCSerializerMixin +from rest_framework import serializers +from app1.models import Product + +class ProductListSerializer(RPCSerializerMixin, serializers.ModelSerializer): + category = RemoteField(route='get') + + class Meta: + model = Product + fields = '__all__' + +users = Product.objects.all() +serializer = ProductListSerializer(users, many=True) +print(serializer.data) +``` +It will send to `route` **one** request with gathered pks in body as `[1,2,3,4,5]`, after which will be iterated to merge current serializer data +which maps to `id` field in rpc response +###### Output +```python +[ + { + 'id': 1, + 'name': 'IPhone', + 'category': { + 'id': 5, + 'name': 'Phone', + } + }, + { + 'id': 2, + 'name': 'LG Smart Tv', + 'category': { + 'id': 3, + 'name': 'TV', + } + }, +] +``` + +### Merge methods +To handle `many=True` in serializer we introduce `RPCSerializerMixin` which uses merge functions. +You can import them as below, and to understand can look to function profile. +```python +from bgtasks.utils.merge import merge, merge_dict, merge_obj +``` + +## Testing +Add `ENVIRONMENT = 'test'` on settings.py in order to imitate response from +another service +```python +import json +from django.test import TestCase +from django.test import Client +from bgtasks import rpc_tasks +from bgtasks import RPCClient +from bgtasks import SUCCESS +from bgtasks.amqp import register_tasks + + +@rpc_tasks('user.add') +def add_user(data): + return 1 + +class RPCTestCase(TestCase): + def setUp(self): + register_tasks() # If you want to run your tasks to test them out, not only rpc tasks which are registered inside of your test file + + def test_add_user(self): + data = {'username': 'john', 'password': 'smith'} + c = Client() + response = c.post('/user/add/', data) + data = json.loads(response.content) + self.assertEqual(response.status_code, 201) + self.assertEqual(data['user_id'], 1) + + + def test_your_tasks(self): + data = RPCClient().call('mytasks', {}) + self.assertEqual(data['status'], SUCCESS) + +``` + + +## Contributing +Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. + +Please make sure to update tests as appropriate. + +## How to deploy +Create config file in home directory `~/.pypirc` +``` +[distutils] +index-servers=pypi +[pypi] +repository = https://upload.pypi.org/legacy/ +username = myrubapa +``` +After run command for build and deploy +```shell +python3 setup.py sdist bdist_wheel +python3 -m twine upload dist/* +``` + +for more detail read [packaging-projects](https://packaging.python.org/tutorials/packaging-projects/) +## License +[MIT](https://choosealicense.com/licenses/mit/) + + + +%package help +Summary: Development documents and examples for bgtasks +Provides: python3-bgtasks-doc +%description help +# bgtasks +bgtasks is python library for dealing with data exchange between +micro services using rabbitmq protocol. Moreover, you can use it as trigger for events which belongs to another services. + +## Installation +Use the package manager [pip](https://pip.pypa.io/en/stable/) to install bgtasks. +```shell +pip install bgtasks +``` + + +## Configuration + +##### settings.py +```python +AMQP = { + 'USERNAME': 'guest', + 'PASSWORD': 'guest', + 'VHOST': '/', + 'HOST': 'localhost', + 'PORT': 5672, + 'RPC_SLEEP_TIME': 0.005, + 'RPC_TIMEOUT': 5, +} +``` +#### Default values +``` +'RPC_TIMEOUT': 60 +'RPC_SLEEP_TIME': 0.05, +``` + +## Usage + +### Creating bgtask view for handle actions through `route` +#### app1 tasks.py +```python +from bgtasks import rpc_tasks +from bgtasks import Response + +@rpc_tasks('message') +def handle(data): + print(data) + return Response('I received your message %s' % data) +``` +To get response +```python +from bgtasks import RPCClient + +rpc_client = RPCClient() + +try: + response = rpc_client.call('message', 'Hi') + print(response) +except TimeoutError: + print('Service is not responding') +``` +In order to avoid conflicts between remote procedure calls you should pass parameters **explicitly with keywords** + +To run rpc task run command below +```bash +python manage.py tasks +``` +## RestFramework +### service1 +#### Model +models.py +```python +from django.db import models + +class Category(models.Model): + name = models.CharField(max_length=255) +``` +In this case your `add` should receive arguments' list with explicit variable name `ids` +#### Tasks +tasks.py +```python +from bgtasks import rpc_tasks +from bgtasks import Response +from bgtasks import serializer_class +from testapp.models import Category +from rest_framework import serializers + + +class CategorySerializer(serializers.ModelSerializer): + class Meta: + model = Category + fields = ('id', 'name') + + +@rpc_tasks('add') +@serializer_class(CategorySerializer, many=True) +def handle(serializer): + serializer.save() + return Response(serializer.data) + + +@rpc_tasks('get') +@serializer_class() +def handle(serializer): + queryset = Category.objects.filter(id__in=serializer.validated_data['ids']) + serializer = CategorySerializer(queryset, many=True) + return Response(serializer.data) +``` +### service2 +#### app1 models.py +```python +from django.db import models +from bgtasks.models import RemoteField + +class Product(models.Model): + name = models.CharField(max_length=255) + category = RemoteField() # in our case, it is in another service id + +``` +#### `RemoteField` + +```python +from rest_framework import serializers +from bgtasks.rest_framework.fields import RemoteField +from app1.models import Product + + +class ProductSerializer(serializers.ModelSerializer): + category = RemoteField(route='get') + + class Meta: + model = Product + fields = ('id', 'name', 'category', ) +``` +#### Format +And make sure that returned response should be formed as below format. +```python +{ + 'status': 'success/fail', + 'data': [ + { + 'id': 1, + # 'data' + }, + { + 'id': 2, + # data + } + ] +} +``` + +#### Handling list serializer +In order to avoid from sending many rpc requests at first level of serializer we added RPCSerializerMixin +```python +from bgtasks import RemoteField +from bgtasks.rest_framework.serilaizers import RPCSerializerMixin +from rest_framework import serializers +from app1.models import Product + +class ProductListSerializer(RPCSerializerMixin, serializers.ModelSerializer): + category = RemoteField(route='get') + + class Meta: + model = Product + fields = '__all__' + +users = Product.objects.all() +serializer = ProductListSerializer(users, many=True) +print(serializer.data) +``` +It will send to `route` **one** request with gathered pks in body as `[1,2,3,4,5]`, after which will be iterated to merge current serializer data +which maps to `id` field in rpc response +###### Output +```python +[ + { + 'id': 1, + 'name': 'IPhone', + 'category': { + 'id': 5, + 'name': 'Phone', + } + }, + { + 'id': 2, + 'name': 'LG Smart Tv', + 'category': { + 'id': 3, + 'name': 'TV', + } + }, +] +``` + +### Merge methods +To handle `many=True` in serializer we introduce `RPCSerializerMixin` which uses merge functions. +You can import them as below, and to understand can look to function profile. +```python +from bgtasks.utils.merge import merge, merge_dict, merge_obj +``` + +## Testing +Add `ENVIRONMENT = 'test'` on settings.py in order to imitate response from +another service +```python +import json +from django.test import TestCase +from django.test import Client +from bgtasks import rpc_tasks +from bgtasks import RPCClient +from bgtasks import SUCCESS +from bgtasks.amqp import register_tasks + + +@rpc_tasks('user.add') +def add_user(data): + return 1 + +class RPCTestCase(TestCase): + def setUp(self): + register_tasks() # If you want to run your tasks to test them out, not only rpc tasks which are registered inside of your test file + + def test_add_user(self): + data = {'username': 'john', 'password': 'smith'} + c = Client() + response = c.post('/user/add/', data) + data = json.loads(response.content) + self.assertEqual(response.status_code, 201) + self.assertEqual(data['user_id'], 1) + + + def test_your_tasks(self): + data = RPCClient().call('mytasks', {}) + self.assertEqual(data['status'], SUCCESS) + +``` + + +## Contributing +Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. + +Please make sure to update tests as appropriate. + +## How to deploy +Create config file in home directory `~/.pypirc` +``` +[distutils] +index-servers=pypi +[pypi] +repository = https://upload.pypi.org/legacy/ +username = myrubapa +``` +After run command for build and deploy +```shell +python3 setup.py sdist bdist_wheel +python3 -m twine upload dist/* +``` + +for more detail read [packaging-projects](https://packaging.python.org/tutorials/packaging-projects/) +## License +[MIT](https://choosealicense.com/licenses/mit/) + + + +%prep +%autosetup -n bgtasks-0.0.44 + +%build +%py3_build + +%install +%py3_install +install -d -m755 %{buildroot}/%{_pkgdocdir} +if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi +if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi +if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi +if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi +pushd %{buildroot} +if [ -d usr/lib ]; then + find usr/lib -type f -printf "/%h/%f\n" >> filelist.lst +fi +if [ -d usr/lib64 ]; then + find usr/lib64 -type f -printf "/%h/%f\n" >> filelist.lst +fi +if [ -d usr/bin ]; then + find usr/bin -type f -printf "/%h/%f\n" >> filelist.lst +fi +if [ -d usr/sbin ]; then + find usr/sbin -type f -printf "/%h/%f\n" >> filelist.lst +fi +touch doclist.lst +if [ -d usr/share/man ]; then + find usr/share/man -type f -printf "/%h/%f.gz\n" >> doclist.lst +fi +popd +mv %{buildroot}/filelist.lst . +mv %{buildroot}/doclist.lst . + +%files -n python3-bgtasks -f filelist.lst +%dir %{python3_sitelib}/* + +%files help -f doclist.lst +%{_docdir}/* + +%changelog +* Thu May 18 2023 Python_Bot <Python_Bot@openeuler.org> - 0.0.44-1 +- Package Spec generated @@ -0,0 +1 @@ +a7152aa195a4c507a6c189d45f683dac bgtasks-0.0.44.tar.gz |