Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(136)

Delta Between Two Patch Sets: samples/analytics/sample_utils.py

Issue 5494058: Adding py sample for google analytics core reporting api (Closed)
Left Patch Set: fixed minor typo Created 13 years, 1 month ago
Right Patch Set: more typo fixes Created 13 years, 1 month ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « samples/analytics/management_v3_reference.py ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 #!/usr/bin/python 1 #!/usr/bin/python
2 # 2 #
3 # Copyright 2012 Google Inc. All Rights Reserved. 3 # Copyright 2012 Google Inc. All Rights Reserved.
4 # 4 #
5 # Licensed under the Apache License, Version 2.0 (the "License"); 5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with 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 7 # You may obtain a copy of the License at
8 # 8 #
9 # http://www.apache.org/licenses/LICENSE-2.0 9 # http://www.apache.org/licenses/LICENSE-2.0
10 # 10 #
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 'Set the level of logging detail.') 80 'Set the level of logging detail.')
81 81
82 82
83 # Name of file that will store the access and refresh tokens to access 83 # Name of file that will store the access and refresh tokens to access
84 # the API without having to login each time. Make sure this file is in 84 # the API without having to login each time. Make sure this file is in
85 # a secure place. 85 # a secure place.
86 TOKEN_FILE_NAME = 'analytics.dat' 86 TOKEN_FILE_NAME = 'analytics.dat'
87 87
88 88
89 def process_flags(argv): 89 def process_flags(argv):
90 """Uses the command-line flags to set the logging level.""" 90 """Uses the command-line flags to set the logging level.
jcgregorio_google 2012/03/06 15:01:59 document parameter
api.nickm1 2012/04/26 06:30:16 Done.
91
92 Args:
93 argv: List of command line arguments passed to the python script.
94 """
91 95
92 # Let the gflags module process the command-line arguments. 96 # Let the gflags module process the command-line arguments.
93 try: 97 try:
94 argv = FLAGS(argv) 98 argv = FLAGS(argv)
95 except gflags.FlagsError, e: 99 except gflags.FlagsError, e:
96 print '%s\nUsage: %s ARGS\n%s' % (e, argv[0], FLAGS) 100 print '%s\nUsage: %s ARGS\n%s' % (e, argv[0], FLAGS)
97 sys.exit(1) 101 sys.exit(1)
98 102
99 # Set the logging according to the command-line flag. 103 # Set the logging according to the command-line flag.
100 logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level)) 104 logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level))
101 105
102 106
103 def prepare_credentials(): 107 def initialize_service():
jcgregorio_google 2012/03/06 15:01:59 Seems gratuitous to break out prepare_credentials
api.nickm1 2012/04/26 06:30:16 I decided to remove them. On 2012/03/06 15:01:59,
104 """Handles auth. Reuses credentialss if available or runs the auth flow.""" 108 """Returns an instance of service from discovery data and does auth.
105 109
106 # If the credentials don't exist or are invalid run through the native client 110 This method tries to read any existing OAuth 2.0 credentials from the
107 # flow. The Storage object will ensure that if successful the good 111 Storage object. If the credentials do not exist, new credentials are
108 # Credentials will get written back to a file. 112 obtained. The crdentials are used to authorize an http object. The
109 storage = Storage(TOKEN_FILE_NAME) 113 http object is used to build the analytics service object.
110 credentials = storage.get()
111 if credentials is None or credentials.invalid:
112 credentials = run(FLOW, storage)
113 return credentials
114 114
115 115 Returns:
116 def retrieve_service(http): 116 An analytics v3 service object.
117 """Retrieves an Anaytics API service via the discovery service.""" 117 """
118
119 # Construct a service object via the discovery service.
120 return build('analytics', 'v3', http=http)
121
122
123 def initialize_service():
124 """Builds instance of service from discovery data and does auth."""
jcgregorio_google 2012/03/06 15:01:59 document return value
api.nickm1 2012/04/26 06:30:16 Done.
125 118
126 # Create an httplib2.Http object to handle our HTTP requests. 119 # Create an httplib2.Http object to handle our HTTP requests.
127 http = httplib2.Http() 120 http = httplib2.Http()
128 121
129 # Prepare credentials, and authorize HTTP object with them. 122 # Prepare credentials, and authorize HTTP object with them.
130 credentials = prepare_credentials() 123 storage = Storage(TOKEN_FILE_NAME)
124 credentials = storage.get()
125 if credentials is None or credentials.invalid:
126 credentials = run(FLOW, storage)
127
131 http = credentials.authorize(http) 128 http = credentials.authorize(http)
132 129
133 # Retrieve service. 130 # Retrieve service.
134 return retrieve_service(http) 131 return build('analytics', 'v3', http=http)
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b