Пример #1
0
        /// <summary>
        /// this is a helper function for external utilities. It is not worth
        /// running the other insert/saves through here, as this would involve
        /// double buffering/copying of the bytes
        /// </summary>
        /// <param name="targetUri"></param>
        /// <param name="payload"></param>
        /// <param name="type"></param>
        /// <returns>Stream</returns>
        public Stream StringSend(Uri targetUri, String payload, GDataRequestType type)
        {
            Tracing.Assert(targetUri != null, "targetUri should not be null");
            if (targetUri == null)
            {
                throw new ArgumentNullException("targetUri");
            }

            Tracing.Assert(payload != null, "payload should not be null");
            if (payload == null)
            {
                throw new ArgumentNullException("payload");
            }

            IGDataRequest request = this.RequestFactory.CreateRequest(type, targetUri);

            request.Credentials = this.Credentials;

            Stream outputStream = request.GetRequestStream();

            StreamWriter w = new StreamWriter(outputStream);

            w.Write(payload);
            w.Flush();

            request.Execute();

            w.Close();
            return(request.GetResponseStream());
        }
Пример #2
0
        /// <summary>object QueryOpenSearchRssDescription()</summary>
        /// <param name="serviceUri">the service to ask for an OpenSearchRss Description</param>
        /// <returns> a webresponse object</returns>
        public Stream QueryOpenSearchRssDescription(Uri serviceUri)
        {
            if (serviceUri == null)
            {
                throw new System.ArgumentNullException("serviceUri");
            }

            IGDataRequest request = this.RequestFactory.CreateRequest(GDataRequestType.Query, serviceUri);

            request.Credentials = this.Credentials;
            request.Execute();
            // return the response
            return(request.GetResponseStream());
        }
Пример #3
0
        /// <summary>the basic interface. Take a URI and just get it</summary>
        /// <param name="queryUri">the URI to execute</param>
        /// <param name="ifModifiedSince">used to set a precondition date that
        /// indicates the feed should be returned only if it has been modified
        /// after the specified date. A value of DateTime.MinValue indicates no
        /// precondition.</param>
        /// <param name="etag">used to set a precondition etag that
        /// indicates the feed should be returned only if it has been modified </param>
        /// <param name="contentLength">returns the content length of the response</param>
        /// <returns> a webresponse object</returns>
        private Stream Query(Uri queryUri, DateTime ifModifiedSince, string etag, out long contentLength)
        {
            Tracing.TraceCall("Enter");
            if (queryUri == null)
            {
                throw new System.ArgumentNullException("queryUri");
            }

            contentLength = -1;

            IGDataRequest request = this.RequestFactory.CreateRequest(GDataRequestType.Query, queryUri);

            request.Credentials     = this.Credentials;
            request.IfModifiedSince = ifModifiedSince;

            if (etag != null)
            {
                ISupportsEtag ise = request as ISupportsEtag;
                if (ise != null)
                {
                    ise.Etag = etag;
                }
            }

            try
            {
                request.Execute();
            }
            catch (Exception)
            {
                // Prevent connection leaks
                if (request.GetResponseStream() != null)
                {
                    request.GetResponseStream().Close();
                }

                throw;
            }

            // return the response
            GDataGAuthRequest gr = request as GDataGAuthRequest;

            if (gr != null)
            {
                contentLength = gr.ContentLength;
            }

            Tracing.TraceCall("Exit");
            return(new GDataReturnStream(request));
        }
Пример #4
0
        /// <summary>Inserts an AtomBase entry against a Uri</summary>
        /// <param name="feedUri">the uri for the feed this object should be posted against</param>
        /// <param name="baseEntry">the entry to be inserted</param>
        /// <param name="type">the type of request to create</param>
        /// <param name="data">the async data payload</param>
        /// <returns> the response as a stream</returns>
        internal virtual Stream EntrySend(Uri feedUri, AtomBase baseEntry, GDataRequestType type, AsyncSendData data)
        {
            Tracing.Assert(feedUri != null, "feedUri should not be null");
            if (feedUri == null)
            {
                throw new ArgumentNullException("feedUri");
            }

            Tracing.Assert(baseEntry != null, "baseEntry should not be null");
            if (baseEntry == null)
            {
                throw new ArgumentNullException("baseEntry");
            }

            this.versionInfo.ImprintVersion(baseEntry);

            IGDataRequest request = this.RequestFactory.CreateRequest(type, feedUri);

            request.Credentials = this.Credentials;

            ISupportsEtag eTarget = request as ISupportsEtag;
            ISupportsEtag eSource = baseEntry as ISupportsEtag;

            if (eTarget != null && eSource != null)
            {
                eTarget.Etag = eSource.Etag;
            }

            request.ContentStore = baseEntry;
            request.IsBatch      = type == GDataRequestType.Batch;

            if (data != null)
            {
                GDataGAuthRequest gr = request as GDataGAuthRequest;
                if (gr != null)
                {
                    gr.AsyncData = data;
                }
            }

            Stream outputStream = request.GetRequestStream();

            baseEntry.SaveToXml(outputStream);
            request.Execute();

            outputStream.Close();
            return(request.GetResponseStream());
        }
        public void     TestGZipQuery()
        {
            IGDataRequest request = this.calendarService.RequestFactory.CreateRequest(GDataRequestType.Query, new Uri(calendarUri));

            Assert.IsTrue(request.UseGZip, "IGDataRequest.UseGZip property should be true.");

            request.Credentials = new GDataCredentials(this.userName, this.passWord);
            request.Execute();
            Assert.IsTrue(request.UseGZip, "IGDataRequest.UseGZip is not true, the response was NOT in GZip format.");

            Stream responseStream = request.GetResponseStream();

            Assert.IsTrue(responseStream != null, "Response stream should not be null.");

            AtomFeed feed = new AtomFeed(new Uri(calendarUri), this.calendarService);

            feed.Parse(request.GetResponseStream(), AlternativeFormat.Atom);
            Assert.IsTrue(feed.Self != null, "AtomFeed object is not right.");
        }
