1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#
4#  Copyright 2014 Google Inc. All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#      http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
18"""Simple command-line sample for Google Coordinate.
19
20Pulls a list of jobs, creates a job and marks a job complete for a given
21Coordinate team. Client IDs for installed applications are created in the
22Google API Console. See the documentation for more information:
23
24   https://developers.google.com/console/help/#WhatIsKey
25
26Usage:
27  $ python coordinate.py -t teamId
28
29You can also get help on all the command-line flags the program understands
30by running:
31
32  $ python coordinate.py --help
33
34To get detailed log output run:
35
36  $ python coordinate.py -t teamId --logging_level=DEBUG
37"""
38from __future__ import print_function
39
40__author__ = '[email protected] (Zach Newell)'
41
42import argparse
43import pprint
44import sys
45
46from oauth2client import client
47from googleapiclient import sample_tools
48from googleapiclient.discovery import build
49from googleapiclient.discovery import http
50
51# Declare command-line flags.
52argparser = argparse.ArgumentParser(add_help=False)
53argparser.add_argument('teamId', help='Coordinate Team ID')
54
55
56def main(argv):
57  # Authenticate and construct service.
58  service, flags = sample_tools.init(
59      argv, 'coordinate', 'v1', __doc__, __file__, parents=[argparser],
60      scope='https://www.googleapis.com/auth/coordinate')
61
62  service = build('coordinate', 'v1', http=http)
63
64  try:
65    # List all the jobs for a team
66    jobs_result = service.jobs().list(teamId=flags.teamId).execute(http=http)
67
68    print('List of Jobs:')
69    pprint.pprint(jobs_result)
70
71    # Multiline note
72    note = """
73    These are notes...
74    on different lines
75    """
76
77    # Insert a job and store the results
78    insert_result = service.jobs().insert(body='',
79      title='Google Campus',
80      teamId=flags.teamId,
81      address='1600 Amphitheatre Parkway Mountain View, CA 94043',
82      lat='37.422120',
83      lng='122.084429',
84      assignee=None,
85      note=note).execute()
86
87    pprint.pprint(insert_result)
88
89    # Close the job
90    update_result = service.jobs().update(body='',
91      teamId=flags.teamId,
92      jobId=insert_result['id'],
93      progress='COMPLETED').execute()
94
95    pprint.pprint(update_result)
96
97  except client.AccessTokenRefreshError as e:
98    print ('The credentials have been revoked or expired, please re-run'
99      'the application to re-authorize')
100
101
102if __name__ == '__main__':
103  main(sys.argv)
104