用于与dayforceapi进行类型化交互的python客户机。
dayforce-client的Python项目详细描述
dayforce客户端
dayforceclient是一个pythonsdk,用于与dayforcesrestapi和SFTP服务器接口。在
安装
$ pip3 install dayforce-client
基本用法(Rest API)
dayforcesrestapi的主接口是Dayforce
类。您可以通过提供一些身份验证和配置参数来实例化Dayforce
类:
Dayforce
类上的所有get_
方法都将返回一个DayforceReponse
对象。DayforceReponse
对象包含用于进行调用的Dayforce
实例、请求中使用的参数以及从API接收的响应:
resp=df.get_employee_details(xrefcode='12345')print(resp.client)print(type(resp.client))print(resp.params)print(type(resp.resp))print(resp.resp.url)print(resp.resp.status_code)print(resp.resp.ok)print(resp.resp.elapsed)...Dayforce(username='your-username',client_namespace='your-client-namespace',dayforce_release=57,api_version='V1',url='https://usr57-services.dayforcehcm.com/Api/your-client-namespace/V1')<class'dayforce_client.client.Dayforce'>{}<class'requests.models.Response'>https://usr57-services.dayforcehcm.com/Api/your-client-namespace/V1/Employees/12345200True0:00:00.550066
查询参数
某些方法还接受将用作请求查询参数的关键字参数:
resp=df.get_employee_details(xrefcode='12345',expand='WorkAssignments')print(resp.resp.url)
输出:
https://usr57-services.dayforcehcm.com/Api/your-client-namespace/V1/Employees/12345?expand=WorkAssignments
访问响应内容
可以使用.get()
语法访问响应内容:
resp=df.get_employee_details(xrefcode='12345')print(resp.get("Data"))
分页
还可以选择使用迭代语法对响应进行分页:
forpageindf.get_employee_raw_punches():forraw_punchinpage.get("Data"):print(raw_punch)
产出资源记录
或者,您可以使用DayforceResponse
.yield_records()
方法来处理响应分页和yieling资源记录。该方法将对响应进行分页并遍历响应内容,以生成给定资源和相应的DayforceResponse
实例的单个资源记录:
forpage,employeeindf.get_employees().yield_records():print(employee)print(page)...{'XRefCode':'12345'}DayforceResponse(client=Dayforce(username='your-username',client_namespace='your-client-namespace',dayforce_release=57,api_version='V1',url='https://usr57-services.dayforcehcm.com/Api/your-client-namespace/V1'),params={},resp=<Response[200]>){'XRefCode':'67891'}DayforceResponse(client=Dayforce(username='your-username',client_namespace='your-client-namespace',dayforce_release=57,api_version='V1',url='https://usr57-services.dayforcehcm.com/Api/your-client-namespace/V1'),params={},resp=<Response[200]>)
基本用法(SFTP客户端)
client=DayforceSFTP(hostname='foo01.dayforcehcm.com',username='mycompany',password='sekret',disable_host_key_checking=True,)client.connect()
{12>但是不鼓励使用
ssh-keyscan foo01.dayforcehcm.com
# foo01.dayforcehcm.com:22 SSH-2.0-9.99 FlowSsh: Bitvise SSH Server (WinSSHD)
foo01.dayforcehcm.com ssh-rsa AAAAB3...snip...XYZ
client=DayforceSFTP(hostname='foo01.dayforcehcm.com',username='mycompany',password='sekret',host_key='AAAAB3...snip...XYZ',)client.connect()
批量导入
提供通过dayp强制导入数据的功能。这个客户机封装了这个流程的大部分业务逻辑,并公开了一个简单的高级API。在
fromdayforceimportDayforceSFTP,ImportError,ImportPendingclient=DayforceSFTP(hostname=...)withclient.connect():batch_token=client.put_import('./local/path/to/batch-20191225T000000.csv',type='KpiTargetImport')whileTrue:try:client.raise_for_import_status(batch_token)print('batch import succeeded')breakexceptImportPendinge:sleep10continueexceptImportErrore:print('batch import failed')break
发展
python3 -m venv install .venv source .venv/bin/activate make dev_install make test
- 项目
标签: