def charge_api(): if request.method == 'POST': # grab json from request body json = request.json # check if all keys present, return error if any missing result = check_json(json, ['email', 'stripe_token', 'description', 'amount']) if result: return error_response("missing {}".format(result)) # add stripe api key stripe.api_key = stripe_keys['secret_key'] try: # create customer ID customer = stripe.Customer.create(email=json['email'], source=json['stripe_token']) # create charge charge = stripe.Charge.create(customer=customer.id, amount=json['amount'], currency='usd', description=json['description']) # jsonify'd 200 success return success_response(charge) except stripe.error.StripeError as e: # jsonify'd 500 error return error_response(e)
def get_trending_topics(project): args = request.get_json() if args is None: args = {} num_topics = args.get('num_topics', 10) pc = ProjectConfig() project_config = pc.get_config_by_slug(project) if project_config is None: return error_response(400, 'No project found with this slug') if not project_config['compile_trending_topics']: return error_response(400, 'This project is configured to not collect trending topic information.') tt = TrendingTopics(project) try: resp = tt.get_trending_topics(num_topics) except: return jsonify([]) return jsonify(resp)
def get_trending_tweets(project): args = request.get_json() if args is None: args = {} num_tweets = args.get('num_tweets', 10) min_score = args.get('min_score', 5) sample_from = args.get('sample_from', 100) query = args.get('query', '') pc = ProjectConfig() project_config = pc.get_config_by_slug(project) if project_config is None: return error_response(400, 'No project found with this slug') if not project_config['compile_trending_tweets']: return error_response(400, 'This project is configured to not collect trending tweets information.') tt = TrendingTweets(project, es_index_name=project_config['es_index_name']) resp = tt.get_trending_tweets(num_tweets, query=query, sample_from=sample_from, min_score=min_score) return jsonify(resp)
def wrapper(*args, **kwargs): for param in args: if isinstance(param, Request): request = param sport = request.data.get('sport', '') if sport == '': sport = request.GET.get('sport', '') if sport != '': try: str_sport = SPORTS[int(sport)] except: return Response(error_response('Sport %s doesnt exist.' % sport), status=status.HTTP_400_BAD_REQUEST) else: return Response(error_response('Sport cannot be empty.'), status=status.HTTP_400_BAD_REQUEST) return f(*args, **kwargs)
def wrapper(*args, **kwargs): for param in args: if isinstance(param, Request): request = param key_provided = request.GET.get('api_key', '') logging.debug('api key %s' % key_provided) if key_provided != getattr(settings, 'LANDING_KEY'): return Response(error_response('Api key needed.'), status=status.HTTP_400_BAD_REQUEST) return f(*args, **kwargs)
def predict(): reqparse.add_argument('text', type=str, required=True) reqparse.add_argument('model_endpoint', type=str, required=True) reqparse.add_argument('project', type=str, required=False) args = reqparse.parse_args() resp = sagemaker.predict(args.model_endpoint, {'text': args.text}) status_code = resp['ResponseMetadata']['HTTPStatusCode'] if status_code != 200: logger.error(resp) return error_response(status_code, 'Prediction unsuccessful.', error_type=resp['Error']['Code']) prediction = json.loads(resp['Body'].read()) return jsonify(prediction)
def endpoint_config(): reqparse.add_argument('model_endpoint', type=str, required=True) args = reqparse.parse_args() resp = sagemaker.predict(args.model_endpoint, { 'text': 'this is just a test', 'include_run_config': True }) status_code = resp['ResponseMetadata']['HTTPStatusCode'] if status_code != 200: logger.error(resp) return error_response(status_code, 'Getting config unsuccessful.', error_type=resp['Error']['Code']) prediction = json.loads(resp['Body'].read()) config = prediction['run_config'] return jsonify(config)
def endpoint_labels(): reqparse.add_argument('model_endpoint', type=str, required=True) args = reqparse.parse_args() resp = sagemaker.predict(args.model_endpoint, {'text': 'this is just a test'}) status_code = resp['ResponseMetadata']['HTTPStatusCode'] if status_code != 200: logger.error(resp) return error_response(status_code, 'Getting endpoint labels unsuccessful.', error_type=resp['Error']['Code']) prediction = json.loads(resp['Body'].read()) labels = prediction['predictions'][0]['labels_fixed'] label_vals = Predict.labels_to_int(labels) label_obj = {'labels': labels} if label_vals is not None: label_obj = {**label_obj, 'label_vals': label_vals} return jsonify(label_obj)
def manage_config(): logger = logging.getLogger('pipeline') config = request.get_json() pc = ProjectConfig() if request.method == 'GET': # read streaming config config = pc.read() return jsonify(config), 200 else: # write streaming config # make sure new configuration is valid is_valid, msg = pc.is_valid(config) if not is_valid: return error_response(400, msg) # write everything to config pc.write(config) # Create new Elasticsearch indices if needed es.update_es_indices(pc.get_es_index_names(config)) return success_response(200, 'Successfully updated configuration files. Make sure to restart stream for changes to be active.')