Пример #6
0
        ///<summary>Deletes an Atom entry when given a Uri</summary>
        ///<param name="uriTarget">The target Uri to call http delete against</param>
        ///<param name="eTag">The eTag of the item to delete. This parameter is used for strong
        /// concurrency support in protocol version 2 and up</param>
        public void Delete(Uri uriTarget, string eTag)
        {
            Tracing.Assert(uriTarget != null, "uri should not be null");
            if (uriTarget == null)
            {
                throw new ArgumentNullException("uriTarget");
            }

            Tracing.TraceMsg("Deleting entry: " + uriTarget.ToString());
            IGDataRequest request = RequestFactory.CreateRequest(GDataRequestType.Delete, uriTarget);

            ISupportsEtag eTarget = request as ISupportsEtag;

            if (eTarget != null && eTag != null)
            {
                eTarget.Etag = eTag;
            }

            request.Credentials = Credentials;
            request.Execute();
            IDisposable disp = request as IDisposable;

            disp.Dispose();
        }
Пример #7
0
        /////////////////////////////////////////////////////////////////////////////


        //////////////////////////////////////////////////////////////////////
        /// <summary>Inserts an AtomBase entry against a Uri. The overloaded
        /// version here will check if this is an AbstractEntry and if it has
        /// a media property set. If so, it will create a mime multipart envelope</summary>
        /// <param name="feedUri">the uri for the feed this object should be posted against</param>
        /// <param name="baseEntry">the entry to be inserted</param>
        /// <param name="type">the type of request to create</param>
        /// <param name="data">the async data payload</param>
        /// <returns> the response as a stream</returns>
        //////////////////////////////////////////////////////////////////////
        internal override Stream EntrySend(Uri feedUri, AtomBase baseEntry, GDataRequestType type, AsyncSendData data)
        {
            if (feedUri == null)
            {
                throw new ArgumentNullException("feedUri");
            }
            //Tracing.Assert(baseEntry != null, "baseEntry should not be null");
            if (baseEntry == null)
            {
                throw new ArgumentNullException("baseEntry");
            }

            AbstractEntry entry = baseEntry as AbstractEntry;

            // if the entry is not an abstractentry or if no media is set, do the default
            if (entry == null || entry.MediaSource == null)
            {
                return(base.EntrySend(feedUri, baseEntry, type, data));
            }

            Stream outputStream = null;
            Stream inputStream  = null;

            try
            {
                IGDataRequest request = this.RequestFactory.CreateRequest(type, feedUri);
                request.Credentials = this.Credentials;

                GDataRequest r = request as GDataRequest;

                if (r != null)
                {
                    r.ContentType = MediaService.MimeContentType;
                    r.Slug        = entry.MediaSource.Name;

                    GDataRequestFactory f = this.RequestFactory as GDataRequestFactory;
                    if (f != null)
                    {
                        f.CustomHeaders.Add("MIME-version: 1.0");
                    }
                }

                if (data != null)
                {
                    GDataGAuthRequest gr = request as GDataGAuthRequest;
                    if (gr != null)
                    {
                        gr.AsyncData = data;
                    }
                }


                outputStream = request.GetRequestStream();
                inputStream  = entry.MediaSource.GetDataStream();
                StreamWriter w = new StreamWriter(outputStream);

                w.WriteLine("Media multipart posting");
                CreateBoundary(w, GDataRequestFactory.DefaultContentType);
                baseEntry.SaveToXml(outputStream);
                w.WriteLine();
                CreateBoundary(w, entry.MediaSource.ContentType);
                WriteInputStreamToRequest(inputStream, outputStream);
                w.WriteLine();
                w.WriteLine("--" + MediaService.MimeBoundary + "--");
                w.Flush();
                request.Execute();
                outputStream.Close();
                outputStream = null;
                return(request.GetResponseStream());
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (outputStream != null)
                {
                    outputStream.Close();
                }
                if (inputStream != null)
                {
                    inputStream.Close();
                }
            }
        }
