Skip to content

Commit

Permalink
Code upload - 20190521 (#2)
Browse files Browse the repository at this point in the history
* Initial commit

* Add testing
  • Loading branch information
jamesliupenn authored May 21, 2019
1 parent 617a2ec commit fcbbbe1
Show file tree
Hide file tree
Showing 12 changed files with 795 additions and 0 deletions.
3 changes: 3 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include README*
include *.txt
include config.json
175 changes: 175 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
# Onfleet Python Wrapper
*Read this document in another language: [English](https://github.com/onfleet/pyonfleet/blob/master/README.md), [正體中文](https://github.com/onfleet/pyonfleet/blob/master/README.zh-tw.md)*

If you have any questions, please reach out to Onfleet by submitting an issue [here](https://github.com/onfleet/pyonfleet/issues) or contact [email protected]

## Table of Contents
- [Onfleet Python Wrapper](#onfleet-python-wrapper)
* [Synopsis](#synopsis)
* [Installation](#installation)
* [Usage](#usage)
+ [Throttling](#throttling)
+ [Responses](#responses)
+ [Supported CRUD Operations](#supported-crud-operations)
- [GET Requests](#get-requests)
* [Examples of get()](#examples-of-get--)
* [Examples of get()](#examples-of-get--)
- [POST Requests](#post-requests)
* [Examples of create()](#examples-of-create--)
- [PUT Requests](#put-requests)
* [Examples of update()](#examples-of-update--)
- [DELETE Requests](#delete-requests)
* [Examples of deleteOne()](#examples-of-deleteone--)
+ [Examples of utilizing your CRUD operations](#examples-of-utilizing-your-crud-operations)

## Synopsis

The Onfleet Python library provides convenient access to the Onfleet API.

## Installation

```
pip install python_onfleet
```
## Usage
Before using the API wrapper, you will need to obtain an API key from your organization admin. Creation and integration of API keys are performed through the [Onfleet dashboard](https://onfleet.com/dashboard#/manage).

To authenticate, you will also need to create a file named `.auth.json` under your working directory, this is where you will store your API credentials.

The format of `.auth.json` is shown below:
```json
{
"API_KEY": "<your_api_key>",
"API_SECRET": "<your_api_secret>" // Not supported now, empty string "" will suffice
}
```
You can also opt in to not store your API key here and use it on the fly instead.

Once the Onfleet object is created, you will get access to all the API endpoints as documented in the [Onfleet API documentation](http://docs.onfleet.com/). Here are some usage case:
```python
from onfleet import Onfleet

api = Onfleet() # if .auth.json was provided
api = Onfleet(api_key="<your_api_key>") # if no .auth.json was provided
```

### Throttling
Rate limiting is enforced by the API with a threshold of 20 requests per second across all your organization's API keys, learn more about it [here](http://docs.onfleet.com/docs/throttling).

### Responses
The `pyonfleet` API wrapper returns a [Response object](https://2.python-requests.org//en/master/api/#requests.Response), where headers and other information are stored. To access the results of the response, simply parse it as JSON using the [json() method](https://developer.mozilla.org/en-US/docs/Web/API/Body/json).

### Supported CRUD Operations
The base URL for the Onfleet API is `https://onfleet.com/api/v2`, here are the supported CRUD operations for each endpoint:

| | GET | POST | PUT | DELETE |
|:------------:|:---------------------------------------------------------------:|:----------------------------------------------------------------------:|:------------------------------------:|:-------------:|
| [Admins](http://docs.onfleet.com/docs/administrators) | get() | create(body) | update(id, body) | deleteOne(id) |
| [Containers](http://docs.onfleet.com/docs/containers) | get(workers=id), get(teams=id), get(organizations=id) | x | update(id, body) | x |
| [Destinations](http://docs.onfleet.com/docs/destinations) | get(id) | create(body) | x | x |
| [Hubs](http://docs.onfleet.com/docs/hubs) | get() | x | x | x |
| [Organization](http://docs.onfleet.com/docs/organizations) | get(), get(id) | x | insertTask(id, body) | x |
| [Recipients](http://docs.onfleet.com/docs/recipients) | get(id), get(name), get(phone) | create(body) | update(id, body) | x |
| [Tasks](http://docs.onfleet.com/docs/tasks) | get(queryParams), get(id), get(shortId) | create(body), clone(id), forceComplete(id), batch(body), autoAssign(body) | update(id, body) | deleteOne(id) |
| [Teams](http://docs.onfleet.com/docs/teams) | get(), get(id) | create(body) | update(id, body), insertTask(id, body) | deleteOne(id) |
| [Webhooks](http://docs.onfleet.com/docs/webhooks) | get() | create(body) | x | deleteOne(id) |
| [Workers](http://docs.onfleet.com/docs/workers) | get(), get(queryParams), get(id), getByLocation(queryParams), getSchedule(id) | create(body), setSchedule(id, body) | update(id, body), insertTask(id, body) | deleteOne(id) |

#### GET Requests
To get all the documents within an endpoint:
```python
get()
```
##### Examples of get()
```python
api.workers.get()
api.workers.get(queryParams="")
```
Option to use query parameters for some certain endpoints, refer back to API documents for endpoints that support query parameters:
```python
api.workers.get(queryParams="phones=<phone_number>")

or

api.workers.get(queryParams={"phones":"<phone_number>"})
```

To get one of the document within an endpoint, specify the param that you wish to search by:
```python
get(param=<some_param>)
```

##### Examples of get()
```python
api.workers.get(id="<24_digit_id>")
api.workers.get(id="<24_digit_id>", queryParams={"analytics": "true"})
api.tasks.get(shortId="<shortId>")
api.recipients.get(phone="<phone_number>")
api.recipients.get(name="<recipient_name>")

api.containers.get(workers="<worker_id>")
api.containers.get(teams="<team_id>")
api.containers.get(organizations="<org_id>")
```

##### Special endpoints - getByLocation using JSON query parameters:
```python
params = {"longitude":"-122.4","latitude":"37.7601983","radius":"6000"}
api.workers.getByLocation(queryParams=params)
```

#### POST Requests
To create a document within an endpoint:
```python
create(body="<body_object>")
```
##### Examples of create()
```python
driver = {
"name": "A Swartz Test",
"phone": "+16173428853",
"teams": ["<a_team_id>", "<a_team_id> (optional)..."],
"vehicle": {
"type": "CAR",
"description": "Tesla Model S",
"licensePlate": "FKNS9A",
"color": "purple",
}
}

api.workers.create(body=driver)
```
Extended POST requests include `clone`, `forceComplete`, `batchCreate`, `autoAssign` on the tasks endpoint, and `setSchedule` on the workers endpoint:

```python
api.tasks.clone(id="<24_digit_id>")
api.tasks.forceComplete(id="<24_digit_id>", body="<completion_details>")
api.tasks.batchCreate(body="<task_object_get>")
api.tasks.autoAssign(body="<auto_assign_object>")

api.workers.setSchedule(id="<24_digit_id>", body="<schedule_object>")
```
For more details, check our documentation on [clone](http://docs.onfleet.com/docs/tasks#clone-task), [forceComplete](http://docs.onfleet.com/docs/tasks#complete-task), [batchCreate](http://docs.onfleet.com/docs/tasks#create-tasks-in-batch), [autoAssign](http://docs.onfleet.com/docs/tasks#automatically-assign-get-of-tasks), and [setSchedule](http://docs.onfleet.com/docs/workers#set-workers-schedule).

#### PUT Requests
To update a document within an endpoint:
```python
update(id="<24_digit_id>", body="<body_object>")
```
##### Examples of update()
```python
updateBody = {
"name": "New Driver Name",
}
api.workers.update(id="<24_digit_id>", body=updateBody)
```

#### DELETE Requests
To delete a document within an endpoint:
```python
deleteOne(id="<24_digit_id>")
```
##### Examples of deleteOne()
```python
api.workers.deleteOne(id="<24_digit_id>")
```
172 changes: 172 additions & 0 deletions README.zh-tw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
# Onfleet Python Wrapper
*其他語言版本: [English](https://github.com/onfleet/pyonfleet/blob/master/README.md),[正體中文](https://github.com/onfleet/pyonfleet/blob/master/README.zh-tw.md)*

如果對於Onfleet應用程式介面或是我們產品有任何的問題,歡迎在此留言或直接聯繫 [email protected]

## 目錄
- [Onfleet Python Wrapper](#onfleet-python-wrapper)
* [概要](#概要)
* [安裝](#安裝)
* [使用守則](#使用守則)
+ [API速限](#API速限)
+ [請求回應](#請求回應)
+ [支援的CRUD操作](#支援的CRUD操作)
- [GET 請求](#get-請求)
* [展示所有資源的範例](#展示所有資源的範例)
* [展示指定資源的範例](#展示指定資源的範例)
- [POST 請求](#post-請求)
* [提交指定資源的範例](#提交指定資源的範例)
- [PUT 請求](#put-請求)
* [取代指定資源的範例](#取代指定資源的範例)
- [DELETE 請求](#delete-請求)
* [刪除指定資源的範例](#刪除指定資源的範例)


## 概要

`python_onfleet` 提供一個快速又便捷的方式,以獲取Onfleet應用程式介面內的資料。

## 安裝

```
pip install python_onfleet
```

## 使用守則
在使用Onfleet應用程式介面之前,請先索取應用程式介面金鑰。創建應用程式介面金鑰的詳情,請洽[Onfleet官方網站]((https://onfleet.com/dashboard#/manage)。

將您的金鑰取代下面的api_key參數即可開始使用:
在開始使用之前,請先在工作資料夾內創建一個檔案,並命名為`.auth.json`,內容存入您的金鑰參數。

`.auth.json`的格式如下:
```json
{
"API_KEY": "<你的API金鑰>",
"API_SECRET": "<你的API密碼>" //這部分目前沒有支援,設定為空白字串""
}
```
當Onfleet物件成功被創建,而金鑰又是合法的,您會獲得訪問以下各endpoint資源的函式。欲獲得各endpoint資源的定義,請洽[Onfleet官方應用程式介面文件](http://docs.onfleet.com/)

假如您不想儲存金鑰至檔案內,您亦可以直接引入API金鑰做為一參數:

```python
from onfleet import Onfleet

api = Onfleet() #有.auth.json檔案的前提
api = Onfleet(api_key="<your_api_key>") #直接引入參數
```
### API速限
原則上API的速限為每秒鐘20次請求,詳情請參考[官方文件](http://docs.onfleet.com/docs/throttling)

### 請求回應
`pyonfleet`所回應的物件為一[Response物件](https://2.python-requests.org//en/master/api/#requests.Response),在此物件中,許多的附帶訊息用於提供給client端更多資訊。若欲取得回應的數據,請在回應的物件中執行[json()](https://developer.mozilla.org/en-US/docs/Web/API/Body/json)即可獲得JSON化的請求結果。

### 支援的CRUD操作
Onfleet應用程式介面的基本URL為 `https://onfleet.com/api/v2`,下面為各endpoint所支援的函式列表:

| | GET | POST | PUT | DELETE |
|:------------:|:---------------------------------------------------------------:|:----------------------------------------------------------------------:|:------------------------------------:|:-------------:|
| [Admins](http://docs.onfleet.com/docs/administrators) | get() | create(body) | update(id, body) | deleteOne(id) |
| [Containers](http://docs.onfleet.com/docs/containers) | get(workers=id), get(teams=id), get(organizations=id) | x | update(id, body) | x |
| [Destinations](http://docs.onfleet.com/docs/destinations) | get(id) | create(body) | x | x |
| [Hubs](http://docs.onfleet.com/docs/hubs) | get() | x | x | x |
| [Organization](http://docs.onfleet.com/docs/organizations) | get(), get(id) | x | insertTask(id, body) | x |
| [Recipients](http://docs.onfleet.com/docs/recipients) | get(id), get(name), get(phone) | create(body) | update(id, body) | x |
| [Tasks](http://docs.onfleet.com/docs/tasks) | get(queryParams), get(id), get(shortId) | create(body), clone(id), forceComplete(id), batch(body), autoAssign(body) | update(id, body) | deleteOne(id) |
| [Teams](http://docs.onfleet.com/docs/teams) | get(), get(id) | create(body) | update(id, body), insertTask(id, body) | deleteOne(id) |
| [Webhooks](http://docs.onfleet.com/docs/webhooks) | get() | create(body) | x | deleteOne(id) |
| [Workers](http://docs.onfleet.com/docs/workers) | get(), get(queryParams), get(id), getByLocation(queryParams), getSchedule(id) | create(body), setSchedule(id, body) | update(id, body), insertTask(id, body) | deleteOne(id) |

#### GET 請求
展示所有資源的指令如下:
```python
get()
```
##### 展示所有資源的範例
```python
api.workers.get()
api.workers.get(queryParams="")
```
部分的endpoint有支援queryParam(查詢參數),詳情請參考Onfleet官方文件:
```python
api.workers.get(queryParams="phones=<phone_number>")
```

展示指定資源的指令如下,根據欲展示的資源參數取代`param`則會根據參數做展示:
```python
get(param=<some_param>)
```

##### 展示指定資源的範例
```python
api.workers.get(id="<24_digit_id>")
api.workers.get(id="<24_digit_id>", queryParams={"analytics": "true"})
api.tasks.get(shortId="<shortId>")
api.recipients.get(phone="<phone_number>")
api.recipients.get(name="<recipient_name>")

api.containers.get(workers="<worker_id>")
api.containers.get(teams="<team_id>")
api.containers.get(organizations="<org_id>")
```

#### POST 請求
提交某單一指定資源的指令如下:
```python
create(body="<body_object>")
```
##### 提交指定資源的範例
```python
driver = {
"name": "A Swartz Test",
"phone": "617-342-8853",
"teams": ["W*8bF5jY11Rk05E0bXBHiGg2"],
"vehicle": {
"type": "CAR",
"description": "Tesla Model S",
"licensePlate": "FKNS9A",
"color": "purple",
}
}

api.workers.create(body=driver)
```
其他延伸的POST請求包含了tasks節點上的`clone`, `forceComplete`, `batchCreate`, `autoAssign`,以及workers節點上的`setSchedule`

```python
api.tasks.clone(id="<24_digit_id>")
api.tasks.forceComplete(id="<24_digit_id>", body="<completion_details>")
api.tasks.batchCreate(body="<task_object_get>")
api.tasks.autoAssign(body="<auto_assign_object>")

api.workers.setSchedule(id="<24_digit_id>", body="<schedule_object>")
```
參考資料:[clone](http://docs.onfleet.com/docs/tasks#clone-task), [forceComplete](http://docs.onfleet.com/docs/tasks#complete-task), [batchCreate](http://docs.onfleet.com/docs/tasks#create-tasks-in-batch), [autoAssign](http://docs.onfleet.com/docs/tasks#automatically-assign-get-of-tasks)以及[setSchedule](http://docs.onfleet.com/docs/workers#set-workers-schedule).

#### PUT 請求
取代(更新)某單一指定資源的指令如下:
```python
update(id="<24_digit_id>", body="<body_object>")
```
##### 取代指定資源的範例
```python
updateBody = {
"name": "New Driver Name",
}
api.workers.update(id="<24_digit_id>", body=updateBody)
```
其他延伸的PUT請求包含了updateSchedule:
```python
api.workers.updateSchedule(id="<24_digit_id>", body=newSchedule)
```
參考資料:[updateSchedule](http://docs.onfleet.com/docs/workers#update-workers-schedule)

#### DELETE 請求
刪除某單一指定資源的指令如下:
```python
deleteOne(id="<24_digit_id>")
```
##### 刪除指定資源的範例
```python
api.workers.deleteOne(id="<24_digit_id>")
```
Loading

0 comments on commit fcbbbe1

Please sign in to comment.