예제 #1
0
/*
Does the actual google books API call.
*/
func lookupISBN(ctx appengine.Context, country string, isbn isbn13.ISBN13) (resp *data.BookMetaData, err error) {
	var r *http.Response
	url := fmt.Sprintf(lookupURLTemplate, uint64(isbn), country, apiKey)
	ctx.Debugf("Calling %s", fmt.Sprintf(lookupURLTemplate, uint64(isbn), country, "<hidden>"))

	client := urlfetch.Client(ctx)
	if r, err = client.Get(url); err == nil {
		if r.StatusCode != http.StatusOK {
			err = fmt.Errorf("Google API returned %s", r.Status)
		} else {
			reply := new(data.LookupReply)
			decode := json.NewDecoder(r.Body)
			defer r.Body.Close()

			ctx.Infof("Completed API call, result %s\n", r.Status)

			if err = decode.Decode(reply); err == nil {

				if reply.Count == 1 {
					resp = &reply.BookInfos[0]
				} else {
					ctx.Infof("Google books reported %d matching items: %v", reply.Count, reply)
					resp = new(data.BookMetaData)
					resp.Volume.Title = placeHolderText + isbn.String()
				}

				resp.Parent = country
				resp.ISBN = isbn.String()
			}
		}
	}

	return
}
예제 #2
0
func putVolumeSingle(call *Call, isbn isbn13.ISBN13) (info *data.BookMetaData, err error) {
	call.Context.Debugf("Updating %d", isbn)
	decode := json.NewDecoder(call.Request.Body)
	info = new(data.BookMetaData)
	if err = decode.Decode(info); err == nil {
		call.Context.Debugf("Raw decoded entity: %v", info)
		info.ISBN = isbn.String()
		normalize(call, info)
		call.Context.Debugf("Normalized decoded entity: %v", info)

		persistence.UpdateBookshelf(call.Context, func(t *tx.Transaction, shelf *data.Bookshelf) error {

			if ptr := shelf.LookupInfo(isbn); ptr != nil {
				*ptr = *info
				t.Put = []tx.KeyDeriver{ptr}
			} else {
				info.Parent = shelf
				shelf.Books = append(shelf.Books, *info)
				t.Put = []tx.KeyDeriver{info}
			}

			return nil
		})
	}
	return info, err
}
예제 #3
0
func (shelf *Bookshelf) LookupInfo(isbn isbn13.ISBN13) *BookMetaData {
	for i := range shelf.Books {
		book := &shelf.Books[i]

		if book.ISBN == isbn.String() {
			return book
		}
	}

	return nil
}
예제 #4
0
func deleteVolumeSingle(call *Call, isbn isbn13.ISBN13) (info *data.BookMetaData, err error) {
	call.Context.Infof("Deleting ISBN %d", isbn)
	err = persistence.UpdateBookshelf(call.Context, func(t *tx.Transaction, shelf *data.Bookshelf) error {
		for i := range shelf.Books {
			if ptr := &shelf.Books[i]; ptr.ISBN == isbn.String() {
				shelf.Books = append(shelf.Books[:i], shelf.Books[i+1:]...)
				t.Delete = []tx.KeyDeriver{ptr}
				return nil
			}

		}
		call.StatusCode = http.StatusNotFound
		return errors.New("ISBN not found")
	})

	return
}
예제 #5
0
func BookInfoPrototypeKey(ctx ae.Context, isbn isbn13.ISBN13, country string) string {
	return country + ":" + isbn.String()
}