Skip to content

Commit 96b8983

Browse files
committed
Merge branch 'master' into feature/docs
2 parents e902d0c + 5736106 commit 96b8983

File tree

14 files changed

+641
-71
lines changed

14 files changed

+641
-71
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ language: python
22
python:
33
- '3.6'
44
install:
5+
- pip install pipenv
56
- pip install -U tox coveralls
67

78
script:

Pipfile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[[source]]
2+
url = "https://pypi.org/simple"
3+
verify_ssl = true
4+
name = "pypi"
5+
6+
[packages]
7+
flask = "*"
8+
jaeger-client = "==3.12.0"
9+
python-json-logger = "==0.1.10"
10+
pyyaml = "==4.2b4"
11+
anyconfig = "==0.9.8"
12+
swagger-ui-bundle = "==0.0.2"
13+
connexion = {extras = ["swagger-ui"],version = "==2.1.0"}
14+
flask-opentracing = "==0.2.0"
15+
16+
[dev-packages]
17+
requests-mock = "*"
18+
coverage = "*"
19+
mock = "*"
20+
nose = "*"
21+
pylint = "*"
22+
tox = "*"
23+
24+
[requires]
25+
python_version = "3.6"

Pipfile.lock

Lines changed: 508 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,52 @@ Encapsulate common rest operations between business services propagating trace h
4040
### pyms/tracer
4141
Create an injector `flask_opentracing.FlaskTracer` to use in our projects
4242

