用于python的moesif api库
moesifapi的Python项目详细描述
python的moesifapi库
这个sdk使用了请求库,并将用于python 2.7-3.5。
如果您使用django作为平台,我们有moesifapi-python中间件,您可以直接使用该中间件。
查看moesif的Developer Documentation和Python API Reference以了解更多信息
如何安装:
pip install moesifapi
使用方法:
代码使用名为requests、jsonpickle和dateutil的python包。 在解决了依赖关系之后,您可以按照以下步骤轻松地使用sdk。
from__future__importprint_functionfrommoesifapi.moesif_api_clientimport*frommoesifapi.modelsimport*client=MoesifAPIClient(my_application_id)api_client=client.api# Note: we recommend sending all API Calls via MVC framework middleware.req_headers=APIHelper.json_deserialize(""" { "Host": "api.acmeinc.com", "Accept": "*/*", "Connection": "Keep-Alive", "User-Agent": "Dalvik/2.1.0 (Linux; U; Android 5.0.2; C6906 Build/14.5.A.0.242)", "Content-Type": "application/json", "Content-Length": "126", "Accept-Encoding": "gzip"} """)req_body=APIHelper.json_deserialize("""{ "items": [ { "type": 1, "id": "fwfrf" }, { "type": 2, "id": "d43d3f" } ]}""")rsp_headers=APIHelper.json_deserialize(""" { "Date": "Tue, 23 Aug 2016 23:46:49 GMT", "Vary": "Accept-Encoding", "Pragma": "no-cache", "Expires": "-1", "Content-Type": "application/json; charset=utf-8" "Cache-Control": "no-cache" } """)rsp_body=APIHelper.json_deserialize("""{ "Error": "InvalidArgumentException", "Message": "Missing field field_a" }""")metadata=APIHelper.json_deserialize("""{ "field1": "foo", "field2": "bar" }""")event_req=EventRequestModel(time="2016-09-09T04:45:42.914",uri="https://api.acmeinc.com/items/reviews/",verb="PATCH",api_version="1.1.0",ip_address="61.48.220.123",headers=req_headers,body=req_body)event_rsp=EventResponseModel(time="2016-09-09T04:45:42.914",status=500,headers=rsp_headers,body=rsp_body)event_model=EventModel(request=event_req,response=event_rsp,user_id="my_user_id",company_id="my_company_id",session_token="23jdf0owekfmcn4u3qypxg09w4d8ayrcdx8nu2ng]s98y18cx98q3yhwmnhcfx43f",metadata=metadata)# Perform the API call through the SDK functionapi_client.create_event(event_model)api_client.create_event(my_api_event_model)
更新用户
api还允许您使用自定义元数据更新用户配置文件。 用户id是必需字段,所有其他字段都是可选的。
metadata=APIHelper.json_deserialize(""" { "email": "pythonapiuser@email.com", "name": "pythonapiuser", "custom": "testdata" } """)user_model=UserModel(user_id='12345',company_id='67890',modified_time=datetime.utcnow(),metadata=metadata)# Perform the API call through the SDK functionapi_client.update_user(user_model)
更新公司
api还允许您使用自定义元数据更新公司信息。 公司ID是必需字段,所有其他字段都是可选的。
metadata=APIHelper.json_deserialize(""" { "email": "pythonapiuser@email.com", "name": "pythonapiuser", "location": "United States" } """)company_model=CompanyModel(company_id='67890',modified_time=datetime.utcnow(),metadata=metadata)# Perform the API call through the SDK functionself.controller.update_company(company_model)
如何测试:
您可以使用自动生成的测试测试sdk 案例。unittest作为测试框架,nose作为测试 跑步者。您可以按如下方式运行测试:
- 手动克隆git repo
- 从terminal/cmd导航到sdk的根目录。
- 调用“pip install-r requirements.txt”
- 将您自己的应用程序ID添加到“测试/控制器/控制器测试库”
- 调用“notests tests/controllers/test_api_controller.py”