<commit_before>'''
Python module for Yummly API: https://developer.yummly.com
'''

import requests

# Yummly API: https://developer.yummly.com

# API URLs
URL_BASE    = 'http://api.yummly.com/v1'
URL_GET     = URL_BASE + '/api/recipe/'
URL_SEARCH  = URL_BASE + '/api/recipes'

# API auth properties which should be set externally
api_id  = None
api_key = None

# basic request config options
# @note: have found that Yummly's API "hangs" so it might be a good idea to have some reasonable timeout and handle appropriately
timeout = 5.0

class YummlyError( Exception ):
    '''Exception class for Yummly errors'''
    pass

### Methods for API get recipe and search recipes

def recipe( recipe_id ):
    url = URL_GET + recipe_id
    response    = _request( url )
    result      = _extract_response( response )
    return result

def search( q, limit=40, offset=0 ):
    '''
    Prepares yummly search API request

    :param q: search string
    :param limit: max results
    :param offset: pagination offset in # of records (e.g. offset=5 means skip first 5 results)

    '''

    url     = URL_SEARCH
    params  = {
        'q':            q,
        'maxResult':    limit,
        'start':        offset
    }

    response    = _request( url, params=params )
    results     = _extract_response( response )
    return results


### Helper functions

def handle_errors( fn ):
    '''Decorator for handling Yummly errors'''
    def handle( *args, **kargs ):
        response = fn( *args, **kargs )

        status = response.status_code

        if status != 200:
            if status == 409:
                raise YummlyError( 'API id and/or key are invalid or API rate limit exceeded' )
            else:
                # error ocurred which may not be documented
                response.raise_for_status()<commit_msg>Rename search function args `limit` and `offset` to `maxResult` and `start` to correspond to yummly api parameter name.<commit_after>'''
Python module for Yummly API: https://developer.yummly.com
'''

import requests

# Yummly API: https://developer.yummly.com

# API URLs
URL_BASE    = 'http://api.yummly.com/v1'
URL_GET     = URL_BASE + '/api/recipe/'
URL_SEARCH  = URL_BASE + '/api/recipes'

# API auth properties which should be set externally
api_id  = None
api_key = None

# basic request config options
# @note: have found that Yummly's API "hangs" so it might be a good idea to have some reasonable timeout and handle appropriately
timeout = 5.0

class YummlyError( Exception ):
    '''Exception class for Yummly errors'''
    pass

### Methods for API get recipe and search recipes

def recipe( recipe_id ):
    url = URL_GET + recipe_id
    response    = _request( url )
    result      = _extract_response( response )
    return result

def search( q, maxResult=40, start=0, **params ):
    '''
    Prepares yummly search API request

    :param q: search string
    :param maxResult: max results
    :param start: pagination offset in # of records (e.g. offset=5 means skip first 5 results)
    :param **params: optional kargs corresponding to Yummly supported search parameters

    '''

    url     = URL_SEARCH

    # copy params to leave source unmodified
    params  = params.copy()
    params.update({
        'q':            q,
        'maxResult':    maxResult,
        'start':        start
    })

    response    = _request( url, params=params )
    results     = _extract_response( response )
    return results


### Helper functions

def handle_errors( fn ):
    '''Decorator for handling Yummly errors'''
    def handle( *args, **kargs ):
        response = fn( *args, **kargs )

        status = response.status_code

        if status != 200:
            if status == 409:
                raise YummlyError( 'API id and/or key are invalid or API rate limit exceeded' )
            else:
                # error ocurred which may not be documented
                response.raise_for_status()
{'fraction_non_alphanumeric': 0.09300557526609224, 'fraction_numerical': 0.007095793208312215, 'mean_word_length': 2.5654923215898826, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, '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': '68454', 'n_tokens_mistral': 1192, 'n_tokens_neox': 1134, 'n_words': 417}