Skip to content
This repository has been archived by the owner on Jun 7, 2020. It is now read-only.

Commit

Permalink
HEAD request for check connection replaced by service online endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
thejoeejoee committed Nov 26, 2017
1 parent 9834687 commit 6e0ce75
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions ifj2017/benchmark/uploader.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# coding=utf-8

import os
import urllib
import urllib.request
import uuid
Expand All @@ -19,21 +18,26 @@ def __init__(self, _api_hostname, token_file):
self._token = None
self._has_connection = False
self._token_file = token_file
self._last_response = None

@property
def has_connection(self):
return self._has_connection

def check_connection(self):
request = urllib.request.Request(self._api_hostname)
request.get_method = lambda: 'HEAD'
request.get_method = lambda: 'GET'

try:
response = urllib.request.urlopen(request)
data = self._request('/api/v1/service-online', {}, force=True)
except URLError as e:
TestLogger.log_warning('Invalid URL {} ({}).'.format(self._api_hostname, e))
TestLogger.log_warning('Problem with connecting to {} ({}).'.format(self._api_hostname, e))
else:
self._has_connection = 200 >= response.status < 400
self._has_connection = (
200 >= self._last_response.status < 400
) and data.get('success')
if data.get('msg'):
TestLogger.log_warning(data.get('msg'))

def authenticate_user(self):
if not self._has_connection:
Expand Down Expand Up @@ -101,14 +105,15 @@ def _save_token(self, token):
with open(self._token_file, 'w') as f:
f.write(token)

def _request(self, url, data):
if not self._has_connection:
def _request(self, url, data, force=False):
if not self._has_connection and not force:
return {}
request = urllib.request.Request(''.join((self._api_hostname, url)))
request.add_header('Content-type', 'application/json')
response = urllib.request.urlopen(request, bytes(dumps(
data
), encoding='utf-8'))
), encoding='utf-8') if data else None)
body = response.read()
response.close()
self._last_response = response
return loads(str(body, encoding='utf-8'), encoding='utf-8')

0 comments on commit 6e0ce75

Please sign in to comment.