mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-22 06:45:37 +08:00
refactor: Simplify datastore.py
Use requests library
This commit is contained in:
@@ -9,13 +9,7 @@ import datetime
|
|||||||
import zlib
|
import zlib
|
||||||
import base64
|
import base64
|
||||||
import io
|
import io
|
||||||
import ssl
|
import requests
|
||||||
|
|
||||||
try:
|
|
||||||
from urllib.request import urlopen
|
|
||||||
from urllib.error import HTTPError, URLError
|
|
||||||
except ImportError:
|
|
||||||
from urllib2 import urlopen, HTTPError, URLError
|
|
||||||
|
|
||||||
|
|
||||||
class DbInfo:
|
class DbInfo:
|
||||||
@@ -116,22 +110,15 @@ class DbInfosLoaderRemote(DbInfosLoader):
|
|||||||
# Open the url
|
# Open the url
|
||||||
url = host + '/shared/' + self.version + '/dbdata/' + self.file_name
|
url = host + '/shared/' + self.version + '/dbdata/' + self.file_name
|
||||||
try:
|
try:
|
||||||
ctx = ssl.create_default_context()
|
response = requests.get(url)
|
||||||
ctx.check_hostname = False
|
response.raise_for_status()
|
||||||
ctx.verify_mode = ssl.CERT_NONE
|
|
||||||
response = urlopen(url, context=ctx)
|
|
||||||
timestamp = datetime.datetime.strptime(response.headers['last-modified'], '%a, %d %b %Y %H:%M:%S GMT')
|
timestamp = datetime.datetime.strptime(response.headers['last-modified'], '%a, %d %b %Y %H:%M:%S GMT')
|
||||||
print("Getting db info " + url)
|
print("Getting db info " + url)
|
||||||
content = response.read().decode('utf-8')
|
j = response.json()
|
||||||
j = json.loads(content)
|
|
||||||
db_infos = self.convert_json_to_db_infos(j)
|
db_infos = self.convert_json_to_db_infos(j)
|
||||||
return db_infos, j, timestamp
|
return db_infos, j, timestamp
|
||||||
except HTTPError as e:
|
except requests.HTTPError as e:
|
||||||
error = "HTTP Error: " + str(e.code) + ' ' + url
|
print(f"HTTP Error: {e}")
|
||||||
print(error)
|
|
||||||
except URLError as e:
|
|
||||||
error = "HTTP Error: " + e.reason + ' ' + url
|
|
||||||
print(error)
|
|
||||||
|
|
||||||
|
|
||||||
def write_text_to_file(file_path, text_content, timestamp):
|
def write_text_to_file(file_path, text_content, timestamp):
|
||||||
@@ -186,22 +173,15 @@ class BaseSync:
|
|||||||
|
|
||||||
url = self.host + '/shared/' + self.version + '/' + data_type + '/' + file_name
|
url = self.host + '/shared/' + self.version + '/' + data_type + '/' + file_name
|
||||||
try:
|
try:
|
||||||
ctx = ssl.create_default_context()
|
response = requests.get(url)
|
||||||
ctx.check_hostname = False
|
response.raise_for_status()
|
||||||
ctx.verify_mode = ssl.CERT_NONE
|
|
||||||
response = urlopen(url, context=ctx)
|
|
||||||
timestamp = datetime.datetime.strptime(response.headers['last-modified'], '%a, %d %b %Y %H:%M:%S GMT')
|
timestamp = datetime.datetime.strptime(response.headers['last-modified'], '%a, %d %b %Y %H:%M:%S GMT')
|
||||||
print("Syncing file " + url)
|
print("Syncing file " + url)
|
||||||
file_path = os.path.join(self.target_path, 'shared', data_type, file_name)
|
file_path = os.path.join(self.target_path, 'shared', data_type, file_name)
|
||||||
content = response.read().decode('utf-8')
|
write_text_to_file(file_path, response.text, timestamp)
|
||||||
write_text_to_file(file_path, content, timestamp)
|
|
||||||
|
|
||||||
except HTTPError as e:
|
except requests.HTTPError as e:
|
||||||
error = "HTTP Error: " + str(e.code) + ' ' + url
|
print(f"HTTP Error: {e}")
|
||||||
print(error)
|
|
||||||
except URLError as e:
|
|
||||||
error = "HTTP Error: " + e.reason + ' ' + url
|
|
||||||
print(error)
|
|
||||||
|
|
||||||
|
|
||||||
class DbDataSync(BaseSync):
|
class DbDataSync(BaseSync):
|
||||||
|
|||||||
Reference in New Issue
Block a user