Repository: internetarchive/openlibrary

Issue:
requests.exceptions.MissingSchema in core/lending.py
<!-- What problem are we solving? What does the experience look like today? What are the symptoms? -->

### Evidence / Screenshot (if possible)

https://sentry.archive.org/sentry/ol-web/issues/5654/?query=is%3Aunresolved

web_1        | 2020-10-21 17:34:54 [134] [openlibrary.core.lending] [ERROR] compose_ia_url failed!
web_1        | 2020-10-21 17:34:54 [134] [openlibrary.core.lending] [ERROR] compose_ia_url failed!
web_1        | 2020-10-21 17:34:54 [134] [openlibrary.core.lending] [ERROR] get_available failed
web_1        | 2020-10-21 17:34:54 [134] [openlibrary.core.lending] [ERROR] get_available()
web_1        | Traceback (most recent call last):
web_1        |   File "/openlibrary/openlibrary/core/lending.py", line 284, in get_available
web_1        |     response = requests.get(
web_1        |   File "/home/openlibrary/.pyenv/versions/3.8.6/lib/python3.8/site-packages/requests/api.py", line 76, in get
web_1        |     return request('get', url, params=params, **kwargs)
web_1        |   File "/home/openlibrary/.pyenv/versions/3.8.6/lib/python3.8/site-packages/requests/api.py", line 61, in request
web_1        |     return session.request(method=method, url=url, **kwargs)
web_1        |   File "/home/openlibrary/.pyenv/versions/3.8.6/lib/python3.8/site-packages/requests/sessions.py", line 516, in request
web_1        |     prep = self.prepare_request(req)
web_1        |   File "/home/openlibrary/.pyenv/versions/3.8.6/lib/python3.8/site-packages/requests/sessions.py", line 449, in prepare_request
web_1        |     p.prepare(
web_1        |   File "/home/openlibrary/.pyenv/versions/3.8.6/lib/python3.8/site-packages/requests/models.py", line 314, in prepare
web_1        |     self.prepare_url(url, params)
web_1        |   File "/home/openlibrary/.pyenv/versions/3.8.6/lib/python3.8/site-packages/requests/models.py", line 388, in prepare_url
web_1        |     raise MissingSchema(error)
web_1        | requests.exceptions.MissingSchema: Invalid URL '': No schema supplied. Perhaps you meant http://?

### Relevant url?
<!-- `https://openlibrary.org/...` -->
[`compose_ia_url()`](https://github.com/internetarchive/openlibrary/blob/master/openlibrary/core/lending.py#L114) needs a rewrite to be less cryptic and to raise an Exception if it can not compose a URL.  Returning an empty string is too confusing for [callers](https://github.com/internetarchive/openlibrary/search?q=compose_ia_url).



### Steps to Reproduce
<!-- What steps caused you to find the bug? -->
1. Go to ...
2. Do ...

<!-- What actually happened after these steps? What did you expect to happen? -->
* Actual: 
* Expected: 

### Details

- **Logged in (Y/N)?** 
- **Browser type/version?** 
- **Operating system?** 
- **Environment (prod/dev/local)?** prod
<!-- If not sure, put prod -->

### Proposal & Constraints
<!-- What is the proposed solution / implementation? Is there a precedent of this approach succeeding elsewhere? -->

### Related files
<!-- Files related to this issue; this is super useful for new contributors who might want to help! If you're not sure, leave this blank; a maintainer will add them. -->

### Stakeholders
<!-- @ tag stakeholders of this bug -->



Discussion:

Resolving patch:
diff --git a/openlibrary/plugins/openlibrary/api.py b/openlibrary/plugins/openlibrary/api.py
--- a/openlibrary/plugins/openlibrary/api.py
+++ b/openlibrary/plugins/openlibrary/api.py
@@ -69,9 +69,10 @@ def GET(self):
         url = lending.compose_ia_url(
             query=i.q, limit=limit, page=page, subject=i.subject,
             work_id=i.work_id, _type=i._type, sorts=sorts)
+        works = lending.get_available(url=url) if url else []
         result = {
             'query': url,
-            'works': [work.dict() for work in lending.get_available(url=url)],
+            'works': [work.dict() for work in works],
         }
         return delegate.RawText(
             simplejson.dumps(result),


Test patch: