Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Charlie Ernesto committed Apr 9, 2024
1 parent 3dd65b8 commit e33d72a
Show file tree
Hide file tree
Showing 20 changed files with 498 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/calculator_service/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Dockerfile
*.pyc
15 changes: 15 additions & 0 deletions examples/calculator_service/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM python:2.7

ENV PYTHONUNBUFFERED 1

RUN mkdir /code
WORKDIR /code

RUN pip install uwsgi

# ADD requirements.txt /code/
# RUN pip install -r requirements.txt
RUN pip install -e git+https://github.com/brianz/servant.git#egg=servant

COPY . /code/
RUN pip install -e .
28 changes: 28 additions & 0 deletions examples/calculator_service/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Calculator Service demo

This is a really simple/dummy service which implements a calculator with Servant. There is a
Docker file which can make it easier to get up and running. To run this:

```
$ ./build.sh
$ docker run -it --rm servant/calculator bash
root@557b6ba4e2c9:/code# python test_calculator.py
100.0 / 6.0 = 16.6666666667
```

It's also possible to mount the current working directory as a volume so that you can hack on the
`test_calculator.py` script

```
docker run -it --rm -v `pwd`:/code servant/calculator bash
```

Now you can edit any files on your host system and that is reflected in the Docker container automatically.

In this default configuration, service calls are executed as a local Python library call. In order to execute the service calls over HTTP, stand up the uwsgi http server:

```
docker run -it --rm -v `pwd`:/code -p 8888:8888 servant/calculator uwsgi --ini uwsgi.ini
```

Now, from your host system, uncomment the `client.configure()` line in `test_calculator.py`, update the host and port (which is 8888 in our example above) and launch `test_calculator.py` from your host system. You should get the exact same response and see uwsgi reply to the request.
3 changes: 3 additions & 0 deletions examples/calculator_service/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

docker build -t servant/calculator .
Empty file.
55 changes: 55 additions & 0 deletions examples/calculator_service/calculator_service/actions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import servant.fields

from servant.exceptions import ActionError
from servant.exceptions import ServantException
from servant.service.actions import Action


class AddAction(Action):
number1 = servant.fields.IntField(
required=True,
)
number2 = servant.fields.IntField(
required=True,
)
result = servant.fields.IntField(
in_response=True,
)

def run(self, **kwargs):
self.result = self.number1 + self.number2


class SubtractAction(AddAction):
def run(self, **kwargs):
self.result = self.number1 - self.number2


class BackwardSubtractAction(SubtractAction):
def run(self, **kwargs):
self.result = self.number2 - self.number1


class MultiplyAction(AddAction):
def run(self, **kwargs):
self.result = self.number1 * self.number2


class DivideAction(Action):
numerator = servant.fields.DecimalField(
required=True,
in_response=True,
)
denominator = servant.fields.DecimalField(
required=True,
in_response=True,
)
quotient = servant.fields.DecimalField(
in_response=True,
)

def run(self, **kwargs):
if self.denominator == 0:
raise ActionError('Cannot divide by zero')
self.quotient = self.numerator / self.denominator

3 changes: 3 additions & 0 deletions examples/calculator_service/calculator_service/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DUMMY_CONFIG = True
VERSION = 1.0
CALCULATOR_NAME = 'calculator_service'
23 changes: 23 additions & 0 deletions examples/calculator_service/calculator_service/service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from servant.service.base import Service

import actions


class CalculatorService(Service):

name = 'calculator_service'
version = 1

action_map = {
'add': actions.AddAction,
'subtract': actions.SubtractAction,
'divide': actions.DivideAction,
}


class CalculatorServiceV2(CalculatorService):
version = 2
action_map = {
'multiply': actions.MultiplyAction,
'subtract': actions.BackwardSubtractAction,
}
1 change: 1 addition & 0 deletions examples/calculator_service/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
git+https://github.com/brianz/servant.git
20 changes: 20 additions & 0 deletions examples/calculator_service/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from setuptools import setup, find_packages

