การจัดการแหล่งข้อมูล

Fitness REST API ช่วยให้คุณสร้าง รับ และอัปเดตแหล่งข้อมูลได้ แหล่งข้อมูลคือแหล่งข้อมูลที่ไม่ซ้ํากัน คุณใช้แหล่งข้อมูลเพื่อแทรกข้อมูลการออกกําลังกายลงใน Store ฟิตเนส และสามารถดึงข้อมูลการออกกําลังกายที่แทรกโดยแหล่งข้อมูลเฉพาะได้

แหล่งข้อมูลจะแสดงด้วยทรัพยากร Users.dataSources

สร้างแหล่งข้อมูล

ตัวอย่างนี้สาธิตวิธีสร้างแหล่งข้อมูลใหม่ชื่อ "MyDataSource" และจะเพิ่มจํานวนขั้นตอน

เมธอด HTTP
โพสต์
URL คำขอ
https://www.googleapis.com/fitness/v1/users/me/dataSources
เนื้อหาของคำขอ
{
  "dataStreamName": "MyDataSource",
  "type": "derived",
  "application": {
    "detailsUrl": "http://example.com",
    "name": "Foo Example App",
    "version": "1"
  },
  "dataType": {
    "field": [
      {
        "name": "steps",
        "format": "integer"
      }
    ],
    "name": "com.google.step_count.delta"
  },
  "device": {
    "manufacturer": "Example Manufacturer",
    "model": "ExampleTablet",
    "type": "tablet",
    "uid": "1000001",
    "version": "1.0"
  }
}

คำตอบ

หากสร้างแหล่งข้อมูลเรียบร้อยแล้ว การตอบกลับจะเป็นรหัสสถานะ 200 OK เนื้อหาการตอบกลับจะมีการนําเสนอ JSON ของแหล่งข้อมูล รวมถึงพร็อพเพอร์ตี้ datasource.dataStreamId ที่คุณจะใช้เป็นรหัสแหล่งข้อมูลสําหรับคําขอที่ตามมาได้

คําสั่ง Curl
$ curl --header "Authorization: Bearer ya29.yourtokenvalue" -X POST \
--header "Content-Type: application/json;encoding=utf-8" -d @createds.json \
"https://www.googleapis.com/fitness/v1/users/me/dataSources"

รับข้อมูลที่เฉพาะเจาะจง

ตัวอย่างนี้สาธิตวิธีเรียกแหล่งข้อมูล ("MyDataSource") ที่คุณสร้างไว้ในตัวอย่างก่อนหน้านี้ เมื่อสร้างแหล่งข้อมูลใหม่ dataStreamId จะมีตัวระบุที่ไม่ซ้ํากัน (แสดงเป็น "1234567890" ในตัวอย่างเหล่านี้) หมายเลขนี้คือหมายเลขโปรเจ็กต์นักพัฒนาซอฟต์แวร์ ซึ่งจะเหมือนกันสําหรับคําขอทั้งหมดที่ใช้บัญชีนักพัฒนาแอปที่เฉพาะเจาะจงดังกล่าว อย่าลืมใช้ dataStreamId จากแหล่งข้อมูลที่คุณสร้าง

เมธอด HTTP
รับ
URL คำขอ
https://www.googleapis.com/fitness/v1/users/me/dataSources/derived:com.google.step_count.delta:1234567890:Example%20Manufacturer:ExampleTablet:1000001:MyDataSource
เนื้อหาของคำขอ
ไม่มี
คำตอบ
หากมีแหล่งข้อมูลอยู่ การตอบกลับจะเป็นรหัสสถานะ 200 OK เนื้อหาการตอบกลับจะแสดงการนําเสนอข้อมูล JSON ของแหล่งข้อมูล
คําสั่ง Curl
$ curl --header "Authorization: Bearer ya29.yourtokenvalue" -X GET 
--header "Content-Type: application/json;encoding=utf-8"
"https://www.googleapis.com/fitness/v1/users/me/dataSources/derived:com.google.step_count.delta:1234567890:Example%20Manufacturer:ExampleTablet:1000001:MyDataSource"

รับข้อมูลรวม

