예제 #1
0
func UpdateGroup(client oauth2_client.OAuth2Client, userId, projection string, original, 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")
	headers.Set("If-Match", original.Etag)
	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, original.GroupId()} {
		if len(s) > 0 {
			if uri[len(uri)-1] != '/' {
				uri += "/"
			}
			uri += s
		}
	}
	entry := &GroupEntryUpdateRequest{Entry: value}
	value.Xmlns = XMLNS_ATOM
	value.XmlnsGcontact = XMLNS_GCONTACT
	value.XmlnsBatch = XMLNS_GDATA_BATCH
	value.XmlnsGd = XMLNS_GD
	value.Etag = original.Etag
	value.Id = original.Id
	value.Updated.Value = time.Now().UTC().Format(GOOGLE_DATETIME_FORMAT)
	value.Categories = make([]AtomCategory, 1)
	value.Categories[0].Scheme = ATOM_CATEGORY_SCHEME_KIND
	value.Categories[0].Term = ATOM_CATEGORY_TERM_GROUP
	buf, err := json.Marshal(entry)
	if err != nil {
		return
	}
	resp, _, err := oauth2_client.AuthorizedPutRequestBytes(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
}
예제 #2
0
func UpdateContact(client oauth2_client.OAuth2Client, userId, contactId 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 := "contact"
	resourceId := contactId
	for _, s := range []string{scope, useUserId, resourceName, resourceId} {
		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.AuthorizedPutRequestBytes(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
}