Пример #1
0
        private List <BookItem> GetBooksForClass(
            Valence.Request.Parameters param,
            Valence.OrgUnitInfo course,
            ref string message)
        {
            Uri uri = param.UserContext.CreateAuthenticatedUri(
                "/d2l/api/" + param.ApiVersion("le") + "/" + course.Id + "/content/isbn/", "GET");

            var request = (HttpWebRequest)WebRequest.Create(uri);

            request.Method = "GET";

            var handler = new Valence.Request.ErrorHandler();
            var result  = new List <BookItem>();

            Valence.Request.Perform(
                request, param.UserContext,
                delegate(string data) {
                var serializer = new JavaScriptSerializer();
                var books      = serializer.Deserialize <Valence.Book[]>(data);

                if ((books == null) || (books.Length <= 0))
                {
                    return;
                }

                foreach (Valence.Book b in books)
                {
                    try {
                        GoogleBook.Volume volume = GoogleBook.Api.Query(b.Isbn);

                        if (volume.TotalItems != 0)
                        {
                            GoogleBook.Item item = volume.items.First();

                            BookItem book = FromGoogleBookToBookItem(item);

                            result.Add(book);
                        }
                    } catch (ArgumentNullException) {}
                }
            },
                handler.Process);

            if (handler.IsError)
            {
                message = "Unable to retrieve assigned books. " + handler.Message;
            }

            return(result);
        }
Пример #2
0
        static public BookItem Adapt(GoogleBook.Item book)
        {
            if (book.VolumeInfo.Title == null)
            {
                throw new ArgumentNullException();
            }

            var item = new BookItem {
                Title = book.VolumeInfo.Title, Author = ""
            };

            if (book.VolumeInfo.Authors != null)
            {
                item.Author = string.Join(", ", book.VolumeInfo.Authors);
            }

            item.Isbn = "";

            if (book.VolumeInfo.IndustryIdentifiers != null)
            {
                foreach (GoogleBook.Item._VolumeInfo.IndustryIdentifierItem id
                         in book.VolumeInfo.IndustryIdentifiers)
                {
                    if (id.Type.StartsWith("ISBN_10"))
                    {
                        item.Isbn = id.Identifier;
                    }
                }
            }

            if (item.Isbn.Length == 0)
            {
                throw new ArgumentNullException();
            }

            item.ThumbnailUrl = "";
            if ((book.VolumeInfo.ImageLinks != null) &&
                (book.VolumeInfo.ImageLinks.Thumbnail != null))
            {
                item.ThumbnailUrl = book.VolumeInfo.ImageLinks.Thumbnail;
            }

            return(item);
        }
Пример #3
0
        public IEnumerable <BookItem> AssignedBooks()
        {
            const string ROUTE = "";

            var client = CreateClient();

            var request = CreateContentRequest(ROUTE, Method.GET);

            var response = client.Execute <List <Valence.Book> >(request);

            if (response.StatusCode != HttpStatusCode.OK)
            {
                throw new InvalidOperationException("Unable to get isbns from course. Returned status: " + response.StatusCode);
            }

            var bookList = new List <BookItem>();

            foreach (Valence.Book b in response.Data)
            {
                try {
                    GoogleBook.Volume volume = GoogleBook.Api.Query(b.Isbn);

                    if (volume.TotalItems != 0)
                    {
                        GoogleBook.Item item = volume.items.First();

                        BookItem book = GoogleBookAdaptor.Adapt(item);

                        // ensure that the isbn in the LMS is set.  Sometimes GoogleBooks returns an alternate ISBN
                        book.Isbn = b.Isbn;

                        bookList.Add(book);
                    }
                } catch (ArgumentNullException) {}
            }

            return(bookList);
        }