Left: | ||
Right: |
OLD | NEW |
---|---|
1 // Copyright 2011 Google Inc. All rights reserved. | 1 // Copyright 2011 Google Inc. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package main | 5 package main |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "encoding/json" | 9 "encoding/json" |
10 "errors" | 10 "errors" |
(...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
481 } | 481 } |
482 | 482 |
483 for _, meth := range a.APIMethods() { | 483 for _, meth := range a.APIMethods() { |
484 meth.generateCode() | 484 meth.generateCode() |
485 } | 485 } |
486 | 486 |
487 for _, res := range reslist { | 487 for _, res := range reslist { |
488 res.generateMethods() | 488 res.generateMethods() |
489 } | 489 } |
490 | 490 |
491 pn("\nfunc cleanPathString(s string) string { return strings.Map(func(r rune) rune { if r >= 0x2d && r <= 0x7a || r == '~' { return r }; return -1 }, s) }") | |
492 return nil | 491 return nil |
493 } | 492 } |
494 | 493 |
495 func (a *API) generateScopeConstants() { | 494 func (a *API) generateScopeConstants() { |
496 auth := jobj(a.m, "auth") | 495 auth := jobj(a.m, "auth") |
497 if auth == nil { | 496 if auth == nil { |
498 return | 497 return |
499 } | 498 } |
500 oauth2 := jobj(auth, "oauth2") | 499 oauth2 := jobj(auth, "oauth2") |
501 if oauth2 == nil { | 500 if oauth2 == nil { |
(...skipping 628 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1130 p("urls := googleapi.ResolveRelative(%q, %q)\n", a.apiBaseURL(), jstr(me th.m, "path")) | 1129 p("urls := googleapi.ResolveRelative(%q, %q)\n", a.apiBaseURL(), jstr(me th.m, "path")) |
1131 if meth.supportsMedia() { | 1130 if meth.supportsMedia() { |
1132 pn("if c.media_ != nil {") | 1131 pn("if c.media_ != nil {") |
1133 // Hack guess, since we get a 404 otherwise: | 1132 // Hack guess, since we get a 404 otherwise: |
1134 //pn("urls = googleapi.ResolveRelative(%q, %q)", a.apiBaseURL(), meth.mediaPath()) | 1133 //pn("urls = googleapi.ResolveRelative(%q, %q)", a.apiBaseURL(), meth.mediaPath()) |
1135 // Further hack. Discovery doc is wrong? | 1134 // Further hack. Discovery doc is wrong? |
1136 pn("urls = strings.Replace(urls, %q, %q, 1)", "https://www.googl eapis.com/", "https://www.googleapis.com/upload/") | 1135 pn("urls = strings.Replace(urls, %q, %q, 1)", "https://www.googl eapis.com/", "https://www.googleapis.com/upload/") |
1137 pn(`params.Set("uploadType", "multipart")`) | 1136 pn(`params.Set("uploadType", "multipart")`) |
1138 pn("}") | 1137 pn("}") |
1139 } | 1138 } |
1140 for _, arg := range args.forLocation("path") { | |
1141 p("\turls = strings.Replace(urls, \"{%s}\", %s, 1)\n", arg.apina me, arg.cleanExpr("c.")) | |
1142 } | |
1143 pn("urls += \"?\" + params.Encode()") | 1139 pn("urls += \"?\" + params.Encode()") |
1144 if meth.supportsMedia() { | 1140 if meth.supportsMedia() { |
1145 if !hasContentType { // Support mediaUpload but no ctype set. | 1141 if !hasContentType { // Support mediaUpload but no ctype set. |
1146 pn("body = new(bytes.Buffer)") | 1142 pn("body = new(bytes.Buffer)") |
1147 pn(`ctype := "application/json"`) | 1143 pn(`ctype := "application/json"`) |
1148 hasContentType = true | 1144 hasContentType = true |
1149 } | 1145 } |
1150 pn("contentLength_, hasMedia_ := googleapi.ConditionallyIncludeM edia(c.media_, &body, &ctype)") | 1146 pn("contentLength_, hasMedia_ := googleapi.ConditionallyIncludeM edia(c.media_, &body, &ctype)") |
1151 } | 1147 } |
1152 pn("req, _ := http.NewRequest(%q, urls, body)", jstr(meth.m, "httpMethod ")) | 1148 pn("req, _ := http.NewRequest(%q, urls, body)", jstr(meth.m, "httpMethod ")) |
1149 // Replace param values after NewRequest to avoid reencoding them. | |
1150 // E.g. Cloud Storage API requires '%2F' in entity param to be kept, but url.Parse replaces it by '/'. | |
adg
2013/05/22 23:28:55
s/by/with/
francesc
2013/05/22 23:39:14
Done.
| |
1151 for _, arg := range args.forLocation("path") { | |
1152 p("\treq.URL.Path = strings.Replace(req.URL.Path, \"{%s}\", %s, 1)\n", arg.apiname, arg.cleanExpr("c.")) | |
adg
2013/05/22 23:28:55
use pn(`req.URL ...`, ...)
francesc
2013/05/22 23:39:14
Done.
| |
1153 } | |
1154 // Set opaque to avoid encoding of the parameters in the URL path. | |
1155 p("\treq.URL.Opaque = req.URL.Path\n") | |
adg
2013/05/22 23:28:55
pn()
francesc
2013/05/22 23:39:14
Done.
| |
1156 | |
1153 if meth.supportsMedia() { | 1157 if meth.supportsMedia() { |
1154 pn("if hasMedia_ { req.ContentLength = contentLength_ }") | 1158 pn("if hasMedia_ { req.ContentLength = contentLength_ }") |
1155 } | 1159 } |
1156 if hasContentType { | 1160 if hasContentType { |
1157 pn(`req.Header.Set("Content-Type", ctype)`) | 1161 pn(`req.Header.Set("Content-Type", ctype)`) |
1158 } | 1162 } |
1159 pn(`req.Header.Set("User-Agent", "google-api-go-client/` + goGenVersion + `")`) | 1163 pn(`req.Header.Set("User-Agent", "google-api-go-client/` + goGenVersion + `")`) |
1160 pn("res, err := c.s.client.Do(req);") | 1164 pn("res, err := c.s.client.Do(req);") |
1161 pn("if err != nil { return %serr }", nilRet) | 1165 pn("if err != nil { return %serr }", nilRet) |
1162 pn("if err := googleapi.CheckResponse(res); err != nil { return %serr }" , nilRet) | 1166 pn("if err := googleapi.CheckResponse(res); err != nil { return %serr }" , nilRet) |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1315 } | 1319 } |
1316 | 1320 |
1317 func (a *argument) String() string { | 1321 func (a *argument) String() string { |
1318 return a.goname + " " + a.gotype | 1322 return a.goname + " " + a.gotype |
1319 } | 1323 } |
1320 | 1324 |
1321 func (a *argument) cleanExpr(prefix string) string { | 1325 func (a *argument) cleanExpr(prefix string) string { |
1322 switch a.gotype { | 1326 switch a.gotype { |
1323 case "[]string": | 1327 case "[]string": |
1324 log.Printf("TODO(bradfitz): only including the first parameter i n path query.") | 1328 log.Printf("TODO(bradfitz): only including the first parameter i n path query.") |
1325 » » return "cleanPathString(" + prefix + a.goname + "[0])" | 1329 » » return "url.QueryEscape(" + prefix + a.goname + "[0])" |
1326 case "string": | 1330 case "string": |
1327 » » return "cleanPathString(" + prefix + a.goname + ")" | 1331 » » return "url.QueryEscape(" + prefix + a.goname + ")" |
1328 case "integer", "int64": | 1332 case "integer", "int64": |
1329 return "strconv.FormatInt(" + prefix + a.goname + ", 10)" | 1333 return "strconv.FormatInt(" + prefix + a.goname + ", 10)" |
1330 case "uint64": | 1334 case "uint64": |
1331 return "strconv.FormatUint(" + prefix + a.goname + ", 10)" | 1335 return "strconv.FormatUint(" + prefix + a.goname + ", 10)" |
1332 } | 1336 } |
1333 log.Panicf("unknown type: apitype=%q, gotype=%q", a.apitype, a.gotype) | 1337 log.Panicf("unknown type: apitype=%q, gotype=%q", a.apitype, a.gotype) |
1334 return "" | 1338 return "" |
1335 } | 1339 } |
1336 | 1340 |
1337 // arguments are the arguments that a method takes | 1341 // arguments are the arguments that a method takes |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1543 si, ok := m[key].([]interface{}) | 1547 si, ok := m[key].([]interface{}) |
1544 if !ok { | 1548 if !ok { |
1545 return nil | 1549 return nil |
1546 } | 1550 } |
1547 sl := make([]string, 0) | 1551 sl := make([]string, 0) |
1548 for _, si := range si { | 1552 for _, si := range si { |
1549 sl = append(sl, si.(string)) | 1553 sl = append(sl, si.(string)) |
1550 } | 1554 } |
1551 return sl | 1555 return sl |
1552 } | 1556 } |
OLD | NEW |