infolinks.py 2.91 KB
Newer Older
Markus Scheidgen's avatar
Markus Scheidgen committed
1
2
3
4
#
# Copyright The NOMAD Authors.
#
# This file is part of NOMAD. See https://nomad-lab.eu for further info.
5
6
7
8
9
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
Markus Scheidgen's avatar
Markus Scheidgen committed
10
#     http://www.apache.org/licenses/LICENSE-2.0
11
12
#
# Unless required by applicable law or agreed to in writing, software
Markus Scheidgen's avatar
Markus Scheidgen committed
13
# distributed under the License is distributed on an "AS IS" BASIS,
14
15
16
# 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.
Markus Scheidgen's avatar
Markus Scheidgen committed
17
#
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

from flask_restplus import Resource
from flask import request

from nomad import config

from .api import api, url
from .common import ns, base_request_args
from .models import json_api_single_response_model, base_endpoint_parser, Meta, \
    json_api_list_response_model


@ns.route('/info')
class Info(Resource):
    @api.doc('info')
    @api.response(400, 'Invalid requests, e.g. bad parameter.')
    @api.expect(base_endpoint_parser, validate=True)
    @api.marshal_with(json_api_single_response_model, skip_none=True, code=200)
    def get(self):
        ''' Returns information about this optimade implementation '''
        base_request_args()

        result = {
            'type': 'info',
            'id': '/',
            'attributes': {
44
                'api_version': '1.0.0',
45
46
                'available_api_versions': [{
                    'url': url(),
47
                    'version': '1.0.0'
48
49
50
                }],
                'formats': ['json'],
                'entry_types_by_format': {
51
                    'json': ['structures', 'calculations']
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
                },
                'available_endpoints': ['structures', 'calculations', 'info'],
                'is_index': False
            }
        }

        return dict(
            meta=Meta(query=request.url, returned=1),
            data=result
        ), 200


@ns.route('/links')
class Links(Resource):
    @api.doc('links')
    @api.response(400, 'Invalid requests, e.g. bad parameter.')
    @api.expect(base_endpoint_parser, validate=True)
    @api.marshal_with(json_api_list_response_model, skip_none=True, code=200)
    def get(self):
        ''' Returns information about related optimade databases '''
        base_request_args()

        result = [
            {
76
                "type": "links",
77
78
79
80
                "id": "index",
                "attributes": {
                    "name": config.meta.name,
                    "description": config.meta.description,
81
                    "link_type": "root",
82
83
84
85
86
87
88
89
90
91
92
93
                    "base_url": {
                        "href": url(version=None, prefix='index'),
                    },
                    "homepage": config.meta.homepage
                }
            }
        ]

        return dict(
            meta=Meta(query=request.url, returned=1),
            data=result
        ), 200