|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. |
| 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 | + * https://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 | +public class Execute { |
| 18 | + // [START apps_script_execute] |
| 19 | + /** |
| 20 | + * Create a HttpRequestInitializer from the given one, except set |
| 21 | + * the HTTP read timeout to be longer than the default (to allow |
| 22 | + * called scripts time to execute). |
| 23 | + * |
| 24 | + * @param {HttpRequestInitializer} requestInitializer the initializer |
| 25 | + * to copy and adjust; typically a Credential object. |
| 26 | + * @return an initializer with an extended read timeout. |
| 27 | + */ |
| 28 | + private static HttpRequestInitializer setHttpTimeout( |
| 29 | + final HttpRequestInitializer requestInitializer) { |
| 30 | + return new HttpRequestInitializer() { |
| 31 | + @Override |
| 32 | + public void initialize(HttpRequest httpRequest) throws IOException { |
| 33 | + requestInitializer.initialize(httpRequest); |
| 34 | + // This allows the API to call (and avoid timing out on) |
| 35 | + // functions that take up to 6 minutes to complete (the maximum |
| 36 | + // allowed script run time), plus a little overhead. |
| 37 | + httpRequest.setReadTimeout(380000); |
| 38 | + } |
| 39 | + }; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Build and return an authorized Script client service. |
| 44 | + * |
| 45 | + * @param {Credential} credential an authorized Credential object |
| 46 | + * @return an authorized Script client service |
| 47 | + */ |
| 48 | + public static Script getScriptService() throws IOException { |
| 49 | + Credential credential = authorize(); |
| 50 | + return new Script.Builder( |
| 51 | + HTTP_TRANSPORT, JSON_FACTORY, setHttpTimeout(credential)) |
| 52 | + .setApplicationName(APPLICATION_NAME) |
| 53 | + .build(); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Interpret an error response returned by the API and return a String |
| 58 | + * summary. |
| 59 | + * |
| 60 | + * @param {Operation} op the Operation returning an error response |
| 61 | + * @return summary of error response, or null if Operation returned no |
| 62 | + * error |
| 63 | + */ |
| 64 | + public static String getScriptError(Operation op) { |
| 65 | + if (op.getError() == null) { |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + // Extract the first (and only) set of error details and cast as a Map. |
| 70 | + // The values of this map are the script's 'errorMessage' and |
| 71 | + // 'errorType', and an array of stack trace elements (which also need to |
| 72 | + // be cast as Maps). |
| 73 | + Map<String, Object> detail = op.getError().getDetails().get(0); |
| 74 | + List<Map<String, Object>> stacktrace = |
| 75 | + (List<Map<String, Object>>)detail.get("scriptStackTraceElements"); |
| 76 | + |
| 77 | + java.lang.StringBuilder sb = |
| 78 | + new StringBuilder("\nScript error message: "); |
| 79 | + sb.append(detail.get("errorMessage")); |
| 80 | + sb.append("\nScript error type: "); |
| 81 | + sb.append(detail.get("errorType")); |
| 82 | + |
| 83 | + if (stacktrace != null) { |
| 84 | + // There may not be a stacktrace if the script didn't start |
| 85 | + // executing. |
| 86 | + sb.append("\nScript error stacktrace:"); |
| 87 | + for (Map<String, Object> elem : stacktrace) { |
| 88 | + sb.append("\n "); |
| 89 | + sb.append(elem.get("function")); |
| 90 | + sb.append(":"); |
| 91 | + sb.append(elem.get("lineNumber")); |
| 92 | + } |
| 93 | + } |
| 94 | + sb.append("\n"); |
| 95 | + return sb.toString(); |
| 96 | + } |
| 97 | + |
| 98 | + public static void main(String[] args) throws IOException { |
| 99 | + // ID of the script to call. Acquire this from the Apps Script editor, |
| 100 | + // under Publish > Deploy as API executable. |
| 101 | + String scriptId = "ENTER_YOUR_SCRIPT_ID_HERE"; |
| 102 | + Script service = getScriptService(); |
| 103 | + |
| 104 | + // Create an execution request object. |
| 105 | + ExecutionRequest request = new ExecutionRequest() |
| 106 | + .setFunction("getFoldersUnderRoot"); |
| 107 | + |
| 108 | + try { |
| 109 | + // Make the API request. |
| 110 | + Operation op = |
| 111 | + service.scripts().run(scriptId, request).execute(); |
| 112 | + |
| 113 | + // Print results of request. |
| 114 | + if (op.getError() != null) { |
| 115 | + // The API executed, but the script returned an error. |
| 116 | + System.out.println(getScriptError(op)); |
| 117 | + } else { |
| 118 | + // The result provided by the API needs to be cast into |
| 119 | + // the correct type, based upon what types the Apps |
| 120 | + // Script function returns. Here, the function returns |
| 121 | + // an Apps Script Object with String keys and values, |
| 122 | + // so must be cast into a Java Map (folderSet). |
| 123 | + Map<String, String> folderSet = |
| 124 | + (Map<String, String>)(op.getResponse().get("result")); |
| 125 | + if (folderSet.size() == 0) { |
| 126 | + System.out.println("No folders returned!"); |
| 127 | + } else { |
| 128 | + System.out.println("Folders under your root folder:"); |
| 129 | + for (String id: folderSet.keySet()) { |
| 130 | + System.out.printf( |
| 131 | + "\t%s (%s)\n", folderSet.get(id), id); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + } catch (GoogleJsonResponseException e) { |
| 136 | + // The API encountered a problem before the script was called. |
| 137 | + e.printStackTrace(System.out); |
| 138 | + } |
| 139 | + } |
| 140 | + // [END apps_script_execute] |
| 141 | +} |
0 commit comments