Skip to content

Commit 3000ae3

Browse files
authored
Merge pull request #498 from ydb-platform/fix_driver_and_driver_config_collision
Fix StaticCredentials and DriverConfig collision
2 parents ede35b0 + eaf6807 commit 3000ae3

File tree

9 files changed

+410
-161
lines changed

9 files changed

+410
-161
lines changed

.github/workflows/docs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ jobs:
1515
id-token: write
1616
steps:
1717
- id: deployment
18-
uses: sphinx-notes/pages@v3
18+
uses: sphinx-notes/pages@v3

docs/examples.rst

Lines changed: 4 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -1,155 +1,8 @@
11
Examples
22
===============
33

4-
Basic example
5-
^^^^^^^^^^^^^
6-
7-
All examples in this section are parts of `basic example <https://github.com/ydb-platform/ydb-python-sdk/tree/main/examples/basic_example_v2>`_.
8-
9-
For deeper upderstanding it is better to read the whole example.
10-
11-
Create table
12-
------------
13-
14-
.. code-block:: python
15-
16-
def create_tables(pool: ydb.QuerySessionPool):
17-
print("\nCreating table series...")
18-
pool.execute_with_retries(
19-
"""
20-
CREATE table `series` (
21-
`series_id` Int64,
22-
`title` Utf8,
23-
`series_info` Utf8,
24-
`release_date` Date,
25-
PRIMARY KEY (`series_id`)
26-
)
27-
"""
28-
)
29-
30-
print("\nCreating table seasons...")
31-
pool.execute_with_retries(
32-
"""
33-
CREATE table `seasons` (
34-
`series_id` Int64,
35-
`season_id` Int64,
36-
`title` Utf8,
37-
`first_aired` Date,
38-
`last_aired` Date,
39-
PRIMARY KEY (`series_id`, `season_id`)
40-
)
41-
"""
42-
)
43-
44-
print("\nCreating table episodes...")
45-
pool.execute_with_retries(
46-
"""
47-
CREATE table `episodes` (
48-
`series_id` Int64,
49-
`season_id` Int64,
50-
`episode_id` Int64,
51-
`title` Utf8,
52-
`air_date` Date,
53-
PRIMARY KEY (`series_id`, `season_id`, `episode_id`)
54-
)
55-
"""
56-
)
57-
58-
59-
Upsert Simple
60-
-------------
61-
62-
.. code-block:: python
63-
64-
def upsert_simple(pool: ydb.QuerySessionPool):
65-
print("\nPerforming UPSERT into episodes...")
66-
67-
pool.execute_with_retries(
68-
"""
69-
UPSERT INTO episodes (series_id, season_id, episode_id, title) VALUES (2, 6, 1, "TBD");
70-
"""
71-
)
72-
73-
74-
Simple Select
75-
----------
76-
77-
.. code-block:: python
78-
79-
def select_simple(pool: ydb.QuerySessionPool):
80-
print("\nCheck series table...")
81-
result_sets = pool.execute_with_retries(
82-
"""
83-
SELECT
84-
series_id,
85-
title,
86-
release_date
87-
FROM series
88-
WHERE series_id = 1;
89-
""",
90-
)
91-
first_set = result_sets[0]
92-
for row in first_set.rows:
93-
print(
94-
"series, id: ",
95-
row.series_id,
96-
", title: ",
97-
row.title,
98-
", release date: ",
99-
row.release_date,
100-
)
101-
102-
return first_set
103-
104-
Select With Parameters
105-
----------------------
106-
107-
.. code-block:: python
108-
109-
def select_with_parameters(pool: ydb.QuerySessionPool, series_id, season_id, episode_id):
110-
result_sets = pool.execute_with_retries(
111-
"""
112-
DECLARE $seriesId AS Int64;
113-
DECLARE $seasonId AS Int64;
114-
DECLARE $episodeId AS Int64;
115-
116-
SELECT
117-
title,
118-
air_date
119-
FROM episodes
120-
WHERE series_id = $seriesId AND season_id = $seasonId AND episode_id = $episodeId;
121-
""",
122-
{
123-
"$seriesId": series_id, # could be defined implicit
124-
"$seasonId": (season_id, ydb.PrimitiveType.Int64), # could be defined via tuple
125-
"$episodeId": ydb.TypedValue(episode_id, ydb.PrimitiveType.Int64), # could be defined via special class
126-
},
127-
)
128-
129-
print("\n> select_with_parameters:")
130-
first_set = result_sets[0]
131-
for row in first_set.rows:
132-
print("episode title:", row.title, ", air date:", row.air_date)
133-
134-
return first_set
135-
136-
Huge Select
137-
-----------
138-
139-
.. code-block:: python
140-
141-
def huge_select(pool: ydb.QuerySessionPool):
142-
def callee(session: ydb.QuerySessionSync):
143-
query = """SELECT * from episodes;"""
144-
145-
with session.transaction().execute(
146-
query,
147-
commit_tx=True,
148-
) as result_sets:
149-
print("\n> Huge SELECT call")
150-
for result_set in result_sets:
151-
for row in result_set.rows:
152-
print("episode title:", row.title, ", air date:", row.air_date)
153-
154-
return pool.retry_operation_sync(callee)
4+
.. toctree::
5+
:maxdepth: 3
1556

