пакетный HTTP-запрос Python по запросам - PullRequest
0 голосов
/ 26 апреля 2018

Я пытаюсь сделать пакетный запрос к API-интерфейсу channeladvisor на основе этого примера:

https://developer.channeladvisor.com/batch-requests/update-quantity-on-three-items-using-the-unshipped-update-type

Мой запрос выглядит так:


    POST https://api.channeladvisor.com/v1/$batch?access_token=xxxxxx
    Content-Length: 1307
    Content-Type: multipart/mixed; boundary=938fabdd69a541beb904b703514f8bc3

    --batch
    Content-Type: multipart/mixed; boundary=938fabdd69a541beb904b703514f8bc3
    --938fabdd69a541beb904b703514f8bc3
    Content-Type: application/http
    Content-Transfer-Encoding: binary
    Content-ID: 1

    POST https://api.channeladvisor.com/V1/Products(227657)/UpdateQuantity HTTP/HTTPS 1.1
    Content-Type: application/json

    {"Value": {"UpdateType": "UnShipped", "Updates": [{"DistributionCenterID": 1, "Quantity": 2}]}}
    --938fabdd69a541beb904b703514f8bc3
    Content-Type: application/http
    Content-Transfer-Encoding: binary
    Content-ID: 2

    POST https://api.channeladvisor.com/V1/Products(227658)/UpdateQuantity HTTP/HTTPS 1.1
    Content-Type: application/json

    {"Value": {"UpdateType": "UnShipped", "Updates": [{"DistributionCenterID": 1, "Quantity": 2}]}}
    --938fabdd69a541beb904b703514f8bc3
    Content-Type: application/http
    Content-Transfer-Encoding: binary
    Content-ID: 3

    POST https://api.channeladvisor.com/V1/Products(227659)/UpdateQuantity HTTP/HTTPS 1.1
    Content-Type: application/json

    {"Value": {"UpdateType": "UnShipped", "Updates": [{"DistributionCenterID": 1, "Quantity": 2}]}}
    --938fabdd69a541beb904b703514f8bc3--
    --batch

Я использую библиотеку запросов Python и небольшой хак для достижения этой цели. При использовании Почтальона это работает нормально. Я получил 204 статуса.

Когда я пробую это с python:

data = {
  'Value':{
    'UpdateType': 'UnShipped',
    'Updates': [{
        'DistributionCenterID': 1,
        'Quantity': 2
    }]
  }
}
headers = {
    'Content-Type': 'application/json'
}
commands = []
commands.append(requests.Request('POST', 'https://api.channeladvisor.com/V1/Products(227657)/UpdateQuantity HTTP/1.1', json=data, headers=headers))
commands.append(requests.Request('POST', 'https://api.channeladvisor.com/V1/Products(227658)/UpdateQuantity HTTP/1.1', json=data, headers=headers))
commands.append(requests.Request('POST', 'https://api.channeladvisor.com/V1/Products(227659)/UpdateQuantity HTTP/1.1', json=data, headers=headers))
#-----------------------------------------------#


batch = BatchRequest()
files = batch.prepare_requests(commands)
r = requests.Request('POST', 'https://api.channeladvisor.com/v1/$batch?access_token='+self.access_token, files=files)
prepared = r.prepare()
prepared = batch.finalize_request(prepared)

batch.pretty_print_POST(prepared)
s = requests.Session()
resp = s.send(prepared)

Я получаю это:


    Traceback (most recent call last):
      File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 601, in urlopen
        chunked=chunked)
      File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 387, in _make_request
        six.raise_from(e, None)
      File "", line 2, in raise_from
      File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 383, in _make_request
        httplib_response = conn.getresponse()
      File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1331, in getresponse
        response.begin()
      File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 297, in begin
        version, status, reason = self._read_status()
      File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 258, in _read_status
        line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
      File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py", line 586, in readinto
        return self._sock.recv_into(b)
      File "/usr/local/lib/python3.6/site-packages/urllib3/contrib/pyopenssl.py", line 285, in recv_into
        raise SocketError(str(e))
    OSError: (54, 'ECONNRESET')

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "/usr/local/lib/python3.6/site-packages/requests/adapters.py", line 440, in send
        timeout=timeout
      File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 639, in urlopen
        _stacktrace=sys.exc_info()[2])
      File "/usr/local/lib/python3.6/site-packages/urllib3/util/retry.py", line 357, in increment
        raise six.reraise(type(error), error, _stacktrace)
      File "/usr/local/lib/python3.6/site-packages/urllib3/packages/six.py", line 685, in reraise
        raise value.with_traceback(tb)
      File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 601, in urlopen
        chunked=chunked)
      File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 387, in _make_request
        six.raise_from(e, None)
      File "", line 2, in raise_from
      File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 383, in _make_request
        httplib_response = conn.getresponse()
      File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1331, in getresponse
        response.begin()
      File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 297, in begin
        version, status, reason = self._read_status()
      File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 258, in _read_status
        line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
      File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py", line 586, in readinto
        return self._sock.recv_into(b)
      File "/usr/local/lib/python3.6/site-packages/urllib3/contrib/pyopenssl.py", line 285, in recv_into
        raise SocketError(str(e))
    urllib3.exceptions.ProtocolError: ('Connection aborted.', OSError("(54, 'ECONNRESET')",))

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "channel.py", line 157, in 
        products.batchStockUpload()
      File "channel.py", line 146, in batchStockUpload
        resp = s.send(prepared, verify=True) #verify='/usr/local/Cellar/openssl/1.0.2o_1'
      File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 618, in send
        r = adapter.send(request, **kwargs)
      File "/usr/local/lib/python3.6/site-packages/requests/adapters.py", line 490, in send
        raise ConnectionError(err, request=request)
    requests.exceptions.ConnectionError: ('Connection aborted.', OSError("(54, 'ECONNRESET')",))

Я перепробовал все и действительно отчаянно задал этот вопрос? Спасибо за помощь

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...