ตัวอย่างนี้สาธิตวิธีค้นหาแหล่งข้อมูลที่เฉพาะเจาะจงสําหรับข้อมูลที่รวบรวม ในกรณีนี้คือ estimated_steps ซึ่งเป็นแหล่งข้อมูลที่ใช้เพื่อแสดงจํานวนก้าวในแอป Google Fit โปรดทราบว่าการประทับเวลาในตัวของคําขอ JSON จะอยู่ในหน่วยมิลลิวินาที

เมธอด HTTP
โพสต์
URL คำขอ
https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate
เนื้อหาของคำขอ
{
  "aggregateBy": [{
    "dataSourceId":
      "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps"
  }],
  "bucketByTime": { "durationMillis": 86400000 },
  "startTimeMillis": 1454284800000,
  "endTimeMillis": 1455062400000
}

คำตอบ

หากมีแหล่งข้อมูลอยู่ การตอบกลับจะเป็นรหัสสถานะ 200 OK เนื้อหาการตอบกลับจะแสดงการนําเสนอข้อมูล JSON ของแหล่งข้อมูล

คําสั่ง Curl
$ curl --header "Authorization: Bearer ya29.yourtokenvalue" -X POST \
--header "Content-Type: application/json;encoding=utf-8" -d @aggregate.json \
"https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate"

อัปเดตแหล่งข้อมูล

ตัวอย่างนี้สาธิตวิธีการอัปเดตชื่อและเวอร์ชันของอุปกรณ์สําหรับแหล่งข้อมูล

เมธอด HTTP
PUT
URL คำขอ
https://www.googleapis.com/fitness/v1/users/me/dataSources/derived:com.google.step_count.delta:1234567890:Example%20Manufacturer:ExampleTablet:1000001:MyDataSource
เนื้อหาของคำขอ
{
  "dataStreamId": "derived:com.google.step_count.delta:1234567890:Example Manufacturer:ExampleTablet:1000001:MyDataSource",
  "dataStreamName": "MyDataSource",
  "type": "derived",
  "application": {
    "detailsUrl": "http://example.com",
    "name": "Foo Example App",
    "version": "1"
  },
  "dataType": {
    "field": [
      {
        "name": "steps",
        "format": "integer"
      }
    ],
    "name": "com.google.step_count.delta"
  },
  "device": {
    "manufacturer": "Example Manufacturer",
    "model": "ExampleTablet",
    "type": "tablet",
    "uid": "1000001",
    "version": "2.0"
  }
}

คำตอบ

หากอัปเดตแหล่งข้อมูลเรียบร้อยแล้ว การตอบกลับจะเป็นรหัสสถานะ 200 OK เนื้อหาการตอบกลับจะมีการนําเสนอ JSON ของแหล่งข้อมูล

คําสั่ง Curl
$ curl --header "Authorization: Bearer ya29.yourtokenvalue" -X PUT \
--header "Content-Type: application/json;encoding=utf-8" -d @updateds.json \
"https://www.googleapis.com/fitness/v1/users/me/dataSources/derived:com.google.step_count.delta:1234567890:Example%20Manufacturer:ExampleTablet:1000001:MyDataSource"

ลบแหล่งข้อมูล

ตัวอย่างนี้สาธิตวิธีการลบแหล่งข้อมูล

เมธอด HTTP
ลบ
URL คำขอ
https://www.googleapis.com/fitness/v1/users/me/dataSources/derived:com.google.step_count.delta:1234567890:Example%20Manufacturer:ExampleTablet:1000001:MyDataSource
เนื้อหาของคำขอ
ไม่มี
คำตอบ
หากลบแหล่งข้อมูลเรียบร้อยแล้ว การตอบกลับจะเป็นรหัสสถานะ 200 OK เนื้อหาการตอบกลับมีการนําเสนอ JSON ของแหล่งข้อมูลที่ลบ
คําสั่ง Curl
$ curl --header "Authorization: Bearer ya29.yourtokenvalue" -X DELETE \
--header "Content-Type: application/json;encoding=utf-8" \
"https://www.googleapis.com/fitness/v1/users/me/dataSources/derived:com.google.step_count.delta:1234567890:Example%20Manufacturer:ExampleTablet:1000001:MyDataSource"