Skip to content

Commit b612533

Browse files
authored
Merge pull request #53 from livechat/AddLoggingToWeb
Add logging to web
2 parents 45c8d8c + 4ccb297 commit b612533

File tree

10 files changed

+159
-39
lines changed

10 files changed

+159
-39
lines changed

changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# Change Log
22
All notable changes to this project will be documented in this file.
33

4+
## [0.1.8] - 2021-10-21
5+
6+
### Added
7+
8+
- Added logging for web interface
9+
410
## [0.1.7] - 2021-10-13
511

612
### Changed

docs/agent_web.html

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ <h1 class="title">Module <code>client</code></h1>
3030
<pre><code class="python">&#39;&#39;&#39; Agent Web client implementation. &#39;&#39;&#39;
3131

3232
# pylint: disable=W0613,R0913,W0622,C0103,W0221
33+
from __future__ import annotations
3334

3435
import typing
3536
from abc import ABCMeta
3637

3738
import httpx
3839

3940
from livechat.utils.helpers import prepare_payload
41+
from livechat.utils.httpx_logger import HttpxLogger
4042

4143

4244
# pylint: disable=R0903
@@ -47,7 +49,7 @@ <h1 class="title">Module <code>client</code></h1>
4749
def get_client(access_token: str,
4850
version: str = &#39;3.3&#39;,
4951
base_url: str = &#39;api.livechatinc.com&#39;,
50-
http2: bool = False):
52+
http2: bool = False) -&gt; AgentWebInterface:
5153
&#39;&#39;&#39; Returns client for specific API version.
5254

5355
Args:
@@ -77,10 +79,15 @@ <h1 class="title">Module <code>client</code></h1>
7779
class AgentWebInterface(metaclass=ABCMeta):
7880
&#39;&#39;&#39; Main class containing API methods. &#39;&#39;&#39;
7981
def __init__(self, access_token: str, version: str, base_url: str,
80-
http2: bool):
82+
http2: bool) -&gt; AgentWebInterface:
83+
logger = HttpxLogger()
8184
self.api_url = f&#39;https://{base_url}/v{version}/agent/action&#39;
8285
self.session = httpx.Client(http2=http2,
83-
headers={&#39;Authorization&#39;: access_token})
86+
headers={&#39;Authorization&#39;: access_token},
87+
event_hooks={
88+
&#39;request&#39;: [logger.log_request],
89+
&#39;response&#39;: [logger.log_response]
90+
})
8491

8592
def modify_header(self, header: dict) -&gt; None:
8693
&#39;&#39;&#39; Modifies provided header in session object.
@@ -1395,7 +1402,7 @@ <h2 class="section-title" id="header-classes">Classes</h2>
13951402
def get_client(access_token: str,
13961403
version: str = &#39;3.3&#39;,
13971404
base_url: str = &#39;api.livechatinc.com&#39;,
1398-
http2: bool = False):
1405+
http2: bool = False) -&gt; AgentWebInterface:
13991406
&#39;&#39;&#39; Returns client for specific API version.
14001407

14011408
Args:
@@ -1424,7 +1431,7 @@ <h2 class="section-title" id="header-classes">Classes</h2>
14241431
<h3>Static methods</h3>
14251432
<dl>
14261433
<dt id="client.AgentWeb.get_client"><code class="name flex">
1427-
<span>def <span class="ident">get_client</span></span>(<span>access_token: str, version: str = '3.3', base_url: str = 'api.livechatinc.com', http2: bool = False)</span>
1434+
<span>def <span class="ident">get_client</span></span>(<span>access_token: str, version: str = '3.3', base_url: str = 'api.livechatinc.com', http2: bool = False)> <a title="client.AgentWebInterface" href="#client.AgentWebInterface">AgentWebInterface</a></span>
14281435
</code></dt>
14291436
<dd>
14301437
<div class="desc"><p>Returns client for specific API version.</p>
@@ -1457,7 +1464,7 @@ <h2 id="raises">Raises</h2>
14571464
def get_client(access_token: str,
14581465
version: str = &#39;3.3&#39;,
14591466
base_url: str = &#39;api.livechatinc.com&#39;,
1460-
http2: bool = False):
1467+
http2: bool = False) -&gt; AgentWebInterface:
14611468
&#39;&#39;&#39; Returns client for specific API version.
14621469

