Skip to content

Commit

Permalink
Dynamic verify flag
Browse files Browse the repository at this point in the history
  • Loading branch information
lmendes86 committed Dec 13, 2018
1 parent fefec3b commit 427b8b3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Post example::

from services.models import Service

service_example = Service(name='simple_service', method='post', url='http://example.com/', headers='{"Content-Type": "<content_type>"'}, parameters= '{"query_param": "<data>"}')
service_example = Service(name='simple_service', method='post', url='http://example.com/', headers='{"Content-Type": "<content_type>"}', parameters= '{"query_param": "<data>"}')
get_data = {'<data>': 'example_data'}
header_data = {'<content_type>': 'application/json'}
service_response = service_example.request(get_data=get_data, header_data=header_data)
Expand All @@ -36,7 +36,7 @@ Or simpler::
from services.models import Service

# This headers will be use for every request to this service
service_example = Service(name='simple_service', method='post', url='http://example.com/', headers='{"Content-Type": "application/json"'})
service_example = Service(name='simple_service', method='post', url='http://example.com/', headers='{"Content-Type": "application/json"}')
# parameters will be put directly to the body of the post request to the service
parameters = {'query_param': 'example_data'}
service_response = service_example.request(parameters=parameters)
Expand Down Expand Up @@ -70,7 +70,7 @@ If you want to publish information to some service but you don't want to wait yo

from services.models import Service

service_example = Service(name='simple_service', method='post', url='http://example.com/', headers='{"Content-Type": "application/json"'})
service_example = Service(name='simple_service', method='post', url='http://example.com/', headers='{"Content-Type": "application/json"}')
parameters = {'query_param': 'example_data'}
# max_retry is the amount of retries do you want to execute the request while the answer is a error (0 if you want to retry "forever").
# retry_interval is the amount of seconds you want to wait between retries.
Expand Down
18 changes: 18 additions & 0 deletions services/migrations/0002_service_verify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.1.4 on 2018-12-13 10:44

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('services', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='service',
name='verify',
field=models.BooleanField(default=True),
),
]
9 changes: 5 additions & 4 deletions services/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Service(models.Model):
url = models.URLField(max_length=200, default='')
method = models.CharField(max_length=20, choices=METHOD_CHOICES, default='get')
timeout = models.IntegerField(blank=True, null=True, default=None)
verify = models.BooleanField(default=True)
headers = models.TextField(blank=True, default='')
parameters = models.TextField(blank=True, default='')
service_failover = models.ManyToManyField("self", through='ServiceFailover', symmetrical=False,
Expand Down Expand Up @@ -125,16 +126,16 @@ def get_from_service(self, url, headers, data):
timeout = 10
try:
if self.method == 'post':
result = requests.post(url, data=json.dumps(data), headers=headers, timeout=timeout, verify=False)
result = requests.post(url, data=json.dumps(data), headers=headers, timeout=timeout, verify=self.verify)
elif self.method == 'get':
data = urllib.parse.urlencode(data)
if data:
url = url + '?' + data
result = requests.get(url, headers=headers, timeout=timeout, verify=False)
result = requests.get(url, headers=headers, timeout=timeout, verify=self.verify)
elif self.method == 'patch':
result = requests.patch(url, data=json.dumps(data), headers=headers, timeout=timeout, verify=False)
result = requests.patch(url, data=json.dumps(data), headers=headers, timeout=timeout, verify=self.verify)
elif self.method == 'put':
result = requests.put(url, data=json.dumps(data), headers=headers, timeout=timeout, verify=False)
result = requests.put(url, data=json.dumps(data), headers=headers, timeout=timeout, verify=self.verify)
try:
response = result.json()
except:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name='django-easy-services',
version='0.1',
version='0.2',
packages=find_packages(),
include_package_data=True,
install_requires=['Django>=1.8', 'requests>=2.20.0', 'asyncio>=3.4.3'],
Expand Down

0 comments on commit 427b8b3

Please sign in to comment.