<commit_before>"""
Copyright 2014 Google Inc. All rights reserved.

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.

This file serves two main purposes
- it serves up the main html page
- and it provides a simple set of apis to the javascript
"""

import httplib2
import jinja2
import json
import logging
import os
import socket
import webapp2

from oauth2client import appengine
from google.appengine.api import users
from google.appengine.api import urlfetch
from google.appengine.api import memcache


# Increase timeout to the maximum for all requests and use caching
urlfetch.set_default_fetch_deadline(60)
socket.setdefaulttimeout(60)
http = httplib2.Http(cache=memcache)

JINJA_ENVIRONMENT = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
    autoescape=True,
    extensions=['jinja2.ext.autoescape'])

client_secrets = os.path.join(os.path.dirname(__file__), 'client_secrets.json')

decorator = appengine.oauth2decorator_from_clientsecrets(
    client_secrets,
    scope=[
      'https://www.googleapis.com/auth/genomics',
      'https://www.googleapis.com/auth/devstorage.read_write'
    ])


class ApiException(Exception):
  pass


class BaseRequestHandler(webapp2.RequestHandler):
  def handle_exception(self, exception, debug_mode):
    if isinstance(exception, ApiException):
      # ApiExceptions are expected, and will return nice error
      # messages to the client
      self.response.write(exception.message)
      self.response.set_status(400)
    else:
      # All other exceptions are unexpected and should be logged
      logging.exception('Unexpected exception')
      self.response.write('Unexpected internal exception')
      self.response.set_status(500)

  def get_content(self, path, method='POST', body=None):
    http = decorator.http()
    response, content = http.request(
      uri="https://www.googleapis.com/genomics/v1beta/%s" % path,
      method=method, body=json.dumps(body) if body else None,
      headers={'Content-Type': 'application/json; charset=UTF-8'})

    logging.debug("api response %s" % response)
    logging.debug("api content %s" % content)

    if response.status == 404:
      raise ApiException('API not found')
    elif response.status == 400:
      raise ApiException('API request malformed')
    elif response.status != 200:
      raise ApiException('Something went wrong with the API call!')

    content = json.loads(content)
    if 'error' in content:
      raise ApiException(content['error']['message'])

    self.response.write(json.dumps(content))


class ReadsetSearchHandler(BaseRequestHandler):

  @decorator.oauth_aware
  def get(self):
    readset_id = self.request.get('readsetId')
    if not readset_id:
      body = {'datasetIds': ['383928317087']}
      self.get_content("readsets/search", body=body)
      return

    # Single readset response
    targets = [
      {'name': "chr1", 'sequenceLength': 249250621},
      {'name': "chr2", 'sequenceLength': 243199373},
      {'name': "chr3", 'sequenceLength': 198022430},
      {'name': "chr4", 'sequenceLength': 191154276},
      {'name': "chr5", 'sequenceLength': 180915260},
      {'name': "chr6", 'sequenceLength': 171115067},
      {'name': "chr7", 'sequenceLength': 159138663},
      {'name': "chr8", 'sequenceLength': 146364022},
      {'name': "chr9", 'sequenceLength': 141213431},
      {'name': "chr10", 'sequenceLength': 135534747},
      {'name': "chr11", 'sequenceLength': 135006516},
      {'name': "chr12", 'sequenceLength': 133851895},
      {'name': "chr13", 'sequenceLength': 115169878},
      {'name': "chr14", 'sequenceLength': 107349540},
      {'name': "chr15", 'sequenceLength': 102531392},
      {'name': "chr16", 'sequenceLength': 90354753},
      {'name': "chr17", 'sequenceLength': 81195210},
      {'name': "chr18", 'sequenceLength': 78077248},
      {'name': "chr19", 'sequenceLength': 59128983},
      {'name': "chr20", 'sequenceLength': 63025520},
      {'name': "chr21", 'sequenceLength': 48129895},
      {'name': "chr22", 'sequenceLength': 51304566},
      {'name': "chrX", 'sequenceLength': 155270560},
      {'name': "chrY", 'sequenceLength': 59373566},<commit_msg>Only fetch a partial response for the readsets call<commit_after>"""
Copyright 2014 Google Inc. All rights reserved.

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.

This file serves two main purposes
- it serves up the main html page
- and it provides a simple set of apis to the javascript
"""

import httplib2
import jinja2
import json
import logging
import os
import socket
import webapp2

from oauth2client import appengine
from google.appengine.api import users
from google.appengine.api import urlfetch
from google.appengine.api import memcache


# Increase timeout to the maximum for all requests and use caching
urlfetch.set_default_fetch_deadline(60)
socket.setdefaulttimeout(60)
http = httplib2.Http(cache=memcache)

JINJA_ENVIRONMENT = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
    autoescape=True,
    extensions=['jinja2.ext.autoescape'])

