File tree Expand file tree Collapse file tree 2 files changed +27
-10
lines changed Expand file tree Collapse file tree 2 files changed +27
-10
lines changed Original file line number Diff line number Diff line change @@ -181,24 +181,22 @@ class FetchUserProfile(object):
181
181
""" Single responsibility callable object that fetches user profile."""
182
182
183
183
@pipeline
184
- def __call__ (self , user_id : int ) -> Result[ IO [' UserProfile' ] , Exception ]:
184
+ def __call__ (self , user_id : int ) -> IO [Result[ ' UserProfile' , Exception ] ]:
185
185
""" Fetches UserProfile dict from foreign API."""
186
- response = self ._make_request(user_id).unwrap()
187
- return self ._parse_json(response)
186
+ return self ._make_request(user_id).map(
187
+ lambda response : self ._parse_json(response.unwrap())
188
+ )
188
189
189
- @safe
190
190
@impure
191
+ @safe
191
192
def _make_request (self , user_id : int ) -> requests.Response:
192
193
response = requests.get(' /api/users/{0} ' .format(user_id))
193
194
response.raise_for_status()
194
195
return response
195
196
196
197
@safe
197
- def _parse_json (
198
- self ,
199
- io_response : IO [requests.Response],
200
- ) -> IO [' UserProfile' ]:
201
- return io_response.map(lambda response : response.json())
198
+ def _parse_json (self ,response : requests.Response) -> ' UserProfile' :
199
+ return response.json()
202
200
```
203
201
204
202
Now we have explicit markers where the ` IO ` did happen
Original file line number Diff line number Diff line change @@ -7,7 +7,13 @@ Container is a concept that allows you
7
7
to write code around the existing wrapped values
8
8
while maintaining the execution context.
9
9
10
- We will show you its simple API of one attribute and several simple methods.
10
+ List of supported containers:
11
+
12
+ - :class: `IO <returns.io.IO> ` to mark explicit ``IO `` actions
13
+ - :class: `Result <returns.result.Result> ` to handle possible exceptions
14
+
15
+ We will show you container's simple API of one attribute
16
+ and several simple methods.
11
17
12
18
13
19
Basics
@@ -268,6 +274,19 @@ You can have a look at the suggested ``mypy``
268
274
in our own repository.
269
275
270
276
277
+ Composition
278
+ -----------
279
+
280
+ You can and should compose different containers together.
281
+ Here's the full table of compositions that make sense:
282
+
283
+ - ``IO[Result[A, B]] `` ✅
284
+ - ``IO[IO[A]] `` 🚫
285
+ - ``Result[IO[A], B] `` 🚫
286
+ - ``Result[A, IO[A]] `` 🚫
287
+ - ``Result[Result[A, B], C] `` 🚫
288
+ - ``Result[A, Result[B, C]] `` 🚫
289
+
271
290
API Reference
272
291
-------------
273
292
You can’t perform that action at this time.
0 commit comments