7+
examples/basic_example
8+
examples/authentication

docs/examples/authentication.rst

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
Authentication
2+
==============
3+
4+
There are several ways to authenticate through YDB Python SDK.
5+
6+
Anonymous Credentials
7+
---------------------
8+
9+
Full executable example `here <https://github.com/ydb-platform/ydb-python-sdk/tree/main/examples/anonymous-credentials>`_.
10+
11+
.. code-block:: python
12+
13+
driver = ydb.Driver(
14+
endpoint=os.getenv("YDB_ENDPOINT"),
15+
database=os.getenv("YDB_DATABASE"),
16+
credentials=ydb.AnonymousCredentials(),
17+
)
18+
19+
20+
Access Token Credentials
21+
------------------------
22+
23+
Full executable example `here <https://github.com/ydb-platform/ydb-python-sdk/tree/main/examples/access-token-credentials>`_.
24+
25+
.. code-block:: python
26+
27+
driver = ydb.Driver(
28+
endpoint=os.getenv("YDB_ENDPOINT"),
29+
database=os.getenv("YDB_DATABASE"),
30+
credentials=ydb.AccessTokenCredentials(os.getenv("YDB_ACCESS_TOKEN_CREDENTIALS")),
31+
)
32+
33+
34+
Static Credentials
35+
---------------------------
36+
37+
Full executable example `here <https://github.com/ydb-platform/ydb-python-sdk/tree/main/examples/static-credentials>`_.
38+
39+
40+
.. code-block:: python
41+
42+
driver_config = ydb.DriverConfig(
43+
endpoint=endpoint,
44+
database=database,
45+
credentials=ydb.StaticCredentials.from_user_password(user, password),
46+
)
47+
48+
driver = ydb.Driver(driver_config=driver_config)
49+
50+
51+
Service Account Credentials
52+
----------------------------
53+
54+
Full executable example `here <https://github.com/ydb-platform/ydb-python-sdk/tree/main/examples/service-account-credentials>`_.
55+
56+
.. code-block:: python
57+
58+
driver = ydb.Driver(
59+
endpoint=os.getenv("YDB_ENDPOINT"),
60+
database=os.getenv("YDB_DATABASE"),
61+
credentials=ydb.iam.ServiceAccountCredentials.from_file(
62+
os.getenv("SA_KEY_FILE"),
63+
),
64+
)
65+
66+
67+
OAuth 2.0 Token Exchange Credentials
68+
------------------------------------
69+
70+
Full executable example `here <https://github.com/ydb-platform/ydb-python-sdk/tree/main/examples/oauth2-token-exchange-credentials>`_.
71+
72+
.. code-block:: python
73+
74+
driver = ydb.Driver(
75+
endpoint=args.endpoint,
76+
database=args.database,
77+
root_certificates=ydb.load_ydb_root_certificate(),
78+
credentials=ydb.oauth2_token_exchange.Oauth2TokenExchangeCredentials(
79+
token_endpoint=args.token_endpoint,
80+
audience=args.audience,
81+
subject_token_source=ydb.oauth2_token_exchange.JwtTokenSource(
82+
signing_method="RS256",
83+
private_key_file=args.private_key_file,
84+
key_id=args.key_id,
85+
issuer=args.issuer,
86+
subject=args.subject,
87+
audience=args.audience,
88+
),
89+
),
90+
)
91+
92+
93+
Metadata Credentials
94+
--------------------
95+
96+
Full executable example `here <https://github.com/ydb-platform/ydb-python-sdk/tree/main/examples/metadata-credentials>`_.
97+
98+
.. code-block:: python
99+
100+
driver = ydb.Driver(
101+
endpoint=os.getenv("YDB_ENDPOINT"),
102+
database=os.getenv("YDB_DATABASE"),
103+
credentials=ydb.iam.MetadataUrlCredentials(),
104+
)

0 commit comments

Comments
 (0)