client_secrets = os.path.join(os.path.dirname(__file__), 'client_secrets.json')

decorator = appengine.oauth2decorator_from_clientsecrets(
    client_secrets,
    scope=[
      'https://www.googleapis.com/auth/genomics',
      'https://www.googleapis.com/auth/devstorage.read_write'
    ])


class ApiException(Exception):
  pass


class BaseRequestHandler(webapp2.RequestHandler):
  def handle_exception(self, exception, debug_mode):
    if isinstance(exception, ApiException):
      # ApiExceptions are expected, and will return nice error
      # messages to the client
      self.response.write(exception.message)
      self.response.set_status(400)
    else:
      # All other exceptions are unexpected and should be logged
      logging.exception('Unexpected exception')
      self.response.write('Unexpected internal exception')
      self.response.set_status(500)

  def get_content(self, path, method='POST', body=None):
    http = decorator.http()
    response, content = http.request(
      uri="https://www.googleapis.com/genomics/v1beta/%s" % path,
      method=method, body=json.dumps(body) if body else None,
      headers={'Content-Type': 'application/json; charset=UTF-8'})

    logging.debug("api response %s" % response)
    logging.debug("api content %s" % content)

    if response.status == 404:
      raise ApiException('API not found')
    elif response.status == 400:
      raise ApiException('API request malformed')
    elif response.status != 200:
      raise ApiException('Something went wrong with the API call!')

    content = json.loads(content)
    if 'error' in content:
      raise ApiException(content['error']['message'])

    self.response.write(json.dumps(content))


class ReadsetSearchHandler(BaseRequestHandler):

  @decorator.oauth_aware
  def get(self):
    readset_id = self.request.get('readsetId')
    if not readset_id:
      body = {'datasetIds': ['383928317087']}
      self.get_content("readsets/search?fields=readsets(id,name)", body=body)
      return

    # Single readset response
    targets = [
      {'name': "chr1", 'sequenceLength': 249250621},
      {'name': "chr2", 'sequenceLength': 243199373},
      {'name': "chr3", 'sequenceLength': 198022430},
      {'name': "chr4", 'sequenceLength': 191154276},
      {'name': "chr5", 'sequenceLength': 180915260},
      {'name': "chr6", 'sequenceLength': 171115067},
      {'name': "chr7", 'sequenceLength': 159138663},
      {'name': "chr8", 'sequenceLength': 146364022},
      {'name': "chr9", 'sequenceLength': 141213431},
      {'name': "chr10", 'sequenceLength': 135534747},
      {'name': "chr11", 'sequenceLength': 135006516},
      {'name': "chr12", 'sequenceLength': 133851895},
      {'name': "chr13", 'sequenceLength': 115169878},
      {'name': "chr14", 'sequenceLength': 107349540},
      {'name': "chr15", 'sequenceLength': 102531392},
      {'name': "chr16", 'sequenceLength': 90354753},
      {'name': "chr17", 'sequenceLength': 81195210},
      {'name': "chr18", 'sequenceLength': 78077248},
      {'name': "chr19", 'sequenceLength': 59128983},
      {'name': "chr20", 'sequenceLength': 63025520},
      {'name': "chr21", 'sequenceLength': 48129895},
      {'name': "chr22", 'sequenceLength': 51304566},
      {'name': "chrX", 'sequenceLength': 155270560},
      {'name': "chrY", 'sequenceLength': 59373566},
{'fraction_non_alphanumeric': 0.1298854267052147, 'fraction_numerical': 0.06296177320912304, 'mean_word_length': 4.203342618384401, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 6, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2661306', 'n_tokens_mistral': 3189, 'n_tokens_neox': 2772, 'n_words': 787}