Пример #8
0
        /// <summary>
        /// this is a helper function for to send binary data to a resource
        /// it is not worth running the other insert/saves through here, as this would involve
        /// double buffering/copying of the bytes
        /// </summary>
        /// <param name="targetUri"></param>
        /// <param name="inputStream"></param>
        /// <param name="type"></param>
        /// <param name="contentType">the contenttype to use in the request, if NULL is passed, factory default is used</param>
        /// <param name="slugHeader">the slugHeader to use in the request, if NULL is passed, factory default is used</param>
        /// <param name="etag">The http etag to pass into the request</param>
        /// <param name="data">The async data needed for notifications</param>
        /// <returns>Stream from the server response. You should close this stream explicitly.</returns>
        private Stream StreamSend(Uri targetUri,
                                  Stream inputStream,
                                  GDataRequestType type,
                                  string contentType,
                                  string slugHeader,
                                  string etag,
                                  AsyncSendData data)
        {
            Tracing.Assert(targetUri != null, "targetUri should not be null");
            if (targetUri == null)
            {
                throw new ArgumentNullException("targetUri");
            }

            if (inputStream == null)
            {
                Tracing.Assert(inputStream != null, "payload should not be null");
                throw new ArgumentNullException("inputStream");
            }

            if (type != GDataRequestType.Insert && type != GDataRequestType.Update)
            {
                Tracing.Assert(type != GDataRequestType.Insert && type != GDataRequestType.Update, "type needs to be insert or update");
                throw new ArgumentNullException("type");
            }

            IGDataRequest request = this.RequestFactory.CreateRequest(type, targetUri);

            request.Credentials = this.Credentials;

            if (data != null)
            {
                GDataGAuthRequest gr = request as GDataGAuthRequest;
                if (gr != null)
                {
                    gr.AsyncData = data;
                }
            }

            // set the contenttype of the request
            if (contentType != null)
            {
                GDataRequest r = request as GDataRequest;
                if (r != null)
                {
                    r.ContentType = contentType;
                }
            }

            if (slugHeader != null)
            {
                GDataRequest r = request as GDataRequest;
                if (r != null)
                {
                    r.Slug = slugHeader;
                }
            }

            if (etag != null)
            {
                ISupportsEtag ise = request as ISupportsEtag;
                if (ise != null)
                {
                    ise.Etag = etag;
                }
            }

            Stream outputStream = request.GetRequestStream();

            WriteInputStreamToRequest(inputStream, outputStream);

            request.Execute();
            outputStream.Close();
            return(new GDataReturnStream(request));
        }
Пример #9
0
        public IEnumerable <ChannelContact> GetContacts()
        {
            var cred = CredentialsProvider.GetCredentials();
            var rs   = new RequestSettings("Tabdeelee-Inbox2-1", cred.Claim, cred.Evidence)
            {
                AutoPaging = true
            };
            var cr = new ContactsRequest(rs);

            var feed = cr.GetContacts();

            foreach (Contact entry in feed.Entries)
            {
                ChannelContact contact = new ChannelContact();

                contact.Person.Name = entry.Title;
                contact.Profile.ChannelProfileKey = entry.Id;

                if (entry.Phonenumbers.Count > 0)
                {
                    var phone = entry.Phonenumbers.First();
                    contact.Profile.PhoneNr = phone.Value;
                }

                if (entry.PrimaryEmail != null)
                {
                    contact.Profile.SourceAddress = new SourceAddress(entry.PrimaryEmail.Address, contact.Person.Name);
                }

                try
                {
                    // Check for 404 with custom httpclient on photourl since regular HttpWebClient keeps throwing exceptions
                    //var token = cr.Service.QueryAuthenticationToken();
                    //var code = HttpStatusClient.GetStatusCode(entry.PhotoUri.ToString(),
                    //    new Dictionary<string, string> { { "Authorization", "GoogleLogin auth=" + token }});

                    //if (code.HasValue && code == 200)
                    //{
                    IGDataRequest request = cr.Service.RequestFactory.CreateRequest(GDataRequestType.Query, entry.PhotoUri);
                    request.Execute();

                    using (var avatarstream = request.GetResponseStream())
                    {
                        if (avatarstream != null)
                        {
                            ChannelAvatar avatar = new ChannelAvatar();

                            // Copy avatarstream to a new memorystream because the source
                            // stream does not support seek operations.
                            MemoryStream ms = new MemoryStream();
                            avatarstream.CopyTo(ms);

                            avatar.Url           = entry.PhotoUri.ToString();
                            avatar.ContentStream = ms;

                            contact.Profile.ChannelAvatar = avatar;
                        }
                    }
                    //}
                }
                catch (CaptchaRequiredException)
                {
                    // Since GMail will keep on raising CaptchaRequiredException, break out here
                    // todo let the user know in some way or another?
                    yield break;
                }
                catch (Exception)
                {
                }

                yield return(contact);
            }
        }