setup(
name = 'calculator_service',
packages=find_packages(),
version = '1.0',
description = 'Example application with Servant',
author='Brian Zambrano',
author_email='[email protected]',
classifiers=[
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'License :: OSI Approved :: GNU General Public License (GPL)',
'Operating System :: OS Independent',
'Development Status :: 1 - Planning',
'Environment :: Console',
'Intended Audience :: Developers',
'Topic :: System :: Distributed Computing',
]
)
68 changes: 68 additions & 0 deletions examples/calculator_service/test_calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import servant.client

calc_client = servant.client.Client('calculator_service', version=1)
simple_client = servant.client.Client('simple_service', version=1)
calc_client2 = servant.client.Client('calculator_service', version=2)

# Uncomment this line and change the connection settings in order to hit an HTTP version of the
# service
# client.configure('http', host='192.168.88.100', port=8888)

def _handle_error(response):
print response.errors, response.field_errors


def do_add(client):
response = client.add(number1=10, number2=15)

if response.is_error():
_handle_error(response)
else:
print response.result


def do_subtract(client):
response = client.subtract(number1=10, number2=15)

if response.is_error():
_handle_error(response)
else:
print response.result


def do_divide(client):
response = client.divide(numerator=100, denominator=6)
# Here are some examples of requests which throw errors
#response = client.divide(numerator=100, denominator=0)
#response = client.divide(numerator=100, denominator='abc')

if response.is_error():
_handle_error(response)
else:
print '%s / %s = %s' % (response.numerator, response.denominator, response.quotient)

def do_multiply(client):
response = client.multiply(number1=12, number2=12)

if response.is_error():
print response.errors
else:
print response.result


calc_client.configure('http', host='192.168.88.100', port=8888)
calc_client2.configure('http', host='192.168.88.100', port=8888)

do_divide(calc_client)

#response = simple_client.get_theater_listing(theater_id=123)
#if not response.is_error():
# for movie in response.movies:
# print movie
#else:
# print response.text
#

do_add(calc_client2)
#calc_client2.service_name = 'fooely'
#do_multiply(calc_client2)
6 changes: 6 additions & 0 deletions examples/calculator_service/uwsgi.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[uwsgi]
http=0.0.0.0:8888
wsgi-file=wsgi_handler.py
processes=1
threads=1
py-auto-reload=3
4 changes: 4 additions & 0 deletions examples/calculator_service/wsgi_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from calculator_service.service import CalculatorService

service = CalculatorService()
application = service.get_wsgi_application
50 changes: 50 additions & 0 deletions examples/simple_service/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import sys
import time

import servant.client

from pprint import pprint as pp

client = servant.client.Client('simple_service', version=1)

try:
if sys.argv[1].lower() == 'http':
client.configure('http', host='localhost', port=8888)
except Exception:
pass

if not client.is_configured():
client.configure('local')

def line():
print
print '-' * 50

#resp = client.ping()
#line()
#pp(resp)

#resp = client.echo(name='brianz')
#line()
#pp(resp)

#resp = client.echo(name='brianz', age=41, is_awesome=True)
#line()
#pp(resp)

#resp = client.get_weather(city='denver', state='CO')
#line()
#pp(resp)
#
n = time.time() - (3600 * 4)

#resp = client.get_historical_weather(
# city_id=2885679,
# start_time=n,
# make_real_request=False)
#line()
#pp(resp)

resp = client.get_theater_listing(theater_id=1234)
line()
pp(resp)
20 changes: 20 additions & 0 deletions examples/simple_service/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from setuptools import setup, find_packages

setup(
name = 'simple_service',
packages=find_packages(),
version = '1.0',
description = 'Example application with Servant',
author='Brian Zambrano',
author_email='[email protected]',
classifiers=[
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'License :: OSI Approved :: GNU General Public License (GPL)',
'Operating System :: OS Independent',
'Development Status :: 1 - Planning',
'Environment :: Console',
'Intended Audience :: Developers',
'Topic :: System :: Distributed Computing',
]
)
Empty file.
Loading

0 comments on commit e33d72a

Please sign in to comment.