//生成短信请求url func (p *phoneSms_t) GenReqUrl(recNum string, smsParam string, SmsFreeSignName string, SmsTemplateCode string) (value string, err error) { //时间戳格式"2015-11-26 20:32:42" var timeStamp = zzcommon.StringSubstr(time.Now().String(), 19) var strMd5 string = p.genSign(recNum, smsParam, timeStamp, SmsFreeSignName, SmsTemplateCode) var reqUrl = p.UrlPattern + "?sign=" + strMd5 + "&app_key=" + p.AppKey + "&method=" + p.Method + "&rec_num=" + recNum + "&sign_method=" + p.SignMethod + "&sms_free_sign_name=" + SmsFreeSignName + "&sms_param=" + smsParam + "&sms_template_code=" + SmsTemplateCode + "&sms_type=" + p.SmsType + "×tamp=" + timeStamp + "&v=" + p.Versions url, err := url.Parse(reqUrl) if nil != err { fmt.Println("######PhoneRegister.genReqUrl err:", reqUrl, err) return reqUrl, err } reqUrl = p.UrlPattern + "?" + url.Query().Encode() return reqUrl, err }
func createYoutubeLink(src io.Reader) { var ids []string scanner := bufio.NewScanner(src) for scanner.Scan() { line := scanner.Text() url, err := url.Parse(line) if err != nil { logrus.WithFields(logrus.Fields{"err": err, "line": line}).Error("This line is not a proper url") continue } values := url.Query() if id, ok := values["v"]; !ok { logrus.WithField("line", line).Error("This line is not properly formed. Could not find id of video") } else { ids = append(ids, id[0]) } } if len(ids) <= 0 { logrus.Error("Could not parse a single line to produce a youtube playlist") os.Exit(-1) } else { var playListUrl = "https://www.youtube.com/watch_videos?video_ids=" + strings.Join(ids, ",") fmt.Println("URL of the playlist: ", playListUrl) } }
func (ch Channel) GetHistory(oldest int64) ([]Message, error) { token := os.Getenv("SLACK_API_TOKEN") var chan_res ChannelHistoryReponse url, _ := url.Parse("https://slack.com/api/channels.history") q := url.Query() q.Set("token", token) q.Set("channel", ch.ID) q.Set("oldest", strconv.FormatInt(oldest, 10)) url.RawQuery = q.Encode() res, err := http.Get(url.String()) defer res.Body.Close() if err != nil { fmt.Println(err) return nil, err } decoder := json.NewDecoder(res.Body) err2 := decoder.Decode(&chan_res) if err2 != nil { return nil, err } if !chan_res.OK { return nil, fmt.Errorf("API eturns not OK") } return chan_res.Messages, nil }
// ParseURI parses a database URI into ConnConfig func ParseURI(uri string) (ConnConfig, error) { var cp ConnConfig url, err := url.Parse(uri) if err != nil { return cp, err } if url.User != nil { cp.User = url.User.Username() cp.Password, _ = url.User.Password() } parts := strings.SplitN(url.Host, ":", 2) cp.Host = parts[0] if len(parts) == 2 { p, err := strconv.ParseUint(parts[1], 10, 16) if err != nil { return cp, err } cp.Port = uint16(p) } cp.Database = strings.TrimLeft(url.Path, "/") err = configSSL(url.Query().Get("sslmode"), &cp) if err != nil { return cp, err } return cp, nil }
// putCAS is used to do a PUT with optional CAS func (c *Client) putCAS(key string, value []byte, flags, index uint64, cas bool) (bool, error) { url := c.pathURL(key) query := url.Query() if cas { query.Set("cas", strconv.FormatUint(index, 10)) } query.Set("flags", strconv.FormatUint(flags, 10)) url.RawQuery = query.Encode() req := http.Request{ Method: "PUT", URL: url, Body: ioutil.NopCloser(bytes.NewReader(value)), } req.ContentLength = int64(len(value)) resp, err := c.config.HTTPClient.Do(&req) if err != nil { return false, err } defer resp.Body.Close() if resp.StatusCode != 200 { return false, fmt.Errorf("unexpected response code: %d", resp.StatusCode) } var buf bytes.Buffer if _, err := io.Copy(&buf, resp.Body); err != nil { return false, fmt.Errorf("failed to read response: %v", err) } res := strings.Contains(string(buf.Bytes()), "true") return res, nil }
// GetMatch returns a *dota.Match parsed out of the Dota2 WebAPI. func (wa *WebAPI) GetMatch(id int) (*dota.Match, error) { url := &url.URL{ Scheme: "https", Path: "api.steampowered.com/IDOTA2Match_570/GetMatchDetails/V001/", } query := url.Query() query.Set("key", wa.key) query.Set("match_id", strconv.Itoa(id)) url.RawQuery = query.Encode() resp, err := http.Get(url.String()) if err != nil { return nil, err } data, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } var md request.GetMatchDetails err = json.Unmarshal(data, &md) if err != nil { return nil, err } return md.ParseMatch() }
// GetCampaignPage returns all the campaigns from the supplied page func (ps *ProfitShare) GetCampaignPage(page int) ([]Campaign, Paginator, error) { url, err := url.Parse("affiliate-campaigns") if err != nil { return []Campaign{}, Paginator{}, err } q := url.Query() q.Add("page", fmt.Sprintf("%d", page)) url.RawQuery = q.Encode() body, err := ps.Get(url.String()) if err != nil { return []Campaign{}, Paginator{}, err } rez := map[string]CampaignsResult{} err = json.Unmarshal(body, &rez) if err != nil { return []Campaign{}, Paginator{}, err } return rez["result"].Campaigns, rez["result"].Paginator, nil }
// PrepGetObjects creates an http.Request from a GetObjectRequest func PrepGetObjects(r GetObjectRequest) (*http.Request, error) { url, err := url.Parse(r.URL) if err != nil { return nil, err } values := url.Query() // required values.Add("Resource", r.Resource) values.Add("Type", r.Type) // optional optionalString := OptionalStringValue(values) // one or the other _MUST_ be present optionalString("ID", r.ID) optionalString("UID", r.UID) // truly optional optionalString("ObjectData", strings.Join(r.ObjectData, ",")) optionalInt := OptionalIntValue(values) optionalInt("Location", r.Location) method := DefaultHTTPMethod if r.HTTPMethod != "" { method = r.HTTPMethod } url.RawQuery = values.Encode() return http.NewRequest(method, url.String(), nil) }
func (remote *Remote) do(command Command, parameters map[string]string) (string, error) { urlString := fmt.Sprintf(requestString, remote.Host, remote.Port, resources[command]) url, err := url.Parse(urlString) if err != nil { fmt.Println(err) return "", err } if parameters != nil { urlQuery := url.Query() for key, value := range parameters { urlQuery.Set(key, value) } url.RawQuery = urlQuery.Encode() } client := new(http.Client) req, err := http.NewRequest("GET", url.String(), nil) req.SetBasicAuth(remote.Username, remote.Password) resp, err := client.Do(req) if err != nil { fmt.Println(err) return "", err } body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println(err) return "", err } return string(body), nil }
// GetAdvertisers returns a list of advertise from a period of time to another func (ps *ProfitShare) GetAdvertisers(from time.Time, to time.Time) ([]Advertiser, error) { dateFormat := "2006-01-02" url, err := url.Parse("affiliate-advertisers") if err != nil { return []Advertiser{}, err } q := url.Query() q.Add("date_from", from.Format(dateFormat)) q.Add("date_to", to.Format(dateFormat)) url.RawQuery = q.Encode() body, err := ps.Get(url.String()) if err != nil { return []Advertiser{}, err } var rez map[string]map[string]Advertiser _ = json.Unmarshal(body, &rez) var ret []Advertiser for _, item := range rez["result"] { ret = append(ret, item) } return ret, nil }
func NewClient(uri string, opts *Options) (client *Client, err error) { url, err := url.Parse(uri) if err != nil { return } url.Path = path.Join("/socket.io", url.Path) url.Path = url.EscapedPath() if strings.HasSuffix(url.Path, "socket.io") { url.Path += "/" } q := url.Query() for k, v := range opts.Query { q.Set(k, v) } url.RawQuery = q.Encode() socket, err := engineio.NewClientConn(opts.Transport, url) if err != nil { return } client = &Client{ opts: opts, conn: socket, events: make(map[string]*caller), acks: make(map[int]*caller), } go client.readLoop() return }
func (r *EnvironmentVariableResource) ListByService(serviceURL string) ([]*EnvironmentVariable, error) { url := r.client.buildBaseURL("envvars/") q := url.Query() q.Set("service", serviceURL) url.RawQuery = q.Encode() return r.findMany(url) }
func Gzip(handler http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // @TODO(mark): Swap this for some handler flags url, err := url.Parse(r.URL.String()) queryParams := url.Query() _, found := queryParams[queryStringKey] if found { handler.ServeHTTP(w, r) return } if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") { handler.ServeHTTP(w, r) return } w.Header().Set("Content-Encoding", "gzip") gz, err := gzip.NewWriterLevel(w, gzip.BestCompression) if err != nil { http.Error(w, "Error with gzip compression", http.StatusInternalServerError) return } defer gz.Close() gzw := gzipResponseWriter{ Writer: gz, ResponseWriter: w, } handler.ServeHTTP(gzw, r) }) }
// PrepUrl handles the parsing and setup for all api calls. The encoded url string is passed // along with a set of param. Params are looped and injected into the master url func (l *Client) PrepUrl(endpoint string, params url.Values, dataKey bool) (string, error) { // parse the url into native http.URL url, err := url.Parse(fmt.Sprintf("%s/%s", l.BaseUrl(), endpoint)) if err != nil { return "", err } values := url.Query() // add the api key if params != nil { for key, val := range params { for _, v := range val { values.Add(key, v) } } } // if there is a data key use that by default, if not use the main api key // assumption here is that if its a data key there are specific reasons if dataKey { values.Add("key", l.dataApiKey) } else { values.Add("key", l.apiKey) } // encode the final url so we can return string and make call url.RawQuery = values.Encode() return url.String(), nil }
func (mrt *MapreduceTests) TestReduceError(c *ck.C) { return u := testReduceError{} job := mrt.setup(&u, &u.SimpleTasks) defer u.SimpleTasks.gather() ds := appwrap.NewLocalDatastore() _, err := Run(appwrap.StubContext(), ds, job) c.Check(err, ck.Equals, nil) resultUrl := <-u.SimpleTasks.done url, err := url.Parse(resultUrl) c.Check(err, ck.IsNil) fields := url.Query() c.Check(err, ck.IsNil) c.Check(fields["status"][0], ck.Equals, "error") c.Check(fields["error"][0], ck.Equals, "error retrying: maxium retries exceeded (task failed due to: reduce had an error)") // see if we handle retries properly v := testReduceError{succeedThreshold: u.count / 2} job = mrt.setup(&v, &v.SimpleTasks) defer v.SimpleTasks.gather() _, err = Run(appwrap.StubContext(), ds, job) c.Check(err, ck.Equals, nil) resultUrl = <-v.SimpleTasks.done c.Check(strings.Index(resultUrl, "status=done"), ck.Equals, 6) }
func (c *client) ListFacetValues(field string, maximumNumberOfValues int) (*FacetValues, error) { url, err := url.Parse(c.endpoint) if err != nil { return nil, err } q := url.Query() q.Set("field", field) q.Set("maximumNumberOfValues", strconv.Itoa(maximumNumberOfValues)) url.RawQuery = q.Encode() url.Path = url.Path + "values" req, err := http.NewRequest("GET", url.String(), nil) if err != nil { return nil, err } req.Header.Add("Authorization", "Bearer "+c.token) resp, err := c.httpClient.Do(req) if err != nil { return nil, err } defer resp.Body.Close() facetValues := &FacetValues{} err = json.NewDecoder(resp.Body).Decode(facetValues) return facetValues, nil }
// NewRequest creates a request object to query Wavefront // A relative URL should be passed along with the method, that will be resolved against the client's BaseURL // Parameters can be passed, a QueryParams map of k: v func (client Client) NewRequest(method, path string, params *QueryParams) (*http.Request, error) { rel, err := url.Parse(path) if err != nil { return nil, err } url := client.BaseURL.ResolveReference(rel) if params != nil { q := url.Query() for k, v := range *params { q.Set(k, v) } url.RawQuery = q.Encode() } req, err := http.NewRequest(method, url.String(), nil) if err != nil { return nil, err } req.Header.Add("X-AUTH-TOKEN", client.Config.Token) if method == "POST" { req.Header.Add("Content-Type", "application/x-www-form-urlencoded") } return req, nil }
func TestSDBSign(t *testing.T) { // Example PutAttributes request url, err := url.Parse(subkey(p)) if err != nil { t.Error(err) } //log.Println("query: ", url.RawQuery) // map go's richer query and map it to my own q := url.Query() m := Strmap{} for k, v := range q { //log.Println(k, v) if len(v) > 1 { log.Fatal("not expecting multi queries") } m[k] = v[0] } // log.Println(m) r := NewSDBRequest(m) // log.Println("A:", r.stringToSign(), "\nB: ", subkey(bah)) if subkey(r.stringToSign()) != subkey(bah) { t.Error("string to sign mismatch") } }
func TestGetRawFile(t *testing.T) { testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != "GET" { t.Fatalf("wanted GET but found %s\n", r.Method) } url := *r.URL wantPath := "/projects/prj/repos/repo/browse/foo/bar" if url.Path != wantPath { t.Fatalf("Want %s but found %s\n", wantPath, url.Path) } if r.Header.Get("Authorization") != "Basic dTpw" { t.Fatalf("Want Basic dTpw but found %s\n", r.Header.Get("Authorization")) } params := url.Query() if params.Get("at") != "master" { t.Fatalf("Want master but found %s\n", params["at"]) } if _, ok := params["raw"]; !ok { t.Fatalf("Want a raw query param but found none") } fmt.Fprint(w, "hello") })) defer testServer.Close() url, _ := url.Parse(testServer.URL) stashClient := NewClient("u", "p", url) data, _ := stashClient.GetRawFile("PRJ", "REPO", "foo/bar", "master") if string(data) != "hello" { t.Fatalf("Want hello, but got <%s>\n", string(data)) } }
func (c *Client) ListFlights(departureDate time.Time, originAirport string, destinationAirport string) ([]*model.Flight, error) { originAirport, err := normalizeAirportCode(originAirport) if err != nil { return nil, err } destinationAirport, err = normalizeAirportCode(destinationAirport) if err != nil { return nil, err } url, err := url.Parse("/v1/mobile/flights/products") if err != nil { return nil, err } url = apiExtensionsBaseUrl.ResolveReference(url) queryValues := url.Query() queryValues.Set("currency-type", "Dollars") queryValues.Set("number-adult-passengers", "1") queryValues.Set("number-senior-passengers", "0") queryValues.Set("promo-code", "") queryValues.Set("origination-airport", originAirport) queryValues.Set("destination-airport", destinationAirport) queryValues.Set("departure-date", departureDate.Format("2006-01-02")) url.RawQuery = queryValues.Encode() httpReq, err := http.NewRequest("GET", url.String(), nil) if err != nil { return nil, err } httpReq.Header.Add(apiKeyHeader, apiKeyValue) httpReq.Header.Add(userAgentHeader, userAgentValue) httpResp, err := c.httpClient.Do(httpReq) if err != nil { return nil, err } defer httpResp.Body.Close() if httpResp.StatusCode != 200 { return nil, fmt.Errorf("Unexpected response code listing flights: %d", httpResp.StatusCode) } else if httpResp.Body == nil { return nil, fmt.Errorf("No body listing flights") } body, err := ioutil.ReadAll(httpResp.Body) if err != nil { return nil, err } swResponse := api_model.ListFlightsResponse{} if err := json.Unmarshal(body, &swResponse); err != nil { return nil, err } return swListFlightsResponseToFlights(&swResponse) }
func (r *ServiceResource) Get(instanceURL string, name string) (*Service, error) { url := r.client.buildBaseURL("services/find/") q := url.Query() q.Set("instance", instanceURL) q.Set("name", name) url.RawQuery = q.Encode() return r.findOne(url) }
// populatePageValues parses the HTTP Link response headers and populates the // various pagination link values in the Response. func (r *Response) populatePageValues() { if links, ok := r.Response.Header["Link"]; ok && len(links) > 0 { for _, link := range strings.Split(links[0], ",") { segments := strings.Split(strings.TrimSpace(link), ";") // link must at least have href and rel if len(segments) < 2 { continue } // ensure href is properly formatted if !strings.HasPrefix(segments[0], "<") || !strings.HasSuffix(segments[0], ">") { continue } // try to pull out page parameter url, err := url.Parse(segments[0][1 : len(segments[0])-1]) if err != nil { continue } page := url.Query().Get("page") since := url.Query().Get("since") if page == "" && since == "" { continue } if since != "" { page = since } for _, segment := range segments[1:] { switch strings.TrimSpace(segment) { case `rel="next"`: r.NextPage, _ = strconv.Atoi(page) case `rel="prev"`: r.PrevPage, _ = strconv.Atoi(page) case `rel="first"`: r.FirstPage, _ = strconv.Atoi(page) case `rel="last"`: r.LastPage, _ = strconv.Atoi(page) } } } } }
func (r *HostNameResource) Get(instanceURL string, host string) (*HostName, error) { url := r.client.buildBaseURL("hosts/find/") q := url.Query() q.Set("instance", instanceURL) q.Set("host", host) url.RawQuery = q.Encode() return r.findOne(url) }
// ParseDrainUri creates a DrainConfig from the drain URI. func ParseDrainUri(name string, uri string, namedFormats map[string]string) (*DrainConfig, error) { url, err := url.Parse(uri) if err != nil { return nil, err } config := DrainConfig{Name: name, Type: url.Scheme} if _, ok := DRAINS[config.Type]; !ok { return nil, fmt.Errorf("unknown drain type: %s", uri) } config.Scheme = url.Scheme config.Host = url.Host config.Path = url.Path // Go doesn't correctly parse file:// uris with empty <host>. // http://tools.ietf.org/html/rfc1738 if url.Scheme == "file" { if strings.HasPrefix(url.Path, "//") { config.Path = url.Path[2:] } } params := url.Query() // parse filters if filters, ok := params["filter"]; ok { params.Del("filter") config.Filters = filters } else { // default filter: all config.Filters = []string{""} } if len(config.Filters) == 0 { panic("filters can't be empty") } // parse format if format, ok := params["format"]; ok { params.Del("format") if format[0] != "json" { config.Format, config.rawFormat, err = parseFormat(name, format[0], namedFormats) if err != nil { return nil, err } } } // assign the rest of the params config.Params = make(map[string]string) for k, v := range params { // NOTE: multi value params are not supported. config.Params[k] = v[0] } return &config, nil }
func addPathParams(path string, params interface{}) (string, error) { // Simple conversion from struct to query parameters // Only supports basic conversion. val := reflect.ValueOf(params) if val.IsNil() { return path, nil } if val.Kind() == reflect.Ptr { val = val.Elem() } if val.Kind() != reflect.Struct { return path, errors.New("Params must be a struct.") } url, err := url.Parse(path) if err != nil { return path, err } // Convert the params obejct into query parameters query := url.Query() typ := val.Type() for i := 0; i < val.NumField(); i++ { typField := typ.Field(i) if typField.PkgPath != "" { continue // unexported } tag := typField.Tag.Get("url") if tag == "-" { continue } // @todo: Add support for more types or utilize url encoding package like // https://github.com/google/go-querystring value := "" valField := val.Field(i) switch valField.Kind() { case reflect.String: value = valField.String() case reflect.Int: if valField.Int() != 0 { value = fmt.Sprintf("%d", valField) } default: value = fmt.Sprintf("%v", valField) } // If we have a value set, add it to the query if value != "" { query.Set(tag, value) } } url.RawQuery = query.Encode() return url.String(), nil }
// ParseURI parses a database URI into ConnConfig // // Query parameters not used by the connection process are parsed into ConnConfig.RuntimeParams. func ParseURI(uri string) (ConnConfig, error) { var cp ConnConfig url, err := url.Parse(uri) if err != nil { return cp, err } if url.User != nil { cp.User = url.User.Username() cp.Password, _ = url.User.Password() } parts := strings.SplitN(url.Host, ":", 2) cp.Host = parts[0] if len(parts) == 2 { p, err := strconv.ParseUint(parts[1], 10, 16) if err != nil { return cp, err } cp.Port = uint16(p) } cp.Database = strings.TrimLeft(url.Path, "/") err = configSSL(url.Query().Get("sslmode"), &cp) if err != nil { return cp, err } ignoreKeys := map[string]struct{}{ "sslmode": struct{}{}, } cp.RuntimeParams = make(map[string]string) for k, v := range url.Query() { if _, ok := ignoreKeys[k]; ok { continue } cp.RuntimeParams[k] = v[0] } return cp, nil }
func main() { url, err := url.Parse("http://example.com/foo?bar=baz") if err != nil { panic(err) } fmt.Println("path: " + url.Path) fmt.Println("params: " + url.RawQuery) fmt.Println("params[\"bar\"]: " + url.Query().Get("bar")) }
func IdURL(cn string) *url.URL { url, err := url.Parse(*fpcaURL) url.Path = "/get-certificate" check(err) q := url.Query() q.Set("nickname", cn) url.RawQuery = q.Encode() return url }
func (r *KeyPairResource) GetByName(name string, resourceGroupURL *string) (*KeyPair, error) { url := r.client.buildBaseURL("keypairs/find/") q := url.Query() q.Set("name", name) if resourceGroupURL != nil { q.Set("resource_group", *resourceGroupURL) } url.RawQuery = q.Encode() return r.findOne(url) }
func (r *ResourceGroupResource) GetByName(name string) (*ResourceGroup, error) { url := r.client.buildBaseURL("resource_groups/find/") q := url.Query() q.Set("name", name) url.RawQuery = q.Encode() resourceGroup, err := r.findOne(url) if _, ok := err.(ErrNotFound); ok { return resourceGroup, fmt.Errorf("resource group %q was not found", name) } return resourceGroup, err }