Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auto release for 0.9.2 #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ This client can run on Linux, macOS and Windows.

- Website: https://www.ucloud.cn/
- Free software: Apache 2.0 license
- [Documentation](https://ucloud.github.io/ucloud-sdk-python2/)
- [Documentation](https://docs.ucloud.cn/opensdk-python/)
15 changes: 15 additions & 0 deletions examples/auth/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# UCloud SDK Auth Example

## What is the goal

Create signature from request payload.

## Setup Environment

Don't need.

## How to run

```sh
python main.py
```
1 change: 1 addition & 0 deletions examples/auth/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# -*- coding: utf-8 -*-
16 changes: 16 additions & 0 deletions examples/auth/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-

from ucloud.core import auth


def main():
cred = auth.Credential(
"[email protected]",
"46f09bb9fab4f12dfc160dae12273d5332b5debe",
)
d = {"Action": "DescribeUHostInstance", "Region": "cn-bj2", "Limit": 10}
print(cred.verify_ac(d))


if __name__ == "__main__":
main()
3 changes: 3 additions & 0 deletions tests/test_unit/test_core/consts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-

TEST_URL = "https://api.ucloud.cn/"
4 changes: 4 additions & 0 deletions tests/test_unit/test_core/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ def test_verify_ac():
"46f09bb9fab4f12dfc160dae12273d5332b5debe",
)
assert cred.verify_ac(d) == "4f9ef5df2abab2c6fccd1e9515cb7e2df8c6bb65"
assert cred.to_dict() == {
"public_key": "[email protected]",
"private_key": "46f09bb9fab4f12dfc160dae12273d5332b5debe",
}
96 changes: 60 additions & 36 deletions tests/test_unit/test_core/test_client.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
# -*- coding: utf-8 -*-

import os
import json
import uuid
import pytest
import logging
import collections
import requests_mock
from ucloud.client import Client
from ucloud.core import exc
from ucloud.core.transport import RequestsTransport, http
from ucloud.testing.mock import MockedTransport
from tests.test_unit.test_core import consts

logger = logging.getLogger(__name__)


@pytest.fixture(scope="session", autouse=True)
@pytest.fixture(scope="function", autouse=True)
def client():
return Client(
{
Expand All @@ -29,49 +34,68 @@ def transport():
return MockedTransport()


class TestClient(object):
def test_client_invoke(self, client, transport):
transport.mock_data(lambda _: {"RetCode": 0, "Action": "Foo"})
client.transport = transport
assert client.invoke("Foo") == {"RetCode": 0, "Action": "Foo"}
def test_client_invoke(client):
expected = {"RetCode": 0, "Action": "Foo"}
with requests_mock.Mocker() as m:
m.post(
consts.TEST_URL,
text=json.dumps(expected),
headers={http.REQUEST_UUID_HEADER_KEY: str(uuid.uuid4())},
)
assert client.invoke("Foo") == expected


def test_client_invoke_code_error(self, client, transport):
transport.mock_data(lambda _: {"RetCode": 171, "Action": "Foo"})
client.transport = transport
def test_client_invoke_code_error(client):
expected = {"RetCode": 171, "Action": "Foo", "Message": "签名错误"}
with requests_mock.Mocker() as m:
m.post(
consts.TEST_URL,
text=json.dumps(expected),
headers={http.REQUEST_UUID_HEADER_KEY: str(uuid.uuid4())},
)
with pytest.raises(exc.RetCodeException):
try:
client.invoke("Foo")
except exc.RetCodeException as e:
assert str(e)
expected = {"RetCode": 171, "Action": "Foo", "Message": ""}
assert e.json() == expected
assert e.retryable is False
assert e.json() == {
"RetCode": 171,
"Action": "Foo",
"Message": "签名错误",
}
raise e

def test_client_invoke_with_retryable_error(self, client, transport):
transport.mock_data(lambda _: {"RetCode": 10000, "Action": "Foo"})
client.transport = transport

def test_client_invoke_with_retryable_error(client):
with requests_mock.Mocker() as m:
m.post(
consts.TEST_URL,
text=json.dumps({"RetCode": 10000, "Action": "Foo"}),
)
with pytest.raises(exc.RetCodeException):
client.invoke("Foo")

def test_client_invoke_with_unexpected_error(self, client, transport):
def raise_error(_):
raise ValueError("temporary error")

transport.mock_data(raise_error)
client.transport = transport
with pytest.raises(ValueError):
client.invoke("Foo")
def test_client_invoke_with_unexpected_error(client):
def raise_error(_):
raise ValueError("temporary error")

transport = RequestsTransport()
transport.middleware.request(raise_error)
client.transport = transport
with pytest.raises(ValueError):
client.invoke("Foo")


def test_client_try_import(self, client):
assert client.pathx()
assert client.stepflow()
assert client.uaccount()
assert client.udb()
assert client.udpn()
assert client.udisk()
assert client.uhost()
assert client.ulb()
assert client.umem()
assert client.unet()
assert client.uphost()
assert client.vpc()
def test_client_try_import(client):
for name in dir(client):
if name.startswith("_") or name in [
"invoke",
"logged_request_handler",
"logged_response_handler",
"logged_exception_handler",
]:
continue
client_factory = getattr(client, name)
if isinstance(client_factory, collections.Callable):
print(client_factory())
163 changes: 110 additions & 53 deletions tests/test_unit/test_core/test_transport.py
Original file line number Diff line number Diff line change
@@ -1,72 +1,129 @@
# -*- coding: utf-8 -*-

import json
import uuid
import pytest
import logging
from ucloud.core.transport import RequestsTransport, Request, Response, utils
import requests_mock
from collections import Counter
from tests.test_unit.test_core.consts import TEST_URL
from ucloud.core import exc
from ucloud.core.transport import (
RequestsTransport,
Request,
Response,
utils,
http,
)

logger = logging.getLogger(__name__)


@pytest.fixture(scope="function", autouse=True)
def transport():
@pytest.fixture(name="transport", scope="function", autouse=True)
def transport_factory():
return RequestsTransport()


class TestTransport(object):
def test_transport_send(self, transport):
req = Request(
url="http://httpbin.org/anything",
method="post",
json={"foo": "bar"},
)
resp = transport.send(req)
assert resp.text
assert resp.json()["json"] == {"foo": "bar"}
@pytest.mark.parametrize(
argnames=("status_code", "content", "expect", "expect_exc", "retryable"),
argvalues=(
(
200,
'{"Action": "Mock", "RetCode": 0}',
{"Action": "Mock", "RetCode": 0},
None,
False,
),
(500, "{}", None, exc.HTTPStatusException, False),
(429, "{}", None, exc.HTTPStatusException, True),
(500, "x", None, exc.HTTPStatusException, False),
(200, "x", None, exc.InvalidResponseException, False),
),
)
def test_transport(
transport, status_code, content, expect, expect_exc, retryable
):
with requests_mock.Mocker() as m:
m.post(TEST_URL, text=content, status_code=status_code)
got_exc = None
try:
resp = transport.send(Request(url=TEST_URL, method="post", json={}))
assert resp.json() == expect
except Exception as e:
got_exc = e
if expect_exc:
assert str(got_exc)
assert got_exc.retryable == retryable
assert isinstance(got_exc, expect_exc)

def test_transport_handler(self, transport):
global_env = {}

def request_handler(r):
global_env["req"] = r
return r
def test_transport_handler(transport):
req_key, resp_key, exc_key = "req", "resp", "exc"
counter = Counter({req_key: 0, resp_key: 0, exc_key: 0})

def response_handler(r):
global_env["resp"] = r
return r
def request_handler(r):
counter[req_key] += 1
return r

transport.middleware.request(handler=request_handler)
transport.middleware.response(handler=response_handler)
req = Request(
url="http://httpbin.org/anything",
method="post",
json={"foo": "bar"},
def response_handler(r):
counter[resp_key] += 1
return r

def exception_handler(r):
counter[exc_key] += 1
return r

transport.middleware.request(handler=request_handler)
transport.middleware.response(handler=response_handler)
transport.middleware.exception(handler=exception_handler)
expect = {"foo": "bar"}
req = Request(url=TEST_URL, method="post", json=expect)
with requests_mock.Mocker() as m:
request_uuid = str(uuid.uuid4())
m.post(
TEST_URL,
text=json.dumps(expect),
status_code=200,
headers={http.REQUEST_UUID_HEADER_KEY: request_uuid},
)
resp = transport.send(req)
assert resp.text
assert resp.json()["json"] == {"foo": "bar"}
assert "req" in global_env
assert "resp" in global_env


class TestResponse(object):
def test_guess_json_utf(self):
import json

encodings = [
"utf-32",
"utf-8-sig",
"utf-16",
"utf-8",
"utf-16-be",
"utf-16-le",
"utf-32-be",
"utf-32-le",
]
for e in encodings:
s = json.dumps("表意字符").encode(e)
assert utils.guess_json_utf(s) == e

def test_response_empty_content(self):
r = Response("http://foo.bar", "post")
assert not r.text
assert resp.json() == expect
assert resp.request_uuid == request_uuid
with pytest.raises(Exception):
transport.send(Request(url="/"))
assert counter[req_key] == 2
assert counter[resp_key] == 1
assert counter[exc_key] == 1


def test_guess_json_utf():
encodings = [
"utf-32",
"utf-8-sig",
"utf-16",
"utf-8",
"utf-16-be",
"utf-16-le",
"utf-32-be",
"utf-32-le",
]
for e in encodings:
s = json.dumps("表意字符").encode(e)
assert utils.guess_json_utf(s) == e


def test_request_methods():
req = Request(
TEST_URL, data={"foo": 42}, json={"bar": 42}, params={"q": "search"}
)
assert req.payload() == {"foo": 42, "bar": 42, "q": "search"}


def test_response_methods():
r = Response(TEST_URL, "post")
assert not r.text
assert r.json() is None
r = Response(TEST_URL, "post", content=b"\xd6", encoding="utf-8")
with pytest.raises(exc.InvalidResponseException):
assert r.json() is None
8 changes: 4 additions & 4 deletions ucloud/client.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# -*- coding: utf-8 -*-

""" Code is generated by ucloud-model, DO NOT EDIT IT. """
from ucloud._compat import CompactClient
from ucloud.core import client


class Client(CompactClient):
def __init__(self, config, transport=None, middleware=None):
class Client(client.Client):
def __init__(self, config, transport=None, middleware=None, logger=None):
self._config = config
super(Client, self).__init__(config, transport, middleware)
super(Client, self).__init__(config, transport, middleware, logger)

def pathx(self):
from ucloud.services.pathx.client import PathXClient
Expand Down
Loading