#     Copyright 2015 Cedraro Andrea <a.cedraro@gmail.com>
# 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
#
# http://www.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.

from jedihttp import utils
utils.AddVendorFolderToSysPath()

import jedi
import logging
import json
from jedihttp import hmaclib
from bottle import response, request, Bottle

try:
  from http import client as httplib
except ImportError:
  import httplib


logger = logging.getLogger( __name__ )
app = Bottle( __name__ )


@app.post( '/healthy' )
def healthy():
  logger.debug( 'received /healthy request' )
  return _JsonResponse( True )


@app.post( '/ready' )
def ready():
  logger.debug( 'received /ready request' )
  return _JsonResponse( True )


@app.post( '/completions' )
def completions():
  logger.debug( 'received /completions request' )
  script = _GetJediScript( request.json )
  return _JsonResponse( {
      'completions': [ {
        'name':        completion.name,
        'description': completion.description,
        'docstring':   completion.docstring(),
        'module_path': completion.module_path,
        'line':        completion.line,
        'column':      completion.column
      } for completion in script.completions() ]
  } )


@app.post( '/gotodefinition' )
def gotodefinition():
  logger.debug( 'received /gotodefinition request' )
  script = _GetJediScript( request.json )
  return _JsonResponse( _FormatGoToDefinitions( script.goto_definitions() ) )


@app.post( '/gotoassignment' )
def gotoassignments():
  logger.debug( 'received /gotoassignment request' )
  script = _GetJediScript( request.json )
  return _JsonResponse( _FormatGoToDefinitions( script.goto_assignments() ) )


def _FormatGoToDefinitions( definitions ):
  return {
      'definitions': [ {
        'module_path':       definition.module_path,
        'line':              definition.line,
        'column':            definition.column,
        'in_builtin_module': definition.in_builtin_module(),
        'is_keyword':        definition.is_keyword,
        'description':       definition.description,
        'docstring':         definition.docstring()
      } for definition in definitions ]
  }


def _GetJediScript( request_data ):
  return jedi.Script( request_data[ 'source' ],
                      request_data[ 'line' ],
                      request_data[ 'col' ],
                      request_data[ 'path' ] )


@app.error( httplib.INTERNAL_SERVER_ERROR )
def ErrorHandler( httperror ):
  hmac_secret = app.config[ 'jedihttp.hmac_secret' ]
  hmachelper = hmaclib.JediHTTPHmacHelper( hmac_secret )
  body = _JsonResponse( {
    'exception': httperror.exception,
    'message': str( httperror.exception ),
    'traceback': httperror.traceback
  } )
  hmachelper.SignResponseHeaders( response.headers, body )
  return body


def _JsonResponse( data ):
  response.content_type = 'application/json'
  return json.dumps( data, default = _Serializer )


def _Serializer( obj ):
  try:
    serialized = obj.__dict__.copy()
    serialized[ 'TYPE' ] = type( obj ).__name__
    return serialized
  except AttributeError:
    return str( obj )

@@ -2906,118 +2906,8 @@
  ):%0A
-  hmac_secret = app.config%5B 'jedihttp.hmac_secret' %5D%0A  hmachelper = hmaclib.JediHTTPHmacHelper( hmac_secret )%0A
   bo
@@ -3052,16 +3052,126 @@
 k%0A  %7D )%0A
+  hmac_secret = app.config%5B 'jedihttp.hmac_secret' %5D%0A  hmachelper = hmaclib.JediHTTPHmacHelper( hmac_secret )%0A
   hmache

Move hmachelper closer to the use of it