<s> from __future__ import ( <EOL> print_function , unicode_literals , division , absolute_import ) <EOL> import requests <EOL> import json <EOL> import time <EOL> import re <EOL> import six <EOL> from monkeylearn . settings import ( MAX_BATCH_SIZE , MIN_BATCH_SIZE ) <EOL> from monkeylearn . exceptions import MonkeyLearnException <EOL> class MonkeyLearnResponse ( object ) : <EOL> def __init__ ( self , result , responses ) : <EOL> self . result = result <EOL> self . query_limit_remaining = responses [ - <NUM_LIT:1> ] . headers [ '<STR_LIT>' ] <EOL> self . raw_responses = responses <EOL> class SleepRequestsMixin ( object ) : <EOL> def make_request ( self , url , method , data = None , sleep_if_throttled = True ) : <EOL> while True : <EOL> if data : <EOL> response = requests . request ( <EOL> method , <EOL> url , <EOL> data = json . dumps ( data ) , <EOL> headers = { <EOL> '<STR_LIT>' : '<STR_LIT>' + self . token , <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' <EOL> } <EOL> ) <EOL> else : <EOL> response = requests . request ( <EOL> method , <EOL> url , <EOL> headers = { <EOL> '<STR_LIT>' : '<STR_LIT>' + self . token , <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' <EOL> } <EOL> ) <EOL> response_json = response . json ( ) <EOL> if sleep_if_throttled and response . status_code == <NUM_LIT> and '<STR_LIT>' in response_json [ '<STR_LIT>' ] : <EOL> seconds = re . findall ( r'<STR_LIT>' , response_json [ '<STR_LIT>' ] ) [ <NUM_LIT:0> ] <EOL> time . sleep ( int ( seconds ) ) <EOL> continue <EOL> elif ( sleep_if_throttled and response . status_code == <NUM_LIT> and <EOL> '<STR_LIT>' in response_json [ '<STR_LIT>' ] ) : <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> continue <EOL> return response <EOL> class HandleErrorsMixin ( object ) : <EOL> def check_batch_limits ( self , text_list , batch_size ) : <EOL> if batch_size > MAX_BATCH_SIZE or batch_size < MIN_BATCH_SIZE : <EOL> raise MonkeyLearnException ( '<STR_LIT>' . format ( <EOL> MIN_BATCH_SIZE , MAX_BATCH_SIZE ) ) <EOL> if not text_list : <EOL> raise MonkeyLearnException ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in text_list : <EOL> raise MonkeyLearnException ( '<STR_LIT>' . format ( <EOL> text_list . index ( '<STR_LIT>' ) ) ) <EOL> for i , text in enumerate ( text_list ) : <EOL> if not isinstance ( text , six . string_types ) : <EOL> raise MonkeyLearnException ( <EOL> '<STR_LIT>' . format ( i ) <EOL> ) <EOL> def handle_errors ( self , response ) : <EOL> if not response . ok : <EOL> try : <EOL> res = response . json ( ) <EOL> except ValueError : <EOL> response . raise_for_status ( ) <EOL> raise MonkeyLearnException ( json . dumps ( res [ '<STR_LIT>' ] ) ) </s>
