Skip to content

Commit b79c737

Browse files
committed
Adding Python DFA Reporting API Samples.
Reviewed in https://codereview.appspot.com/26070044/.
1 parent 89d832a commit b79c737

18 files changed

+963
-0
lines changed

.hgignore

+3
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ google_api_python_client.egg-info/*
1919
dist/*
2020
snapshot/*
2121
MANIFEST
22+
.project
23+
.pydevproject
24+
.settings/*

samples/dfareporting/README

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
A collection of command-line samples for the DFA Reporting REST API.
2+
3+
api: dfareporting
4+
keywords: cmdline
5+
author: Jonathon Imperiosi ([email protected])
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"installed": {
3+
"client_id": "[[INSERT CLIENT ID HERE]]",
4+
"client_secret": "[[INSERT CLIENT SECRET HERE]]",
5+
"redirect_uris": [],
6+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
7+
"token_uri": "https://accounts.google.com/o/oauth2/token"
8+
}
9+
}

samples/dfareporting/create_report.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/python
2+
#
3+
# Copyright 2013 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""This example illustrates how to create a report.
18+
19+
Tags: reports.insert
20+
"""
21+
22+
__author__ = ('[email protected] (Jonathon Imperiosi)')
23+
24+
import argparse
25+
import pprint
26+
import sys
27+
28+
from apiclient import sample_tools
29+
from oauth2client import client
30+
31+
# Declare command-line flags.
32+
argparser = argparse.ArgumentParser(add_help=False)
33+
argparser.add_argument('profile_id', type=int,
34+
help='The ID of the profile to create a report for')
35+
36+
37+
def main(argv):
38+
# Authenticate and construct service.
39+
service, flags = sample_tools.init(
40+
argv, 'dfareporting', 'v1.3', __doc__, __file__, parents=[argparser],
41+
scope='https://www.googleapis.com/auth/dfareporting')
42+
43+
profile_id = flags.profile_id
44+
45+
try:
46+
# Create a new report resource to insert
47+
report = {
48+
'name': 'Example Standard Report',
49+
'type': 'STANDARD',
50+
'criteria': {
51+
'dateRange': {'relativeDateRange': 'YESTERDAY'},
52+
'dimensions': [{'name': 'dfa:campaign'}],
53+
'metricNames': ['dfa:clicks']
54+
}
55+
}
56+
57+
# Construct the request.
58+
request = service.reports().insert(profileId=profile_id, body=report)
59+
60+
# Execute request and print response.
61+
pprint.pprint(request.execute())
62+
except client.AccessTokenRefreshError:
63+
print ('The credentials have been revoked or expired, please re-run the '
64+
'application to re-authorize')
65+
66+
if __name__ == '__main__':
67+
main(sys.argv)

samples/dfareporting/delete_report.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/python
2+
#
3+
# Copyright 2013 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""This example illustrates how to delete a report.
18+
19+
Tags: reports.delete
20+
"""
21+
22+
__author__ = ('[email protected] (Jonathon Imperiosi)')
23+
24+
import argparse
25+
import pprint
26+
import sys
27+
28+
from apiclient import sample_tools
29+
from oauth2client import client
30+
31+
# Declare command-line flags.
32+
argparser = argparse.ArgumentParser(add_help=False)
33+
argparser.add_argument('profile_id', type=int,
34+
help='The ID of the profile to delete a report for')
35+
argparser.add_argument('report_id', type=int,
36+
help='The ID of the report to delete')
37+
38+
39+
def main(argv):
40+
# Authenticate and construct service.
41+
service, flags = sample_tools.init(
42+
argv, 'dfareporting', 'v1.3', __doc__, __file__, parents=[argparser],
43+
scope='https://www.googleapis.com/auth/dfareporting')
44+
45+
profile_id = flags.profile_id
46+
report_id = flags.report_id
47+
48+
try:
49+
# Construct the request.
50+
request = service.reports().delete(profileId=profile_id, reportId=report_id)
51+
52+
# Execute request and print response.
53+
pprint.pprint(request.execute())
54+
except client.AccessTokenRefreshError:
55+
print ('The credentials have been revoked or expired, please re-run the '
56+
'application to re-authorize')
57+
58+
if __name__ == '__main__':
59+
main(sys.argv)

samples/dfareporting/download_file.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/python
2+
#
3+
# Copyright 2013 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""This example illustrates how to download a file.
18+
19+
Tags: files.get
20+
"""
21+
22+
__author__ = ('[email protected] (Jonathon Imperiosi)')
23+
24+
import argparse
25+
import sys
26+
27+
from apiclient import sample_tools
28+
from oauth2client import client
29+
30+
# Declare command-line flags.
31+
argparser = argparse.ArgumentParser(add_help=False)
32+
argparser.add_argument('report_id', type=int,
33+
help='The ID of the report to get a file for')
34+
argparser.add_argument('file_id', type=int,
35+
help='The ID of the file to get')
36+
37+
38+
def main(argv):
39+
# Authenticate and construct service.
40+
service, flags = sample_tools.init(
41+
argv, 'dfareporting', 'v1.3', __doc__, __file__, parents=[argparser],
42+
scope='https://www.googleapis.com/auth/dfareporting')
43+
44+
report_id = flags.report_id
45+
file_id = flags.file_id
46+
47+
try:
48+
# Construct the request.
49+
request = service.files().get_media(reportId=report_id, fileId=file_id)
50+
51+
# Execute request and print the file contents
52+
print request.execute()
53+
except client.AccessTokenRefreshError:
54+
print ('The credentials have been revoked or expired, please re-run the '
55+
'application to re-authorize')
56+
57+
if __name__ == '__main__':
58+
main(sys.argv)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/python
2+
#
3+
# Copyright 2013 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""This example illustrates how to get all dimension values for a dimension.
18+
19+
Tags: dimensionValues.query
20+
"""
21+
22+
__author__ = ('[email protected] (Jonathon Imperiosi)')
23+
24+
import argparse
25+
import pprint
26+
import sys
27+
28+
from apiclient import sample_tools
29+
from oauth2client import client
30+
31+
# Declare command-line flags.
32+
argparser = argparse.ArgumentParser(add_help=False)
33+
argparser.add_argument('profile_id', type=int,
34+
help='The ID of the profile to get a report for')
35+
36+
def main(argv):
37+
# Authenticate and construct service.
38+
service, flags = sample_tools.init(
39+
argv, 'dfareporting', 'v1.3', __doc__, __file__, parents=[argparser],
40+
scope='https://www.googleapis.com/auth/dfareporting')
41+
42+
profile_id = flags.profile_id
43+
44+
try:
45+
# Create the dimension to query
46+
dimension = {
47+
'dimensionName': 'dfa:advertiser',
48+
'startDate': '2013-01-01',
49+
'endDate': '2013-12-31'
50+
}
51+
52+
# Construct the request.
53+
request = service.dimensionValues().query(profileId=profile_id,
54+
body=dimension)
55+
56+
while request is not None:
57+
# Execute request and print response.
58+
response = request.execute()
59+
pprint.pprint(response)
60+
61+
nextPageToken = response.get('nextPageToken')
62+
63+
if nextPageToken and nextPageToken != '0':
64+
request = service.dimensionValues().query_next(request, response)
65+
else:
66+
request = None
67+
except client.AccessTokenRefreshError:
68+
print ('The credentials have been revoked or expired, please re-run the '
69+
'application to re-authorize')
70+
71+
if __name__ == '__main__':
72+
main(sys.argv)

samples/dfareporting/get_all_files.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/python
2+
#
3+
# Copyright 2013 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""This example illustrates how to get a list of all the files for a profile.
18+
19+
Tags: files.list
20+
"""
21+
22+
__author__ = ('[email protected] (Jonathon Imperiosi)')
23+
24+
import argparse
25+
import pprint
26+
import sys
27+
28+
from apiclient import sample_tools
29+
from oauth2client import client
30+
31+
# Declare command-line flags.
32+
argparser = argparse.ArgumentParser(add_help=False)
33+
argparser.add_argument('profile_id', type=int,
34+
help='The ID of the profile to use')
35+
36+
37+
def main(argv):
38+
# Authenticate and construct service.
39+
service, flags = sample_tools.init(
40+
argv, 'dfareporting', 'v1.3', __doc__, __file__, parents=[argparser],
41+
scope='https://www.googleapis.com/auth/dfareporting')
42+
43+
profile_id = flags.profile_id
44+
45+
try:
46+
# Construct a get request for the specified profile.
47+
request = service.files().list(profileId=profile_id, maxResults=10)
48+
49+
while request is not None:
50+
# Execute request and print response.
51+
response = request.execute()
52+
pprint.pprint(response)
53+
54+
nextPageToken = response.get('nextPageToken')
55+
56+
if nextPageToken:
57+
request = service.files().list_next(request, response)
58+
else:
59+
request = None
60+
except client.AccessTokenRefreshError:
61+
print ('The credentials have been revoked or expired, please re-run the '
62+
'application to re-authorize')
63+
64+
if __name__ == '__main__':
65+
main(sys.argv)

0 commit comments

Comments
 (0)