Esempio n. 1
0
// 获取预授权码,用于公众号oauth
func (ra *regularApi) GetPreAuthCode(accessToken string) (string, float64) {
	postData := struct {
		Component_appid string `json:"component_appid"`
	}{
		Component_appid: ra.wt.appId,
	}

	res, err := goreq.Request{
		Method: "POST",
		Uri:    fmt.Sprintf(apiCreatePreAuthCode, accessToken),
		Body:   postData,
	}.Do()
	if err != nil {
		log.Println("getPreAuthCode api failed: ", err.Error())
		return "", 0
	}
	result := &struct {
		PAC       string  `json:"pre_auth_code"`
		ExpiresIn float64 `json:"expires_in"`
	}{}

	err = unmarshalResponseToJson(res, result)
	if err != nil {
		log.Println("Parse pre auth token failed: ", err)
		return "", 0
	}
	return result.PAC, result.ExpiresIn

}
Esempio n. 2
0
// 获取第三方平台令牌
func (ra *regularApi) GetAccessToken(ticket string) (string, float64) {

	postData := struct {
		Component_appid         string `json:"component_appid"`
		Component_appsecret     string `json:"component_appsecret"`
		Component_verify_ticket string `json:"component_verify_ticket"`
	}{
		Component_appid:         ra.wt.appId,
		Component_appsecret:     ra.wt.appSecret,
		Component_verify_ticket: ticket,
	}

	res, err := goreq.Request{
		Method: "POST",
		Uri:    apiComponentToken,
		Body:   postData,
	}.Do()
	if err != nil {
		log.Println("getAccessToken api failed: ", err.Error())
		return "", 0
	}

	result := &struct {
		CAT       string  `json:"component_access_token"`
		ExpiresIn float64 `json:"expires_in"`
	}{}
	err = unmarshalResponseToJson(res, result)
	if err != nil {
		log.Println("Parse access token failed: ", err)

	}
	return result.CAT, result.ExpiresIn

}
Esempio n. 3
0
//retrieve object from the object store
func (u *UserGraphResource) getObject(id string, response *restful.Response) (*Object, error) {
	res, err := goreq.Request{
		Uri:         u.baseUrl + id,
		Accept:      "application/json",
		ContentType: "application/json",
	}.Do()

	if err != nil {
		response.AddHeader("Content-Type", "text/plain")
		response.WriteErrorString(http.StatusInternalServerError, err.Error())
		return nil, errors.New("500")
	}
	if res.StatusCode == 404 {
		response.AddHeader("Content-Type", "text/plain")
		response.WriteErrorString(http.StatusNotFound, "Requested item could not be found.")
		return nil, errors.New("404")
	}
	if res.StatusCode != 200 {
		response.AddHeader("Content-Type", "text/plain")
		value, _ := res.Body.ToString()
		response.WriteErrorString(http.StatusInternalServerError, "Object store returned: "+
			strconv.Itoa(res.StatusCode)+" Response body:\n"+value)
		return nil, errors.New(strconv.Itoa(res.StatusCode))
	}
	obj := new(Object)
	res.Body.FromJsonTo(&obj)
	res.Body.Close()
	return obj, nil
}
Esempio n. 4
0
func (c Client) get(url string, requestData interface{}, result interface{}) []error {
	var errs []error

	resp, err := goreq.Request{
		Uri:         url,
		QueryString: requestData,
		CookieJar:   c.cookie,
	}.Do()
	if err != nil {
		errs = append(errs, errors.New(err.Error()))
		log.Fatalln(err.Error())
	}
	defer func() {
		err := resp.Body.Close()
		if err != nil {
			log.Fatal(err)
		}
	}()

	if resp.StatusCode != http.StatusOK {
		errs = append(errs, errors.New("Failed login: status code is "+resp.Status))
	}

	err = resp.Body.FromJsonTo(&result)
	if err != nil {
		errs = append(errs, errors.New(err.Error()))
		log.Println(err.Error())
	}

	return errs
}
Esempio n. 5
0
func main() {

	item := Item{Id: 1111, Name: "foobar"}

	res, err := goreq.Request{
		Method: "POST",
		Uri:    "http://www.google.com",
		Body:   item,
	}.Do()

	if err != nil {
		fmt.Println("Something wrong ", err.Error())
	} else {
		spew.Dump(res.Header)
		fmt.Println(res.Body.ToString())
	}

}
Esempio n. 6
0
func main() {
	flag.Parse()
	if *eaddr == "" || *topic == "" || *channel == "" {
		log.Println("Please input the elastic addr or topic or channel")
		return
	}
	res, err := goreq.Request{Uri: *eaddr + "?topic=" + *topic + "&channel=" + *channel}.Do()
	if err != nil {
		log.Println(err.Error())
	}
	var v models.NodeItem
	body, _ := res.Body.ToString()
	log.Println(body)
	err = json.Unmarshal([]byte(body), &v)

	if err != nil {
		log.Fatal(err.Error())
	}
	addr = v.Ip + ":" + strconv.Itoa(v.Tcpport)
	consumer, err = nsq.NewConsumer(*topic, *channel, cfg)
	if err != nil {
		log.Fatal(err.Error())
	}
	consumer.AddHandler(nsq.HandlerFunc(func(m *nsq.Message) error {
		log.Println(string(m.Body))
		return nil
	}))
	consumer.ConnectToNSQD(addr)
	<-exitchan
}
Esempio n. 7
0
// POST http://localhost:8085/users
// {"name":"user name"}
//
func (u *UserGraphResource) createUser(request *restful.Request, response *restful.Response) {
	obj := new(Object)
	err := request.ReadEntity(&obj.Data.User)
	if err != nil {
		response.AddHeader("Content-Type", "text/plain")
		response.WriteErrorString(http.StatusInternalServerError, err.Error())
		return
	}

	res, err := goreq.Request{
		Method:      "POST",
		Body:        obj,
		Uri:         u.baseUrl,
		Accept:      "application/json",
		ContentType: "application/json",
	}.Do()

	if err != nil {
		response.AddHeader("Content-Type", "text/plain")
		response.WriteErrorString(http.StatusInternalServerError, err.Error())
		return
	}
	if res.StatusCode != 201 {
		response.AddHeader("Content-Type", "text/plain")
		value, _ := res.Body.ToString()
		response.WriteErrorString(http.StatusInternalServerError, "Object store returned: "+
			strconv.Itoa(res.StatusCode)+" Response body:\n"+value)
		return
	}
	res.Body.FromJsonTo(&obj)
	res.Body.Close()
	user := obj.Data.User
	user.Id = obj.Id
	response.WriteHeader(http.StatusCreated)
	response.WriteEntity(user)
}