Exemplo n.º 1
0
def esi_client_factory(token=None,
                       datasource=None,
                       spec_file=None,
                       version=None,
                       **kwargs):
    """
    Generates an ESI client.
    :param token: :class:`esi.Token` used to access authenticated endpoints.
    :param datasource: Name of the ESI datasource to access.
    :param spec_file: Absolute path to a swagger spec file to load.
    :param version: Base ESI API version. Accepted values are 'legacy', 'latest', 'dev', or 'vX' where X is a number.
    :param kwargs: Explicit resource versions to build, in the form Character='v4'. Same values accepted as version.
    :return: :class:`bravado.client.SwaggerClient`

    If a spec_file is specified, specific versioning is not available. Meaning the version and resource version kwargs
    are ignored in favour of the versions available in the spec_file.
    """

    client = requests_client.RequestsClient()
    if token or datasource:
        client.authenticator = TokenAuthenticator(token=token,
                                                  datasource=datasource)

    api_version = version or app_settings.ESI_API_VERSION

    if spec_file:
        return read_spec(spec_file, http_client=client)
    else:
        spec = build_spec(api_version, http_client=client, **kwargs)
        return SwaggerClient(spec)
Exemplo n.º 2
0
def modify_or_make_new(client, symbol, mbl, side, market_data):
    new_client = SwaggerClient(swagger_spec=client.swagger_spec,
                               also_return_response=True)

    for market in market_data:
        origin_order, origin_auth = mbl[side].pop(market["price"],
                                                  (None, None))

        if not origin_order:
            new_auth = AUTH_QUEUE.get()

            new_client.swagger_spec.http_client.authenticator = new_auth

            AUTH_QUEUE.put_nowait(new_auth)

            if market["size"] != 0:
                new_order, _ = new_client.Order.Order_new(
                    symbol=symbol,
                    side=side,
                    orderQty=scale_size(market["size"]),
                    price=market["price"]).result()

                mbl[side][market["price"]] = (new_order, new_auth)

            continue

        new_client.swagger_spec.http_client.authenticator = origin_auth

        if market["size"] == 0:
            try:
                new_client.Order.Order_cancel(
                    orderID=origin_order["orderID"]).result()
            except HTTPNotFound:
                pass
            except (SwaggerError, HTTPBadRequest) as e:
                handle_exception(e)
            finally:
                mbl[side].pop(origin_order["price"], None)

            continue

        if market["size"] != origin_order["orderQty"]:
            try:
                new_client.Order.Order_amend(orderID=origin_order["orderID"],
                                             orderQty=scale_size(
                                                 market["size"])).result()
            except (SwaggerError, HTTPBadRequest) as e:
                handle_exception(e)

                # 改单错误,此处认为对应OrderID的委托不存在
                mbl[side].pop(origin_order["price"], None)

                continue

            origin_order["ordQty"] = market["size"]

            mbl[side][market["price"]] = (origin_order, origin_auth)
Exemplo n.º 3
0
    def __init__(self, host="http://trade", config=None, size=200):
        self._pool_size = size

        self._instance_list = list()

        ins = nge(host=host, config=config)

        self._instance_list.append(ins)

        for _ in range(self._pool_size-1):
            new_instance = SwaggerClient(
                swagger_spec=ins.swagger_spec,
                also_return_response=config.get("also_return_response",
                                                True) if config else True)
            self._instance_list.append(new_instance)

        self._semaphore = BoundedSemaphore(self._pool_size)

        self._lock = Lock()

        self._idx = 0
Exemplo n.º 4
0
def esi_client_factory(
    token=None, datasource=None, spec_file=None, version=None, **kwargs
):
    """
    Generates an ESI client.
    :param token: :class:`esi.Token` used to access authenticated endpoints.
    :param datasource: Name of the ESI datasource to access.
    :param spec_file: Absolute path to a swagger spec file to load.
    :param version: Base ESI API version. Accepted values are 'legacy', 'latest', 
    'dev', or 'vX' where X is a number.
    :param kwargs: Explicit resource versions to build, in the form Character='v4'. 
    Same values accepted as version.
    :return: :class:`bravado.client.SwaggerClient`

    If a spec_file is specified, specific versioning is not available. 
    Meaning the version and resource version kwargs
    are ignored in favour of the versions available in the spec_file.
    """    
    if app_settings.ESI_INFO_LOGGING_ENABLED:
        logger.info('Generating an ESI client...')
    client = requests_client.RequestsClient()
    my_http_adapter = HTTPAdapter(
        pool_maxsize=app_settings.ESI_CONNECTION_POOL_MAXSIZE, 
        max_retries=app_settings.ESI_CONNECTION_ERROR_MAX_RETRIES
    )
    client.session.mount('https://', my_http_adapter)
    
    if token or datasource:
        client.authenticator = TokenAuthenticator(token=token, datasource=datasource)

    api_version = version or app_settings.ESI_API_VERSION

    if spec_file:
        return read_spec(spec_file, http_client=client)
    else:
        spec = build_spec(api_version, http_client=client, **kwargs)
        return SwaggerClient(spec)