Skip to content

Commit 3cd6507

Browse files
authored
Merge pull request googleworkspace#222 from googleworkspace/form-api-py-watches
Checkin of Forms API Python Watches samples
2 parents 3dc8f66 + 8a90958 commit 3cd6507

4 files changed

Lines changed: 170 additions & 0 deletions

File tree

forms/snippets/create_watch.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START forms_create_watch]
16+
from __future__ import print_function
17+
from apiclient import discovery
18+
from httplib2 import Http
19+
from oauth2client import client
20+
from oauth2client import file
21+
from oauth2client import tools
22+
23+
SCOPES = "https://www.googleapis.com/auth/drive"
24+
DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1beta&key=<YOUR_API_KEY>&labels=FORMS_BETA_TESTERS"
25+
26+
store = file.Storage('credentials.json')
27+
creds = None
28+
if not creds or creds.invalid:
29+
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
30+
creds = tools.run_flow(flow, store)
31+
32+
service = discovery.build('forms', 'v1beta', http=creds.authorize(
33+
Http()), discoveryServiceUrl=DISCOVERY_DOC, static_discovery=False)
34+
35+
watch = {
36+
"watch": {
37+
"target": {
38+
"topic": {
39+
"topicName": "<YOUR_TOPIC_PATH>"
40+
}
41+
},
42+
"eventType": "RESPONSES"
43+
}
44+
}
45+
46+
form_id = '<YOUR_FORM_ID>'
47+
48+
# Print JSON response after form watch creation
49+
result = service.forms().watches().create(formId=form_id,body=watch).execute()
50+
print(result)
51+
# [END forms_create_watch]

forms/snippets/delete_watch.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START forms_delete_watch]
16+
from __future__ import print_function
17+
from apiclient import discovery
18+
from httplib2 import Http
19+
from oauth2client import client
20+
from oauth2client import file
21+
from oauth2client import tools
22+
23+
SCOPES = "https://www.googleapis.com/auth/drive"
24+
DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1beta&key=<YOUR_API_KEY>&labels=FORMS_BETA_TESTERS"
25+
26+
store = file.Storage('credentials.json')
27+
creds = None
28+
if not creds or creds.invalid:
29+
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
30+
creds = tools.run_flow(flow, store)
31+
service = discovery.build('forms', 'v1beta', http=creds.authorize(
32+
Http()), discoveryServiceUrl=DISCOVERY_DOC, static_discovery=False)
33+
34+
form_id = '<YOUR_FORM_ID>'
35+
watch_id = '<YOUR_WATCH_ID>'
36+
37+
# Print JSON response after deleting a form watch
38+
result = service.forms().watches().delete(formId=form_id,watchId=watch_id).execute()
39+
print(result)
40+
# [END forms_delete_watch]

forms/snippets/list_watches.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START forms_list_form_watches]
16+
from __future__ import print_function
17+
from apiclient import discovery
18+
from httplib2 import Http
19+
from oauth2client import client
20+
from oauth2client import file
21+
from oauth2client import tools
22+
23+
SCOPES = "https://www.googleapis.com/auth/drive"
24+
DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1beta&key=<YOUR_API_KEY>&labels=FORMS_BETA_TESTERS"
25+
26+
store = file.Storage('credentials.json')
27+
creds = None
28+
if not creds or creds.invalid:
29+
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
30+
creds = tools.run_flow(flow, store)
31+
service = discovery.build('forms', 'v1beta', http=creds.authorize(
32+
Http()), discoveryServiceUrl=DISCOVERY_DOC, static_discovery=False)
33+
34+
form_id = '<YOUR_FORM_ID>'
35+
36+
# Print JSON list of form watches
37+
result = service.forms().watches().list(formId=form_id).execute()
38+
print(result)
39+
# [END forms_list_form_watches]

forms/snippets/renew_watch.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START forms_renew_watch]
16+
from __future__ import print_function
17+
from apiclient import discovery
18+
from httplib2 import Http
19+
from oauth2client import client
20+
from oauth2client import file
21+
from oauth2client import tools
22+
23+
SCOPES = "https://www.googleapis.com/auth/drive"
24+
DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1beta&key=<YOUR_API_KEY>&labels=FORMS_BETA_TESTERS"
25+
26+
store = file.Storage('credentials.json')
27+
creds = None
28+
if not creds or creds.invalid:
29+
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
30+
creds = tools.run_flow(flow, store)
31+
service = discovery.build('forms', 'v1beta', http=creds.authorize(
32+
Http()), discoveryServiceUrl=DISCOVERY_DOC, static_discovery=False)
33+
34+
form_id = '<YOUR_FORM_ID>'
35+
watch_id = '<YOUR_WATCH_ID>'
36+
37+
# Print JSON response after renewing a form watch
38+
result = service.forms().watches().renew(formId=form_id,watchId=watch_id).execute()
39+
print(result)
40+
# [END forms_renew_watch]

0 commit comments

Comments
 (0)