14631470
Args:
@@ -2283,10 +2290,15 @@ <h3>Inherited members</h3>
22832290
<pre><code class="python">class AgentWebInterface(metaclass=ABCMeta):
22842291
&#39;&#39;&#39; Main class containing API methods. &#39;&#39;&#39;
22852292
def __init__(self, access_token: str, version: str, base_url: str,
2286-
http2: bool):
2293+
http2: bool) -&gt; AgentWebInterface:
2294+
logger = HttpxLogger()
22872295
self.api_url = f&#39;https://{base_url}/v{version}/agent/action&#39;
22882296
self.session = httpx.Client(http2=http2,
2289-
headers={&#39;Authorization&#39;: access_token})
2297+
headers={&#39;Authorization&#39;: access_token},
2298+
event_hooks={
2299+
&#39;request&#39;: [logger.log_request],
2300+
&#39;response&#39;: [logger.log_response]
2301+
})
22902302

22912303
def modify_header(self, header: dict) -&gt; None:
22922304
&#39;&#39;&#39; Modifies provided header in session object.
@@ -4512,7 +4524,7 @@ <h2 id="args">Args</h2>
45124524
</details>
45134525
</dd>
45144526
<dt id="client.AgentWebInterface.multicast"><code class="name flex">
4515-
<span>def <span class="ident">multicast</span></span>(<span>self, recipients: dict = None, content: Any = None, type: str = None, payload: dict = None, headers: dict = None) ‑> httpx.Response</span>
4527+
<span>def <span class="ident">multicast</span></span>(<span>self, recipients: dict = None, content: typing.Any = None, type: str = None, payload: dict = None, headers: dict = None) ‑> httpx.Response</span>
45164528
</code></dt>
45174529
<dd>
45184530
<div class="desc"><p>Sends a multicast (chat-unrelated communication).</p>
@@ -5569,7 +5581,7 @@ <h2 id="returns">Returns</h2>
55695581
</details>
55705582
</dd>
55715583
<dt id="client.AgentWebInterface.upload_file"><code class="name flex">
5572-
<span>def <span class="ident">upload_file</span></span>(<span>self, file: <class 'BinaryIO'> = None, payload: dict = None, headers: dict = None) ‑> httpx.Response</span>
5584+
<span>def <span class="ident">upload_file</span></span>(<span>self, file: typing.BinaryIO = None, payload: dict = None, headers: dict = None) ‑> httpx.Response</span>
55735585
</code></dt>
55745586
<dd>
55755587
<div class="desc"><p>Uploads a file to the server as a temporary file. It returns a URL that expires

docs/configuration_api.html

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ <h1 class="title">Module <code>client</code></h1>
3030
<pre><code class="python">&#39;&#39;&#39; Configuration API client implementation. &#39;&#39;&#39;
3131

3232
# pylint: disable=W0613,W0622,C0103,R0913,R0903
33+
from __future__ import annotations
3334

3435
from abc import ABCMeta
3536

3637
import httpx
3738

3839
from livechat.utils.helpers import prepare_payload
40+
from livechat.utils.httpx_logger import HttpxLogger
3941

4042

4143
class ConfigurationApi:
@@ -45,7 +47,7 @@ <h1 class="title">Module <code>client</code></h1>
4547
def get_client(token: str,
4648
version: str = &#39;3.3&#39;,
4749
base_url: str = &#39;api.livechatinc.com&#39;,
48-
http2: bool = False):
50+
http2: bool = False) -&gt; ConfigurationApiInterface:
4951
&#39;&#39;&#39; Returns client for specific Configuration API version.
5052

5153
Args:
@@ -73,10 +75,16 @@ <h1 class="title">Module <code>client</code></h1>
7375

7476
class ConfigurationApiInterface(metaclass=ABCMeta):
7577
&#39;&#39;&#39; Interface class. &#39;&#39;&#39;
76-
def __init__(self, token: str, version: str, base_url: str, http2: bool):
78+
def __init__(self, token: str, version: str, base_url: str,
79+
http2: bool) -&gt; ConfigurationApiInterface:
80+
logger = HttpxLogger()
7781
self.api_url = f&#39;https://{base_url}/v{version}/configuration/action&#39;
7882
self.session = httpx.Client(http2=http2,
79-
headers={&#39;Authorization&#39;: token})
83+
headers={&#39;Authorization&#39;: token},
84+
event_hooks={
85+
&#39;request&#39;: [logger.log_request],
86+
&#39;response&#39;: [logger.log_response]
87+
})
8088

8189
def modify_header(self, header: dict) -&gt; None:
8290
&#39;&#39;&#39; Modifies provided header in session object.
@@ -1261,7 +1269,7 @@ <h2 class="section-title" id="header-classes">Classes</h2>
12611269
def get_client(token: str,
12621270
version: str = &#39;3.3&#39;,
12631271
base_url: str = &#39;api.livechatinc.com&#39;,
1264-
http2: bool = False):
1272+
http2: bool = False) -&gt; ConfigurationApiInterface:
12651273
&#39;&#39;&#39; Returns client for specific Configuration API version.
12661274

12671275
Args:
@@ -1289,7 +1297,7 @@ <h2 class="section-title" id="header-classes">Classes</h2>
12891297
<h3>Static methods</h3>
12901298
<dl>
12911299
<dt id="client.ConfigurationApi.get_client"><code class="name flex">
1292-
<span>def <span class="ident">get_client</span></span>(<span>token: str, version: str = '3.3', base_url: str = 'api.livechatinc.com', http2: bool = False)</span>
1300+
<span>def <span class="ident">get_client</span></span>(<span>token: str, version: str = '3.3', base_url: str = 'api.livechatinc.com', http2: bool = False)> <a title="client.ConfigurationApiInterface" href="#client.ConfigurationApiInterface">ConfigurationApiInterface</a></span>
12931301
</code></dt>
12941302
<dd>
12951303
<div class="desc"><p>Returns client for specific Configuration API version.</p>
@@ -1324,7 +1332,7 @@ <h2 id="raises">Raises</h2>
13241332
def get_client(token: str,
13251333
version: str = &#39;3.3&#39;,
13261334
base_url: str = &#39;api.livechatinc.com&#39;,
1327-
http2: bool = False):
1335+
http2: bool = False) -&gt; ConfigurationApiInterface:
13281336
&#39;&#39;&#39; Returns client for specific Configuration API version.
13291337

13301338
Args:
@@ -1500,10 +1508,16 @@ <h3>Inherited members</h3>
15001508
</summary>
15011509
<pre><code class="python">class ConfigurationApiInterface(metaclass=ABCMeta):
15021510
&#39;&#39;&#39; Interface class. &#39;&#39;&#39;
1503-
def __init__(self, token: str, version: str, base_url: str, http2: bool):
1511+
def __init__(self, token: str, version: str, base_url: str,
1512+
http2: bool) -&gt; ConfigurationApiInterface:
1513+
logger = HttpxLogger()
15041514
self.api_url = f&#39;https://{base_url}/v{version}/configuration/action&#39;
15051515
self.session = httpx.Client(http2=http2,
1506-
headers={&#39;Authorization&#39;: token})
1516+
headers={&#39;Authorization&#39;: token},
1517+
event_hooks={
1518+
&#39;request&#39;: [logger.log_request],
1519+
&#39;response&#39;: [logger.log_response]
1520+
})
15071521

15081522
def modify_header(self, header: dict) -&gt; None:
15091523
&#39;&#39;&#39; Modifies provided header in session object.

docs/customer_web.html

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ <h1 class="title">Module <code>client</code></h1>
3838
import httpx
3939

4040
from livechat.utils.helpers import prepare_payload
41+
from livechat.utils.httpx_logger import HttpxLogger
4142

4243

4344
# pylint: disable=R0903
@@ -96,10 +97,16 @@ <h1 class="title">Module <code>client</code></h1>
9697
&#39;&#39;&#39; Main class containing API methods. &#39;&#39;&#39;
9798
def __init__(self, access_token: str, version: str, base_url: str,
9899
http2: bool) -&gt; CustomerWebInterface:
100+
logger = HttpxLogger()
99101
self.api_url = f&#39;https://{base_url}/v{version}/customer/action&#39;
100102
if all([access_token, isinstance(access_token, str)]):
101103
self.session = httpx.Client(
102-
http2=http2, headers={&#39;Authorization&#39;: access_token})
104+
http2=http2,
105+
headers={&#39;Authorization&#39;: access_token},
106+
event_hooks={
107+
&#39;request&#39;: [logger.log_request],
108+
&#39;response&#39;: [logger.log_response]
109+
})
103110
else:
104111
raise ValueError(
105112
&#39;Incorrect or missing `access_token` argument (should be of type str.)&#39;
@@ -1057,7 +1064,8 @@ <h1 class="title">Module <code>client</code></h1>
10571064
self.query_string = f&#39;?license_id={str(license_id)}&#39;
10581065
else:
10591066
raise ValueError(
1060-
&#39;Incorrect or missing `license_id` argument (should be of type int.)&#39;)
1067+
&#39;Incorrect or missing `license_id` argument (should be of type int.)&#39;
1068+
)
10611069

10621070
def list_license_properties(self,
10631071
namespace: str = None,
@@ -1132,7 +1140,7 @@ <h1 class="title">Module <code>client</code></h1>
11321140
super().__init__(access_token, version, url, http2)
11331141
if isinstance(organization_id, str):
11341142
self.organization_id = organization_id
1135-
self.query_string = f&#39;?organization_id={str(organization_id)}&#39;
1143+
self.query_string = f&#39;?organization_id={organization_id}&#39;
11361144
else:
11371145
raise ValueError(
11381146
&#39;Incorrect or missing `organization_id` argument (should be of type str.)&#39;
@@ -1379,7 +1387,8 @@ <h2 id="raises">Raises</h2>
13791387
self.query_string = f&#39;?license_id={str(license_id)}&#39;
13801388
else:
13811389
raise ValueError(
1382-
&#39;Incorrect or missing `license_id` argument (should be of type int.)&#39;)
1390+
&#39;Incorrect or missing `license_id` argument (should be of type int.)&#39;
1391+
)
13831392

13841393
def list_license_properties(self,
13851394
namespace: str = None,
@@ -1510,7 +1519,7 @@ <h3>Inherited members</h3>
15101519
super().__init__(access_token, version, url, http2)
15111520
if isinstance(organization_id, str):
15121521
self.organization_id = organization_id
1513-
self.query_string = f&#39;?organization_id={str(organization_id)}&#39;
1522+
self.query_string = f&#39;?organization_id={organization_id}&#39;
15141523
else:
15151524
raise ValueError(
15161525
&#39;Incorrect or missing `organization_id` argument (should be of type str.)&#39;
@@ -1642,10 +1651,16 @@ <h3>Inherited members</h3>
16421651
&#39;&#39;&#39; Main class containing API methods. &#39;&#39;&#39;
16431652
def __init__(self, access_token: str, version: str, base_url: str,
16441653
http2: bool) -&gt; CustomerWebInterface:
1654+
logger = HttpxLogger()
16451655
self.api_url = f&#39;https://{base_url}/v{version}/customer/action&#39;
16461656
if all([access_token, isinstance(access_token, str)]):
16471657
self.session = httpx.Client(
1648-
http2=http2, headers={&#39;Authorization&#39;: access_token})
1658+
http2=http2,
1659+
headers={&#39;Authorization&#39;: access_token},
1660+
event_hooks={
1661+
&#39;request&#39;: [logger.log_request],
1662+
&#39;response&#39;: [logger.log_response]
1663+
})
16491664
else:
16501665
raise ValueError(
16511666
&#39;Incorrect or missing `access_token` argument (should be of type str.)&#39;

docs/reports_api.html

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ <h1 class="title">Module <code>client</code></h1>
3838
import httpx
3939

4040
from livechat.utils.helpers import prepare_payload
41+
from livechat.utils.httpx_logger import HttpxLogger
4142

4243

4344
class ReportsApi:
@@ -77,9 +78,14 @@ <h1 class="title">Module <code>client</code></h1>
7778
&#39;&#39;&#39; Interface class. &#39;&#39;&#39;
7879
def __init__(self, token: str, version: str, base_url: str,
7980
http2: bool) -&gt; ReportsApiInterface:
81+
logger = HttpxLogger()
8082
self.api_url = f&#39;https://{base_url}/v{version}/reports&#39;
8183
self.session = httpx.Client(http2=http2,
82-
headers={&#39;Authorization&#39;: token})
84+
headers={&#39;Authorization&#39;: token},
85+
event_hooks={
86+
&#39;request&#39;: [logger.log_request],
87+
&#39;response&#39;: [logger.log_response]
88+
})
8389

8490
def modify_header(self, header: dict) -&gt; None:
8591
&#39;&#39;&#39; Modifies provided header in session object.
@@ -156,9 +162,10 @@ <h1 class="title">Module <code>client</code></h1>
156162
payload[&#39;from&#39;] = date_from
157163
if date_to is not None:
158164
payload[&#39;to&#39;] = date_to
159-
return self.session.get(f&#39;{self.api_url}/chats/agents_chatting_duration&#39;,
160-
params=payload,
161-
headers=headers)
165+
return self.session.get(
166+
f&#39;{self.api_url}/chats/agents_chatting_duration&#39;,
167+
params=payload,
168+
headers=headers)
162169

163170
def tags(self,
164171
date_to: str = None,
@@ -544,9 +551,10 @@ <h2 class="section-title" id="header-classes">Classes</h2>
544551
payload[&#39;from&#39;] = date_from
545552
if date_to is not None:
546553
payload[&#39;to&#39;] = date_to
547-
return self.session.get(f&#39;{self.api_url}/chats/agents_chatting_duration&#39;,
548-
params=payload,
549-
headers=headers)
554+
return self.session.get(
555+
f&#39;{self.api_url}/chats/agents_chatting_duration&#39;,
556+
params=payload,
557+
headers=headers)
550558

551559
def tags(self,
552560
date_to: str = None,
@@ -744,9 +752,10 @@ <h2 id="returns">Returns</h2>
744752
payload[&#39;from&#39;] = date_from
745753
if date_to is not None:
746754
payload[&#39;to&#39;] = date_to
747-
return self.session.get(f&#39;{self.api_url}/chats/agents_chatting_duration&#39;,
748-
params=payload,
749-
headers=headers)</code></pre>
755+
return self.session.get(
756+
f&#39;{self.api_url}/chats/agents_chatting_duration&#39;,
757+
params=payload,
758+
headers=headers)</code></pre>
750759
</details>
751760
</dd>
752761
<dt id="client.ReportsAPI33.tags"><code class="name flex">
@@ -1769,9 +1778,14 @@ <h2 id="raises">Raises</h2>
17691778
&#39;&#39;&#39; Interface class. &#39;&#39;&#39;
17701779
def __init__(self, token: str, version: str, base_url: str,
17711780
http2: bool) -&gt; ReportsApiInterface:
1781+
logger = HttpxLogger()
17721782
self.api_url = f&#39;https://{base_url}/v{version}/reports&#39;
17731783
self.session = httpx.Client(http2=http2,
1774-
headers={&#39;Authorization&#39;: token})
1784+
headers={&#39;Authorization&#39;: token},
1785+
event_hooks={
1786+
&#39;request&#39;: [logger.log_request],
1787+
&#39;response&#39;: [logger.log_response]
1788+
})
17751789

17761790
def modify_header(self, header: dict) -&gt; None:
17771791
&#39;&#39;&#39; Modifies provided header in session object.

livechat/agent/web/client.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import httpx
1010

1111
from livechat.utils.helpers import prepare_payload
12+
from livechat.utils.httpx_logger import HttpxLogger
1213

1314

1415
# pylint: disable=R0903
@@ -50,9 +51,14 @@ class AgentWebInterface(metaclass=ABCMeta):
5051
''' Main class containing API methods. '''
5152
def __init__(self, access_token: str, version: str, base_url: str,
5253
http2: bool) -> AgentWebInterface:
54+
logger = HttpxLogger()
5355
self.api_url = f'https://{base_url}/v{version}/agent/action'
5456
self.session = httpx.Client(http2=http2,
55-
headers={'Authorization': access_token})
57+
headers={'Authorization': access_token},
58+
event_hooks={
59+
'request': [logger.log_request],
60+
'response': [logger.log_response]
61+
})
5662

5763
def modify_header(self, header: dict) -> None:
5864
''' Modifies provided header in session object.

0 commit comments

Comments
 (0)