43+
## Pipenv
44+
45+
### Advantages over plain pip and requirements.txt
46+
[Pipenv](https://pipenv.readthedocs.io/en/latest/) generates two files: a `Pipfile`and a `Pipfile.lock`.
47+
* `Pipfile`: Is a high level declaration of the dependencies of your project. It can contain "dev" dependencies (usually test related stuff) and "standard" dependencies which are the ones you'll need for your project to function
48+
* `Pipfile.lock`: Is the "list" of all the dependencies your Pipfile has installed, along with their version and their hashes. This prevents two things: Conflicts between dependencies and installing a malicious module.
49+
50+
### How to...
51+
52+
Here the most 'common' `pipenv` commands, for a more in-depth explanation please refer to the [official documentation](https://pipenv.readthedocs.io/en/latest/).
53+
54+
#### Install pipenv
55+
```bash
56+
pip install pipenv
57+
```
58+
59+
#### Install dependencies defined in a Pipfile
60+
```bash
61+
pipenv install
62+
```
63+
64+
#### Install both dev and "standard" dependencies defined in a Pipfile
65+
```bash
66+
pipenv install --dev
67+
```
68+
69+
#### Install a new module
70+
```bash
71+
pipenv install django
72+
```
73+
74+
#### Install a new dev module (usually test related stuff)
75+
```bash
76+
pipenv install nose --dev
77+
```
78+
79+
#### Install dependencies in production
80+
```bash
81+
pipenv install --deploy
82+
```
83+
84+
#### Start a shell
85+
```bash
86+
pipenv shell
87+
```
88+
4389
## Documentation
4490

4591
This project use MkDocs
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import os
2-
31
from pyms.flask.app import Microservice
42

5-
os.environ["CONFIGMAP_FILE"] = "config.yml"
6-
ms = Microservice(service="my-ms", path=__file__)
3+
ms = Microservice(service="my-ms", path=__file__)

examples/microservice_swagger/main.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import os
2-
31
from pyms.flask.app import Microservice
42

5-
os.environ["CONFIGMAP_FILE"] = "config.yml"
63
ms = Microservice(service="my-ms", path=__file__)
74
app = ms.create_app()
85

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import os
21
from flask import jsonify
2+
33
from pyms.flask.app import Microservice
44

5-
os.environ["CONFIGMAP_FILE"] = "config.yml"
65
ms = Microservice(service="my-minimal-microservice", path=__file__)
76
app = ms.create_app()
87

8+
99
@app.route("/")
1010
def example():
1111
return jsonify({"main": "hello world"})
1212

13+
1314
if __name__ == '__main__':
1415
app.run()

pyms/config/confile.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
class ConfFile(dict):
1515
empty_init = False
16+
default_file = "config.yml"
1617

1718
def __init__(self, *args, **kwargs):
1819
"""
@@ -63,9 +64,9 @@ def __getattr__(self, name, *args, **kwargs):
6364
raise AttrDoesNotExistException("Variable {} not exist in the config file".format(name))
6465

6566
def _get_conf_from_env(self):
66-
file = os.environ.get(CONFIGMAP_FILE_ENVIRONMENT)
67-
logger.info("[CONF] Searching file in ENV[{}]: {}...".format(CONFIGMAP_FILE_ENVIRONMENT, file))
68-
return self._get_conf_from_file(os.environ.get(CONFIGMAP_FILE_ENVIRONMENT))
67+
config_file = os.environ.get(CONFIGMAP_FILE_ENVIRONMENT, self.default_file)
68+
logger.info("[CONF] Searching file in ENV[{}]: {}...".format(CONFIGMAP_FILE_ENVIRONMENT, config_file))
69+
return self._get_conf_from_file(config_file)
6970

7071
def _get_conf_from_file(self, path: Text) -> dict:
7172
if not path or not os.path.isfile(path):

pyms/flask/services/requests.py

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
from pyms.flask.services.driver import DriverService
88

9-
DATA = 'data'
10-
119

1210
class Service(DriverService):
1311
service = "requests"
@@ -26,7 +24,7 @@ def insert_trace_headers(self, headers):
2624
2725
:rtype: dict
2826
"""
29-
self._tracer = current_app.tracer
27+
3028
try:
3129
# FLASK https://github.com/opentracing-contrib/python-flask
3230
span = self._tracer.get_span()
@@ -46,6 +44,7 @@ def _get_headers(self, headers):
4644
if not headers:
4745
headers = {}
4846

47+
self._tracer = current_app.tracer
4948
if self._tracer:
5049
headers = self.insert_trace_headers(headers)
5150

@@ -63,6 +62,23 @@ def _build_url(url, path_params=None):
6362

6463
return url.format_map(path_params)
6564

65+
def parse_response(self, response):
66+
"""Parses response's json object. Checks configuration in order to parse a concrete node or the whole response.
67+
68+
:param response: request's response that contains a valid json
69+
70+
:rtype: dict
71+
"""
72+
73+
try:
74+
data = response.json()
75+
if self.config.data:
76+
data = data.get(self.config.data, {})
77+
return data
78+
except ValueError:
79+
current_app.logger.warning("Response.content is not a valid json {}".format(response.content))
80+
return {}
81+
6682
def get(self, url, path_params=None, params=None, headers=None, **kwargs):
6783
"""Sends a GET request.
6884
@@ -99,15 +115,7 @@ def get_for_object(self, url, path_params=None, params=None, headers=None, **kwa
99115
"""
100116

101117
response = self.get(url, path_params=path_params, params=params, headers=headers, **kwargs)
102-
103-
try:
104-
data = response.json()
105-
if self.config.data:
106-
data = data.get(self.config.data, {})
107-
return data
108-
except ValueError:
109-
current_app.logger.warning("Response.content is not a valid json {}".format(response.content))
110-
return {}
118+
return self.parse_response(response)
111119

112120
def post(self, url, path_params=None, data=None, json=None, headers=None, **kwargs):
113121
"""Sends a POST request.
@@ -147,12 +155,7 @@ def post_for_object(self, url, path_params=None, data=None, json=None, headers=N
147155
"""
148156

149157
response = self.post(url, path_params=path_params, data=data, json=json, headers=headers, **kwargs)
150-
151-
try:
152-
return response.json().get(DATA, {})
153-
except ValueError:
154-
current_app.logger.warning("Response.content is not a valid json {}".format(response.content))
155-
return {}
158+
return self.parse_response(response)
156159

157160
def put(self, url, path_params=None, data=None, headers=None, **kwargs):
158161
"""Sends a PUT request.
@@ -192,12 +195,7 @@ def put_for_object(self, url, path_params=None, data=None, headers=None, **kwarg
192195
"""
193196

194197
response = self.put(url, path_params=path_params, data=data, headers=headers, **kwargs)
195-
196-
try:
197-
return response.json().get(DATA, {})
198-
except ValueError:
199-
current_app.logger.warning("Response.content is not a valid json {}".format(response.content))
200-
return {}
198+
return self.parse_response(response)
201199

202200
def delete(self, url, path_params=None, headers=None, **kwargs):
203201
"""Sends a DELETE request.

requirements-tests.txt

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)