Skip to content

Commit

Permalink
Merge pull request #35 from open-contracting/new-api-2
Browse files Browse the repository at this point in the history
Item HTTP API end point - change to match file one
  • Loading branch information
odscjames committed Jan 25, 2019
2 parents 4c1e7c4 + 6693d0d commit ab31cb2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
6 changes: 3 additions & 3 deletions docs/web-api-v1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ Store Item

The end point is /api/v1/submit/item/

You must pass all the data as one JSON object in the body of a post request.
Pass data as POST variables.

Firstly, you must pass details of the collection.

* `collection_source` - String.
* `collection_data_version` - String. In format YYYY-MM-DD HH:MM:SS
* `collection_sample` - Boolean.
* `collection_sample` - String. Pass "true" for True.

Secondly, you must pass details of the file.

Expand All @@ -61,5 +61,5 @@ Thirdly, you must pass details of the item in the file.

* `number` - Integer.

Finally, pass the actual file loaded as JSON data in the `data` key.
Finally, pass the data as a string in the `data` key.

21 changes: 11 additions & 10 deletions ocdskingfisherprocess/web/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from ocdskingfisherprocess.util import parse_string_to_date_time, parse_string_to_boolean
import tempfile
import os
import json

config = Config()
config.load_user_config()
Expand Down Expand Up @@ -99,28 +100,28 @@ def api_v1_submit_item():
database = DataBase(config=config)
store = Store(config=config, database=database)

data = request.get_json()

collection_source = data.get('collection_source')
collection_data_version = parse_string_to_date_time(data.get('collection_data_version'))
collection_sample = data.get('collection_sample', False)
collection_source = request.form.get('collection_source')
collection_data_version = parse_string_to_date_time(request.form.get('collection_data_version'))
collection_sample = parse_string_to_boolean(request.form.get('collection_sample', False))

store.load_collection(
collection_source,
collection_data_version,
collection_sample,
)

file_filename = data.get('file_name')
file_url = data.get('url')
file_data_type = data.get('data_type')
item_number = int(data.get('number'))
file_filename = request.form.get('file_name')
file_url = request.form.get('url')
file_data_type = request.form.get('data_type')
item_number = int(request.form.get('number'))

data = json.loads(request.form.get('data'))

store.store_file_item(
file_filename,
file_url,
file_data_type,
data.get('data'),
data,
item_number,
)

Expand Down
27 changes: 27 additions & 0 deletions tests/test_web_api_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,30 @@ def test_api_v1_submit_file(self):
files = self.database.get_all_files_in_collection(collection_id)
assert len(files) == 1
assert files[0].filename == 'test.json'

def test_api_v1_submit_item(self):
self.setup_main_database()

data = {
'collection_source': 'test',
'collection_data_version': '2018-10-10 00:12:23',
'collection_sample': 'true',
'file_name': 'test.json',
'url': 'http://example.com',
'data_type': 'record',
'number': 0,
'data': ' {"valid_data": "Totally. It totally is."}',
}

result = self.flaskclient.post('/api/v1/submit/item/',
data=data,
headers={'Authorization': 'ApiKey ' + self.config.web_api_keys[0]})

assert result.status_code == 200

collection_id = self.database.get_collection_id('test', '2018-10-10 00:12:23', True)
assert collection_id

files = self.database.get_all_files_in_collection(collection_id)
assert len(files) == 1
assert files[0].filename == 'test.json'

0 comments on commit ab31cb2

Please sign in to comment.