Example #1
0
func CreateGroup(client oauth2_client.OAuth2Client, userId, projection string, value *ContactGroup) (response *GroupResponse, err error) {
	var useUserId string
	if len(userId) <= 0 {
		useUserId = GOOGLE_DEFAULT_USER_ID
	} else {
		useUserId = url.QueryEscape(userId)
	}
	if len(projection) <= 0 {
		projection = GOOGLE_DEFAULT_PROJECTION
	}
	headers := make(http.Header)
	headers.Set("GData-Version", "3.0")
	m := make(url.Values)
	if len(m.Get(CONTACTS_ALT_PARAM)) <= 0 {
		m.Set(CONTACTS_ALT_PARAM, "json")
	}
	uri := GOOGLE_GROUPS_API_ENDPOINT
	for _, s := range []string{useUserId, projection} {
		if len(s) > 0 {
			if uri[len(uri)-1] != '/' {
				uri += "/"
			}
			uri += s
		}
	}
	entry := &GroupEntryInsertRequest{Version: "1.0", Encoding: "UTF-8", Entry: value}
	value.Xmlns = XMLNS_ATOM
	value.XmlnsGcontact = XMLNS_GCONTACT
	value.XmlnsBatch = XMLNS_GDATA_BATCH
	value.XmlnsGd = XMLNS_GD
	buf, err := json.Marshal(entry)
	if err != nil {
		return
	}
	resp, _, err := oauth2_client.AuthorizedPostRequestBytes(client, headers, uri, m, buf)
	if err != nil {
		return
	}
	if resp != nil {
		if resp.StatusCode >= 400 {
			b, _ := ioutil.ReadAll(resp.Body)
			err = errors.New(string(b))
		} else {
			response = new(GroupResponse)
			err = json.NewDecoder(resp.Body).Decode(response)
		}
	}
	return
}
Example #2
0
func CreateContact(client oauth2_client.OAuth2Client, userId string, contact *Contact) (err error) {
	if userId == "" {
		userId = "me"
	}
	var useUserId string
	if len(userId) <= 0 {
		useUserId = YAHOO_DEFAULT_USER_ID
	} else {
		useUserId = url.QueryEscape(userId)
	}
	uri := YAHOO_SOCIAL_API_ENDPOINT
	scope := "user"
	resourceName := "contacts"
	for _, s := range []string{scope, useUserId, resourceName} {
		if len(s) > 0 {
			if uri[len(uri)-1] != '/' {
				uri += "/"
			}
			uri += s
		}
	}
	headers := make(http.Header)
	headers.Set("Accept", "application/json")
	headers.Set("Content-Type", "application/json; charset=utf-8")
	m := make(url.Values)
	m.Add("alt", "json")
	b, err := json.Marshal(contact)
	if err != nil {
		return err
	}
	resp, _, err := oauth2_client.AuthorizedPostRequestBytes(client, headers, uri, m, b)
	if err != nil {
		return err
	}
	if resp != nil {
		if resp.StatusCode >= 400 {
			e := new(ErrorResponse)
			b, _ := ioutil.ReadAll(resp.Body)
			json.Unmarshal(b, e)
			if len(e.ErrorField.Description) > 0 {
				err = e
			} else {
				err = errors.New(string(b))
			}
		}
	}
	return err
}