Skip to content

Commit 3c87ef1

Browse files
authored
Merge pull request aboutcode-org#4 from TG1999/api
Design initial API
2 parents 856c9a4 + 2b11e7c commit 3c87ef1

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*pyc
2+
*__pycache__
3+
.cache
4+
.pytest_cache

fetchcode/api.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# fetchcode is a free software tool from nexB Inc. and others.
2+
# Visit https://github.com/nexB/fetchcode for support and download.
3+
#
4+
# Copyright (c) nexB Inc. and others. All rights reserved.
5+
# http://nexb.com and http://aboutcode.org
6+
#
7+
# This software is licensed under the Apache License version 2.0.
8+
#
9+
# You may not use this software except in compliance with the License.
10+
# You may obtain a copy of the License at:
11+
# http://apache.org/licenses/LICENSE-2.0
12+
# Unless required by applicable law or agreed to in writing, software distributed
13+
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations under the License.
16+
17+
import tempfile
18+
19+
import requests
20+
21+
22+
class Response:
23+
"""
24+
Represent the response from fetching a URL with:
25+
- `location`: the absolute location of the files that was fetched
26+
- `content_type`: content type of the file
27+
- `size`: size of the retrieved content in bytes
28+
- `url`: fetched URL
29+
"""
30+
def __init__(self, location, content_type, size, url):
31+
self.location = location
32+
self.content_type = content_type
33+
self.size = size
34+
self.url = url
35+
36+
37+
def fetch(url):
38+
"""
39+
Return a `Response` object built from fetching the content at the `url` URL string.
40+
"""
41+
r = requests.get(url)
42+
43+
temp = tempfile.NamedTemporaryFile(delete=False)
44+
filename = temp.name
45+
46+
with open(filename,'wb') as f:
47+
f.write(r.content)
48+
49+
resp = Response(location=filename,
50+
content_type=r.headers['content-type'],
51+
size=int(r.headers['content-length']),
52+
url=url)
53+
54+
return resp

tests/test_api.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# fetchcode is a free software tool from nexB Inc. and others.
2+
# Visit https://github.com/nexB/fetchcode for support and download.
3+
#
4+
# Copyright (c) nexB Inc. and others. All rights reserved.
5+
# http://nexb.com and http://aboutcode.org
6+
#
7+
# This software is licensed under the Apache License version 2.0.
8+
#
9+
# You may not use this software except in compliance with the License.
10+
# You may obtain a copy of the License at:
11+
# http://apache.org/licenses/LICENSE-2.0
12+
# Unless required by applicable law or agreed to in writing, software distributed
13+
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations under the License.
16+
17+
import os
18+
from unittest import mock
19+
20+
from fetchcode import api
21+
22+
@mock.patch('fetchcode.api.requests.get')
23+
def test_fetch(mock_get):
24+
25+
with mock.patch('fetchcode.api.open', mock.mock_open()) as mocked_file:
26+
url = 'https://raw.githubusercontent.com/lk-geimfari/unittest/master/logo.png'
27+
response = api.fetch(url=url)
28+
assert response is not None
29+
assert hasattr(response,'size')
30+
assert hasattr(response,'location')
31+
assert hasattr(response,'url')
32+
assert hasattr(response,'content_type')

0 commit comments

Comments
 (0)