Skip to content

Commit 61e8bf3

Browse files
Add client description (#53)
1 parent c690d4e commit 61e8bf3

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

src/ahttpx/_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ async def close(self):
106106
await self.transport.close()
107107

108108
def __repr__(self):
109-
return "<Client>"
109+
return f"<Client [{self.transport.description()}]>"
110110

111111

112112
class RedirectMiddleware:

src/ahttpx/_pool.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,15 @@ def _remove_closed_connections(self) -> None:
6969
if conn.is_closed():
7070
self._connections.remove(conn)
7171

72+
def description(self) -> str:
73+
counts = {"active": 0}
74+
for status in [c.description() for c in self._connections]:
75+
counts[status] = counts.get(status, 0) + 1
76+
return ", ".join(f"{count} {status}" for status, count in counts.items())
77+
7278
# Builtins...
7379
def __repr__(self) -> str:
74-
return f"<ConectionPool {self._connections!r}>"
80+
return f"<ConectionPool [{self.description()}]]"
7581

7682
def __enter__(self) -> "ConnectionPool":
7783
return self
@@ -107,6 +113,18 @@ def is_expired(self, when: float):
107113
def is_closed(self):
108114
return self._state.our_state in (h11.CLOSED, h11.ERROR)
109115

116+
def description(self):
117+
return {
118+
h11.IDLE: "idle",
119+
h11.SEND_BODY: "active",
120+
h11.DONE: "active",
121+
h11.MUST_CLOSE: "closing",
122+
h11.CLOSED: "closed",
123+
h11.ERROR: "error",
124+
h11.MIGHT_SWITCH_PROTOCOL: "upgrading",
125+
h11.SWITCHED_PROTOCOL: "upgraded",
126+
}[self._state.our_state]
127+
110128
# API entry points...
111129
@contextlib.asynccontextmanager
112130
async def send(self, request: Request) -> typing.AsyncIterator[Response]:
@@ -189,7 +207,7 @@ async def _close(self):
189207

190208
# Builtins...
191209
def __repr__(self):
192-
return f"<Connection [{str(self._origin)}, {self._state.our_state}]>"
210+
return f"<Connection [{str(self._origin)}, {self.description()}]>"
193211

194212
async def __aenter__(self) -> "Connection":
195213
return self

src/httpx/_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def close(self):
106106
self.transport.close()
107107

108108
def __repr__(self):
109-
return "<Client>"
109+
return f"<Client [{self.transport.description()}]>"
110110

111111

112112
class RedirectMiddleware:

src/httpx/_pool.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,15 @@ def _remove_closed_connections(self) -> None:
6969
if conn.is_closed():
7070
self._connections.remove(conn)
7171

72+
def description(self) -> str:
73+
counts = {"active": 0}
74+
for status in [c.description() for c in self._connections]:
75+
counts[status] = counts.get(status, 0) + 1
76+
return ", ".join(f"{count} {status}" for status, count in counts.items())
77+
7278
# Builtins...
7379
def __repr__(self) -> str:
74-
return f"<ConectionPool {self._connections!r}>"
80+
return f"<ConectionPool [{self.description()}]]"
7581

7682
def __enter__(self) -> "ConnectionPool":
7783
return self
@@ -107,6 +113,18 @@ def is_expired(self, when: float):
107113
def is_closed(self):
108114
return self._state.our_state in (h11.CLOSED, h11.ERROR)
109115

116+
def description(self):
117+
return {
118+
h11.IDLE: "idle",
119+
h11.SEND_BODY: "active",
120+
h11.DONE: "active",
121+
h11.MUST_CLOSE: "closing",
122+
h11.CLOSED: "closed",
123+
h11.ERROR: "error",
124+
h11.MIGHT_SWITCH_PROTOCOL: "upgrading",
125+
h11.SWITCHED_PROTOCOL: "upgraded",
126+
}[self._state.our_state]
127+
110128
# API entry points...
111129
@contextlib.contextmanager
112130
def send(self, request: Request) -> typing.Iterator[Response]:
@@ -189,7 +207,7 @@ def _close(self):
189207

190208
# Builtins...
191209
def __repr__(self):
192-
return f"<Connection [{str(self._origin)}, {self._state.our_state}]>"
210+
return f"<Connection [{str(self._origin)}, {self.description()}]>"
193211

194212
def __enter__(self) -> "Connection":
195213
return self

0 commit comments

Comments
 (0)