Beispiel #1
0
def credentials(config, request) -> Credentials:
    """ Get `--user-token` argument from `CONFIG_FILE` or CLI.

    Args:
        config: `config` fixture.
        request: Pytest `request` fixture.

    Returns:
        Credentials: Trading API's credentials.
    """

    if config is not None:
        int_account = config.get("int_account")
        username = config.get("username")
        password = config.get("password")
        totp_secret_key = config.get("totp_secret_key")
        one_time_password = config.get("one_time_password")
    else:
        int_account = request.config.getoption("--int-account")
        username = request.config.getoption("--username")
        password = request.config.getoption("--password")
        totp_secret_key = request.config.getoption("--totp-secret-key")
        one_time_password = request.config.getoption("--one-time-password")

    credentials = Credentials(
        int_account=int_account,
        username=username,
        password=password,
        totp_secret_key=totp_secret_key,
        one_time_password=one_time_password,
    )

    return credentials
 def __init__(self):
     self.__default_credentials = Credentials(
         int_account=None,
         username=config.DG_USERNAME,
         password=config.DG_PASSWORD,
         one_time_password=None,
         totp_secret_key=config.DG_TOTP_SECRET,
     )
     self.__trading_api = TradingAPI(
         credentials=self.__default_credentials, )
Beispiel #3
0
    def login(self, ns_parser: Namespace):
        # GET ATTRIBUTES
        degiro_model = self.__degiro_model
        default_credentials = degiro_model.login_default_credentials()

        credentials = Credentials()
        credentials.CopyFrom(default_credentials)
        credentials.username = ns_parser.username
        credentials.password = ns_parser.password

        if ns_parser.otp is not None:
            credentials.one_time_password = ns_parser.otp
        if ns_parser.topt_secret is not None:
            credentials.totp_secret_key = ns_parser.topt_secret

        degiro_model.login(credentials=credentials)

        DegiroView.__login_display_success()
Beispiel #4
0
)

# SETUP LOGGING LEVEL
logging.basicConfig(level=logging.DEBUG)

# SETUP CONFIG DICT
with open('config/config.json') as config_file:
    config_dict = json.load(config_file)

# SETUP CREDENTIALS
int_account = config_dict['int_account']
username = config_dict['username']
password = config_dict['password']
credentials = Credentials(
    int_account=int_account,
    username=username,
    password=password,
)

# SETUP TRADING API
trading_api = TradingAPI(credentials=credentials)

# CONNECT
trading_api.connect()

# SETUP REQUEST
today = datetime.date.today()
from_date = CashAccountReport.Request.Date(
    year=2020,
    month=1,
    day=1,
Beispiel #5
0
def get_session_id(
    credentials: Credentials,
    session: requests.Session = None,
    logger: logging.Logger = None,
) -> str:
    """ Establish a connection with Degiro's Trading API.

    Args:
        credentials (Credentials):
            credentials.int_account (int)
                Account unique identifer in Degiro's system.
                It is optional.
            credentials.password (str)
                Password used to log in the website.
                It is mandatory.
            credentials.username (str)
                Username used to log in the website.
                It is mandatory.
            credentials.totp_secret is optional.
                Secret code for Two-factor Authentication (2FA).
                It is optional.
        session (requests.Session, optional):
            If you one wants to reuse existing "Session" object.
            Defaults to None.
        logger (logging.Logger, optional):
            If you one wants to reuse existing "Logger" object.
            Defaults to None.

    Raises:
        ConnectionError: Connection failed.

    Returns:
        str: Session id
    """

    if logger is None:
        logger = build_logger()
    if session is None:
        session = build_session()

    if credentials.HasField('oneof_2fa') is True:
        url = urls.LOGIN + '/totp'
        username = credentials.username
        password = credentials.password

        if credentials.HasField('totp_secret_key') is True:
            totp_secret_key = credentials.totp_secret_key
            one_time_password = str(otp.get_totp(totp_secret_key))
        else:
            one_time_password = credentials.one_time_password

        payload_dict = {
            'username': username,
            'password': password,
            'isPassCodeReset': False,
            'isRedirectToMobile': False,
            'queryParams': {},
            'oneTimePassword': one_time_password,
        }
    else:
        url = urls.LOGIN
        username = credentials.username
        password = credentials.password

        payload_dict = {
            'username': username,
            'password': password,
            'isPassCodeReset': False,
            'isRedirectToMobile': False,
            'queryParams': {},
        }

    request = requests.Request(
        method='POST',
        url=url,
        json=payload_dict,
    )
    prepped = session.prepare_request(request)

    response = None
    try:
        response = session.send(prepped, verify=False)
        response_dict = response.json()
    except Exception as e:
        logger.fatal('response:%s', response)
        raise ConnectionError(e)

    logger.info('get_session_id:response_dict: %s', response_dict)

    if 'sessionId' in response_dict:
        return response_dict['sessionId']
    elif 'status' in response_dict and response_dict['status'] == 6:
        logger.fatal('response_dict:%s', response_dict)
        raise ConnectionError(
            '2FA is enabled, please provide the "totp_secret".')
    else:
        logger.fatal('response_dict:%s', response_dict)
        raise ConnectionError('No session id returned.')
Beispiel #6
0
# SETUP LOGGING LEVEL
logging.basicConfig(level=logging.DEBUG)

# SETUP CONFIG DICT
with open('config/config.json') as config_file:
    config_dict = json.load(config_file)

# SETUP CREDENTIALS
username = config_dict['username']
password = config_dict['password']
one_time_password = config_dict['one_time_password']

credentials = Credentials(
    int_account=None,
    username=username,
    password=password,
    one_time_password=one_time_password,
)

# SETUP TRADING API
trading_api = TradingAPI(credentials=credentials)

# CONNECT
trading_api.connect()

# ACCESS SESSION_ID
session_id = trading_api.connection_storage.session_id

print('You are now connected, with the session id :', session_id)
Beispiel #7
0
# SETUP LOGGING LEVEL
logging.basicConfig(level=logging.DEBUG)

# SETUP CONFIG DICT
with open('config/config.json') as config_file:
    config_dict = json.load(config_file)

# SETUP CREDENTIALS
username = config_dict['username']
password = config_dict['password']
totp_secret_key = config_dict['totp_secret_key']

credentials = Credentials(
    int_account=None,
    username=username,
    password=password,
    totp_secret_key=totp_secret_key,
)

# SETUP TRADING API
trading_api = TradingAPI(credentials=credentials)

# CONNECT
trading_api.connect()

# ACCESS SESSION_ID
session_id = trading_api.connection_storage.session_id

print('You are now connected, with the session id :', session_id)