
from tornado.web import Finish

from biothings.web.api.es.handlers.base_handler import ESRequestHandler


class BiothingHandler(ESRequestHandler):
    '''
    Handle requests to the annotation lookup endpoint.
    '''
    name = 'annotation'

    def pre_query_builder_hook(self, options):
        '''
        Set GA tracking object.
        '''
        if self.request.method == 'POST':
            self.ga_event_object({'qsize': len(options.esqb.ids)})
        return options

    def pre_query_hook(self, options, query):
        '''
        Request _version field.
        '''
        options.es.version = True
        return super().pre_query_hook(options, query)

    def pre_finish_hook(self, options, res):
        '''
        Return single result for GET.
        Also does not return empty results.
        '''
        if isinstance(res, dict):
            if not res.get('hits'):
                self.send_error(404, reason=self.web_settings.ID_NOT_FOUND_TEMPLATE.format(bid=options.esqb.bid))
                raise Finish()  # TODO
            res = res['hits'][0]
            res.pop('_score')

        elif isinstance(res, list):
            for hit in res:
                hit.pop('_score', None)

        return res

@@ -1,35 +1,835 @@
-%0Afrom tornado.web import Finish
+%22%22%22%0A    Biothings Annotation Endpoint%0A%0A    URL pattern examples:%0A%0A        /%7Bpre%7D/%7Bver%7D/%7Btyp%7D/?%0A        /%7Bpre%7D/%7Bver%7D/%7Btyp%7D/(%5B%5E%5C/%5D+)/?%0A%0A    GET /v3/gene/1017%0A        %7B%0A            %22HGNC%22: %221771%22,%0A            %22MIM%22: %22116953%22,%0A            %22_id%22: %221017%22,%0A            %22_version%22: 1%0A            ...%0A        %7D%0A%0A    GET /v3/gene/0%0A        %7B%0A            %22success%22: false,%0A            %22error%22: %22ID '0' not found%22%0A        %7D%0A%0A    POST /v3/gene%0A    %7B%0A        %22ids%22: %5B%221017%22%5D%0A    %7D%0A    %3E%3E%3E %5B%0A            %7B%0A                %22query%22: %221017%22,%0A                %22HGNC%22: %221771%22,%0A                %22MIM%22: %22116953%22,%0A                %22_id%22: %221017%22,%0A                ...%0A            %7D%0A        %5D%0A%0A    POST /v3/gene%0A    %7B%0A        %22ids%22: %5B%220%22%5D%0A    %7D%0A    %3E%3E%3E %5B%0A            %7B%0A                %22query%22: %220%22,%0A                %22notfound%22: true%0A            %7D%0A        %5D%0A%0A%22%22%22
 %0A%0Afr
@@ -898,16 +898,64 @@
 Handler%0A
+from biothings.web.api.handler import EndRequest
 %0A%0Aclass 
@@ -997,74 +997,435 @@
     
-'''
+%22%22%22
 %0A    
-Handle requests to the annotation lookup endpoint.
+An Annotation Request%0A%0A    1.  queries a term against a pre-determined field that%0A        represents the id of a document, like _id and dbsnp.rsid%0A%0A    2.  should find either exactly 1 or 0 result and returns%0A        either 200 for a document found or 404 for not found.%0A%0A    3.  formats the response like the _source field in elasticsearch.%0A        Result does not contain _score field, but contains _version.%0A
 %0A    
-'''
+%22%22%22
 %0A   
@@ -1497,32 +1497,60 @@
 s):%0A        '''%0A
+        Use default scopes.%0A
         Set GA t
@@ -1569,32 +1569,65 @@
 ct.%0A        '''%0A
+        options.esqb.scopes = %5B%5D%0A
         if self.
@@ -1712,19 +1712,17 @@
 ns.esqb.
-ids
+q
 )%7D)%0A    
@@ -2173,36 +2173,19 @@
     
-self.send_error(404, reason=
+template = 
 self
@@ -2219,16 +2219,50 @@
 TEMPLATE
+%0A                reason = template
 .format(
@@ -2282,12 +2282,9 @@
 sqb.
-bid)
+q
 )%0A  
@@ -2307,24 +2307,38 @@
 ise 
-Finish()  # TODO
+EndRequest(404, reason=reason)
 %0A   
@@ -2395,16 +2395,22 @@
 '_score'
+, None
 )%0A%0A     

Revise annotation error handling