Design initial API - #4
Conversation
|
@pombredanne please have a look on it and suggest me some changes. |
pombredanne
left a comment
There was a problem hiding this comment.
Thank you. See my comments inline.
There are some comments on the format, other on the code (which I reckon is really a placeholder) and some on the API. The API of the returned data could either be an object or a dict. I think an object would be better.
| con = requests.head(url) | ||
| r = requests.get(url) | ||
| name = url.split('/')[-1] | ||
| filename = '../download/'+name |
There was a problem hiding this comment.
You cannot assume some location like this. It has to be some temp file instead.
There was a problem hiding this comment.
I am not able to get this, like what should I have here ?
| 'type_of_data' : con.headers['content-type'], | ||
| 'size' : con.headers['content-length'], | ||
| 'url' : url, | ||
| 'checksum' : hashlib.md5(open(filename,'rb').read()).hexdigest() |
There was a problem hiding this comment.
what if this fails? what if the file is not closed? And if the content is already in memory, why re-reading from disk?
Also why privilege md5 and make it the one value for checksum?
There was a problem hiding this comment.
Okay please can you suggest me some alternative for this
| import requests | ||
| import hashlib | ||
| def fetch(url): | ||
| con = requests.head(url) |
There was a problem hiding this comment.
Why to do issue a head first?
There was a problem hiding this comment.
I am not able to get this please can you elaborate a little 😅
|
I have made some changes that you suggested, and I was not able to understand some suggestions please explain them again to me so I can complete them too 😀 |
pombredanne
left a comment
There was a problem hiding this comment.
Thanks for the updates... See my comments inline with the code
|
@pombredanne I have made some more changes I hope now the PR is good to merge 😀 |
pombredanne
left a comment
There was a problem hiding this comment.
Thank you: I posted several comments for your consideration. At a high level we want some detailed docstrings for API functions. And we should NOT catch exceptions and surely not print things on errors: this is meant to be a library, and printing error message will pollute the terminal of the users of the apps that will use this lib....AND they will not be able to do ay thing about it. (side note: try >>> import this ;) )
We likely need to have some tests too sooner than later.
pombredanne
left a comment
There was a problem hiding this comment.
Thank you. This is starting to look quite good. One thing thouh is that we do not want tests to make live network call. Instead you should use some mock object and never do a never call during a test
|
Thanks @pombredanne please can you give me some example, I am not able to understand which one should I use here 😅 |
|
@pombredanne done with Mocking of tests too, can the PR be merged now ? |
pombredanne
left a comment
There was a problem hiding this comment.
Thank you for the updates... we are making good progress!
There are several small things to fix for your review... also you are missing tests for the fetch() function.
Now you also need to craft proper commit messages that are informative and detailed:
- some fixes
- some more fixes
etc are not great: what information does it carry? close to none. Say you review this commit streams in a few months from now: will these commit messages help you understand what the commits are about? I very much doubt so ;)
Good commit matter a lot: please see https://github.com/nexB/aboutcode/wiki/Writing-good-commit-messages and the related docs in details!
Thank you!
| self.url = url | ||
|
|
||
|
|
||
| def get_request(url): |
There was a problem hiding this comment.
What's the purpose of this function that is only a one liner? Is this to help with mock testing? if not it would be best inlined.
| return requests.get(url) | ||
|
|
||
|
|
||
| def Filewriter(filename,content): |
There was a problem hiding this comment.
We never use Title case for function names. Also always use a space between args.
That said, why a function for a simple write? IMHO it would best inlined, unless you can explain a good reason why,
| """ | ||
| Takes URL as parameter and write down the content inside it | ||
| to a temporary location and return a `Response` kind of object | ||
| """ |
There was a problem hiding this comment.
I would prefer a formulation such as:
Return a `Response` object built from fetching the content at the `url` URL string.
There was a problem hiding this comment.
It seems cool, doing it ;)
| Takes URL as parameter and write down the content inside it | ||
| to a temporary location and return a `Response` kind of object | ||
| """ | ||
| r = get_request(url=url) |
There was a problem hiding this comment.
inline this as explained above, unless you can explain why you need this
| import os | ||
| from unittest.mock import Mock,patch,mock_open | ||
|
|
||
| from nose.tools import assert_is_not_none |
There was a problem hiding this comment.
Please do not use nose. We prefer using pytest as a test runner and using simple plain assert instead.
|
|
||
| from nose.tools import assert_is_not_none | ||
|
|
||
| from src import api |
There was a problem hiding this comment.
if our module is called src, there is something wrong: there should be a a fetchcode module with an api submodule instead
| def test_get_request(): | ||
| """ | ||
| To test api when an image is being downloaded | ||
| """ |
There was a problem hiding this comment.
Try to avoid docstring on test functions: instead using an explicit verbose function name is enough and clearer in most cases. It also helps with selecting tests.
| mock_get.return_value.ok = True | ||
| url = 'https://raw.githubusercontent.com/lk-geimfari/unittest/master/logo.png' | ||
| response = api.get_request(url) | ||
| assert_is_not_none(response) |
There was a problem hiding this comment.
Use assert response is not None instead and do not use nose. That said, I am not sure what you are testing in this cases: can you explain a bit?
|
@pombredanne made all the changes and squashed all commits into one, I think the commit message is clean enough to describe the work |
pombredanne
left a comment
There was a problem hiding this comment.
Thank you! there are only a few minor format comments to address and a suggested improvement on doc and field names... and we are good to go!
| 2) Content type of the file | ||
| 3) Size of the file | ||
| 4) URL of the file | ||
| """ |
There was a problem hiding this comment.
What about using this instead:
Represent the response from fetching a URL with:
- `filename`: the absolute location of the files that was fetched
- `content_type`: content type of the file
- `size`: size of the retrieved content in bytes
- `url`: fetched URL
Also in hindsight, what about using location and not filename and ensure this is an absolute file location? and not just a file name?
There was a problem hiding this comment.
All changes have been done, please have a look
Signed-off-by: tushar goel <tushar.goel.dav@gmail.com> some fixes Signed-off-by: tushar goel <tushar.goel.dav@gmail.com> some fixes Signed-off-by: tushar goel <tushar.goel.dav@gmail.com> some changes refined Signed-off-by: tushar goel <tushar.goel.dav@gmail.com> make some fixes Signed-off-by: tushar goel <tushar.goel.dav@gmail.com> Update api.py add tests and improve api Signed-off-by: tushar goel <tushar.goel.dav@gmail.com> Divide code into functions and test file writing and get requests with Mock Signed-off-by: tushar goel <tushar.goel.dav@gmail.com> Divide code Signed-off-by: tushar goel <tushar.goel.dav@gmail.com> test the functions inside API Signed-off-by: tushar goel <tushar.goel.dav@gmail.com> Design Initial API and test it with mock Object Signed-off-by: tushar goel <tushar.goel.dav@gmail.com> Nitpicking: Remove empty lines Nitpicking: change parameters name Design Initial API Signed-off-by: tushar goel <tushar.goel.dav@gmail.com>
|
@pombredanne please review it 😄 all the problems has been resolved I think it can be merged now |
| 2) Content type of the file | ||
| 3) Size of the file | ||
| 4) URL of the file | ||
| """ |
Signed-off-by: Ayan Sinha Mahapatra <ayansmahapatra@gmail.com>
Solves issue #3
Signed-off-by: tushar goel tushar.goel.dav@gmail.com