Skip to content

Commit

Permalink
[Mod] RestClient: restore logger
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoric committed Sep 24, 2019
1 parent 2bf5486 commit 033740a
Showing 1 changed file with 39 additions and 6 deletions.
45 changes: 39 additions & 6 deletions vnpy/api/rest/rest_client.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import json
import logging
import multiprocessing
import os
import sys
import traceback
import uuid
from datetime import datetime
from enum import Enum
from multiprocessing.dummy import Pool
from threading import Lock, Thread
from types import TracebackType
from typing import Any, Callable, List, Optional, Type, Union
from vnpy.trader.utility import get_file_logger

import requests

Expand Down Expand Up @@ -131,6 +134,7 @@ def __init__(self):
"""
self.url_base = '' # type: str
self._active = False
self.logger: Optional[logging.Logger] = None

self.proxies = None

Expand All @@ -146,13 +150,26 @@ def __init__(self):
def alive(self):
return self._active

def init(self, url_base: str, proxy_host: str = "", proxy_port: int = 0):
def init(self,
url_base: str,
proxy_host: str = "",
proxy_port: int = 0,
log_path: Optional[str] = None,
):
"""
Init rest client with url_base which is the API root address.
e.g. 'https://www.bitmex.com/api/v1/'
:param url_base:
:param proxy_host:
:param proxy_port:
:param log_path: optional. file to save log.
"""
self.url_base = url_base

if log_path is not None:
self.logger = get_file_logger(log_path)
self.logger.setLevel(logging.DEBUG)

if proxy_host and proxy_port:
proxy = f"{proxy_host}:{proxy_port}"
self.proxies = {"http": proxy, "https": proxy}
Expand Down Expand Up @@ -349,6 +366,11 @@ def _process_stream_request(self, request: Request):
self._process_request(request)
self._clean_finished_streams()

def _log(self, msg, *args):
logger = self.logger
if logger:
logger.debug(msg, *args)

def _process_request(
self, request: Request
):
Expand All @@ -358,22 +380,33 @@ def _process_request(
try:
with self._get_session() as session:
request = self.sign(request)

url = self.make_full_url(request.path)

# send request
uid = uuid.uuid4()
stream = request.stream
method = request.method
headers = request.headers
params = request.params
data = request.data
self._log("[%s] sending request %s %s, headers:%s, params:%s, data:%s",
uid, method, url,
headers, params, data)
response = session.request(
request.method,
method,
url,
headers=request.headers,
params=request.params,
data=request.data,
headers=headers,
params=params,
data=data,
proxies=self.proxies,
stream=stream,
)
request.response = response
status_code = response.status_code

self._log("[%s] received response from %s:%s", uid, method, url)

# check result & call corresponding callbacks
if not stream: # normal API:
# just call callback with all contents received.
if status_code // 100 == 2: # 2xx codes are all successful
Expand Down

0 comments on commit 033740a

Please sign in to comment.