在不使用Marketplace的情况下,如何使用API Gateway进行货币化?

0

【以下的问题经过翻译处理】 想知道你们中是否有人实现了使用API网关托管的API的货币化,而没有使用AWS SaaS Marketplace选项?如果有,你们使用了哪些支付网关,采取了什么方法?

profile picture
专家
已提问 2 年前44 查看次数
1 回答
0

【以下的回答经过翻译处理】 API商业化的主要思想是创建支持它的基础设施。有两种方法可以解决这个问题:

  1. 快速本地方式(使用Data Exchange,产品链接, 数据提供者文档链接)
  2. Building blocks方式

Building blocks方式

如果有可能实现Building blocks架构,那么就存在很大的灵活性,可以通过AWS实现与API-Monetization的本地集成,而无需管理基础架构。让我们将其分解成不同的主题来解决:

参考体系结构在这里

  1. 开发人员门户

对于开发人员门户,在无服务器应用程序框架(Github, SAR)中有几个部署。这个解决方案非常完整,使用户能够创建一个单一的地方来管理公共和私有端点,还可以管理不同API的版本以及最终用户如何注册。


  1. 用户门户/API商业化门户

现在这部分取决于客户要求或规定,因为在这里用户将看到他们的费用/用法,并能够支付,因为不同的客户根据地区而有所不同。可以使用API Gateway构建它,并参考DynamoDB表来跟踪API的使用情况,最后与本地/区域电子支付提供商集成。DynamoDB api 使用表示例为:

user-id;
timestamp;
body; cost;
email; 
httMethod;
Isbase64; 
path;
QueryStrings

DynamoDB api 价格表示例为:

path; 
method; 
price; 
stage

如果您想捕获 API 上的价格变化/报价,需要注意的重要事项是捕获每次 API 调用的成本,所以如果价格随时间变化,就可以更轻松地跟踪每个用户的使用情况。 这是有关如何使用 lambda 跟踪用户使用情况的示例代码:

import json
import boto3
import datetime

def lambda_handler(event, context):
    headers = {'headers': {'Access-Control-Allow-Headers':'Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token', 'Access-Control-Allow-Origin': '*'}}
    response_info = api_prices()
    prices = collapse_prices(response_info['Items'])
    dynamodb = boto3.client('dynamodb')
    email = event['requestContext']['authorizer']['claims']['email']
    name = event['requestContext']['authorizer']['claims']['name']
    username = event['requestContext']['authorizer']['claims']['cognito:username']
    event_copy = dict(event)
    
    # Deleting cognito token before storing it into dynamo
    del event_copy['headers']['Authorization']
    del event_copy['multiValueHeaders']['Authorization']
    
    # Storing request into dynamo
    response = dynamodb.put_item(
        Item={
            'user-id': {'S': username,},
            'timestamp': {'S': str(datetime.datetime.now().timestamp())},
            'httpMethod':{'S': event['httpMethod']},
            'path':{'S': event['path']},
            'resource':{ 'S': event['path']},
            "body":{'S': json.dumps(event['body'])},
            "isBase64Encoded":{'S': str(event['isBase64Encoded'])},
            "queryStringParameters":{'S': json.dumps(event['queryStringParameters'])},
            "multiValueQueryStringParameters":{'S': json.dumps(event['multiValueQueryStringParameters'])},
            "email":{'S': email},
            "name":{'S': name},
            'cost':{'S': str(float(prices["{0}{1}".format(event['httpMethod'],event['path'])]))}
        },
        TableName='OpenAPI-table',
    )
    
    # Queue message to the caller sqs
    sqs = boto3.client('sqs')
    response = sqs.send_message(
        QueueUrl='https://sqs.us-east-1.amazonaws.com/9999999999/OpenAPI-queue',
        MessageBody=json.dumps(
            {
                'httpMethod':event['httpMethod'],
                'path':event['path'],
                'resource':event['path'],
                "body": event['body'],
                "isBase64Encoded": event['isBase64Encoded'],
                "queryStringParameters": event['queryStringParameters'],
                "multiValueQueryStringParameters": event['multiValueQueryStringParameters'],
                "email": email,
                "name": name,
            })
        )
    return {
        'headers' : headers['headers'],
        'statusCode': 200,
        'body': json.dumps({'message':"Your request is queued",'message_id':response['MessageId']})
    }
def api_prices():
    dynamodb = boto3.resource('dynamodb')
    table_info = dynamodb.Table('Open-API-info')
    return table_info.scan()
def collapse_prices(prices):
    return {"{0}{1}".format(price['method'],price['path']):price['price'] for price in prices}

4.后端和业务支持最后允许业务跟踪他们的 API 行为。使用 AWS Glue + AWS Athena + AWS。Quicksight 将允许客户创建自定义 BI 门户以关注他们的收入。也感谢查询的可能性 DyanmoDB 直接与 Athena 联合查询。 DynamoDB(Github,SAR) 使用 QuickSight Spice 避免价格上涨查询 AWS DynamoDB 并且仅在定义 BI 周期后才将数据导入 QuickSight 也是一个好主意。

profile picture
专家
已回答 2 年前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则