Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*pyc
*__pycache__
.cache
.pytest_cache
54 changes: 54 additions & 0 deletions fetchcode/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# fetchcode is a free software tool from nexB Inc. and others.
# Visit https://github.com/nexB/fetchcode for support and download.
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# http://nexb.com and http://aboutcode.org
#
# This software is licensed under the Apache License version 2.0.
#
# You may not use this software except in compliance with the License.
# You may obtain a copy of the License at:
# http://apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.

import tempfile

import requests


class Response:
"""
Represent the response from fetching a URL with:
- `location`: 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
"""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All changes have been done, please have a look

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

def __init__(self, location, content_type, size, url):
self.location = location
self.content_type = content_type
self.size = size
self.url = url


def fetch(url):
"""
Return a `Response` object built from fetching the content at the `url` URL string.
"""
r = requests.get(url)

temp = tempfile.NamedTemporaryFile(delete=False)
filename = temp.name

with open(filename,'wb') as f:
f.write(r.content)

resp = Response(location=filename,
content_type=r.headers['content-type'],
size=int(r.headers['content-length']),
url=url)

return resp
32 changes: 32 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# fetchcode is a free software tool from nexB Inc. and others.
# Visit https://github.com/nexB/fetchcode for support and download.
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# http://nexb.com and http://aboutcode.org
#
# This software is licensed under the Apache License version 2.0.
#
# You may not use this software except in compliance with the License.
# You may obtain a copy of the License at:
# http://apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.

import os
from unittest import mock

from fetchcode import api

@mock.patch('fetchcode.api.requests.get')
def test_fetch(mock_get):

with mock.patch('fetchcode.api.open', mock.mock_open()) as mocked_file:
url = 'https://raw.githubusercontent.com/lk-geimfari/unittest/master/logo.png'
response = api.fetch(url=url)
assert response is not None
assert hasattr(response,'size')
assert hasattr(response,'location')
assert hasattr(response,'url')
assert hasattr(response,'content_type')