public IAsyncOperation<string> DummyEchoAsync(string echoStr)
        {
            try
            {
                using (EnsureCredentialsUseContext context = new EnsureCredentialsUseContext(
                    this.Username, this.Password, this.Accesskey, _client.InnerChannel))
                {
                    return _client
                        .DummyEchoAsync(echoStr)
                        .ContinueWith(t => t.Result.DummyEchoResult)
                        .AsAsyncOperation();
                }
            }
            catch (FaultException faultException)
            {
                MessageFault messageFault = faultException.CreateMessageFault();

                if (messageFault.HasDetail)
                {
                    string innerErrorXml;
                    using (var xmlReader = messageFault.GetReaderAtDetailContents())
                    {
                        innerErrorXml = xmlReader.ReadInnerXml();
                    }
                    if (innerErrorXml != null)
                        throw new Exception(innerErrorXml);
                    throw;
                }
            }
            throw new Exception("Request cannot be fulfilled");
        }
 public async Task<string[]> GetUserTags()
 {
     using (EnsureCredentialsUseContext context = new EnsureCredentialsUseContext(
             this.Username, this.Password, this.Accesskey, _client.InnerChannel))
     {
         return await this._client
                          .UserGetTagsAsync(null, null)
                          .ContinueWith(t => t.Result.tags);
     }
 }
 public async Task<Image[]> GetUserImageList()
 {
     using (EnsureCredentialsUseContext context = new EnsureCredentialsUseContext(
             this.Username, this.Password, this.Accesskey, _client.InnerChannel))
     {
         return await this._client
             .ImageGetListByUserAsync(0, null)
             .ContinueWith(t => t.Result.images);
     }
 }
        public async Task<Album[]> GetUserAlbumsList()
        {
            User user = new User()
            {
                username = "******",
            };

            try
            {
                using (EnsureCredentialsUseContext context = new EnsureCredentialsUseContext(
                    this.Username, this.Password, this.Accesskey, _client.InnerChannel))
                {
                    return await this._client
                        .AlbumGetListByUserAsync(user, 0, null, FotosSapoPtInterface)
                        .ContinueWith(t => t.Result.albums);
                }
            }
            catch (FaultException faultException)
            {
                MessageFault messageFault = faultException.CreateMessageFault();

                if (messageFault.HasDetail)
                {
                    string innerErrorXml;
                    using (var xmlReader = messageFault.GetReaderAtDetailContents())
                    {
                        innerErrorXml = xmlReader.ReadInnerXml();
                    }
                    if (innerErrorXml != null)
                        throw new Exception(innerErrorXml);
                    throw;
                }
            }
            throw new Exception("Request cannot be fulfilled");
        }
        public async Task<bool> AddImageToAlbums(string imageId, Album[] albums)
        {
            Image img = new Image { uid = imageId, albums = albums };

            try
            {
                using (EnsureCredentialsUseContext context = new EnsureCredentialsUseContext(
                    this.Username, this.Password, this.Accesskey, _client.InnerChannel))
                {
                    return await this._client
                                     .ImageAddToAlbumAsync(img, PhotosServiceClient.FotosSapoPtInterface)
                                     .ContinueWith(t => t.Result.result.ok);
                }
            }
            catch (FaultException faultException)
            {
                MessageFault messageFault = faultException.CreateMessageFault();

                if (messageFault.HasDetail)
                {
                    string innerErrorXml;
                    using (var xmlReader = messageFault.GetReaderAtDetailContents())
                    {
                        innerErrorXml = xmlReader.ReadInnerXml();
                    }
                    if (innerErrorXml != null)
                        throw new Exception(innerErrorXml);
                    throw;
                }
            }
            throw new Exception("Request cannot be fulfilled");
        }
        public async Task<Album> CreateAlbumAsync(Album album)
        {
            try
            {
                using (EnsureCredentialsUseContext context = new EnsureCredentialsUseContext(
                    this.Username, this.Password, this.Accesskey, _client.InnerChannel))
                {
                    return await this._client
                                     .AlbumCreateAsync(album)
                                     .ContinueWith(t =>
                                                       {
                                                           if (t.Result.result.ok)
                                                               return t.Result.album;
                                                           throw new Exception("Server returned error");
                                                       });
                }
            }
            catch (FaultException faultException)
            {
                MessageFault messageFault = faultException.CreateMessageFault();

                if (messageFault.HasDetail)
                {
                    string innerErrorXml;
                    using (var xmlReader = messageFault.GetReaderAtDetailContents())
                    {
                        innerErrorXml = xmlReader.ReadInnerXml();
                    }
                    if (innerErrorXml != null)
                        throw new Exception(innerErrorXml);
                    throw;
                }
            }
            throw new Exception("Request cannot be fulfilled");
        }
        public async Task<CreateImageResult> CreateImageAsync(IStorageFile file, Image img)
        {
            ImageCreateResult res = null;
            try
            {
                using (EnsureCredentialsUseContext context = new EnsureCredentialsUseContext(
                    this.Username, this.Password, this.Accesskey, _client.InnerChannel))
                {
                    res = await this._client.ImageCreateAsync(img, FotosSapoPtInterface);
                }
            }
            catch (FaultException faultException)
            {
                MessageFault messageFault = faultException.CreateMessageFault();

                if (messageFault.HasDetail)
                {
                    string innerErrorXml;
                    using (var xmlReader = messageFault.GetReaderAtDetailContents())
                    {
                        innerErrorXml = xmlReader.ReadInnerXml();
                    }
                    if (innerErrorXml != null)
                        throw new Exception(innerErrorXml);
                    throw;
                }
            }
            if (res != null && res.result.ok)
            {
                BackgroundUploader uploader = new BackgroundUploader();

                //Default HTTP Method is POST (http://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.networking.backgroundtransfer.backgrounduploader.method.aspx)
                //uploader.Method = "POST";

                UploadOperation uploadOperation = 
                    await PhotosServiceClient.CreateUploadOperationForCreateImage(file, res.token, uploader);

                await uploadOperation.StartAsync();

                ResponseInformation responseInformation = uploadOperation.GetResponseInformation();

                uint contentLength = Convert.ToUInt32(responseInformation.Headers["Content-Length"]);

                uint statusCode = responseInformation.StatusCode;

                IInputStream resultStreamAt = uploadOperation.GetResultStreamAt(0);

                IBuffer result = await resultStreamAt.ReadAsync(
                    new Windows.Storage.Streams.Buffer(contentLength),
                    contentLength,
                    InputStreamOptions.None);

                Stream responseStream = result.AsStream();

                XDocument xDocument = XDocument.Load(responseStream);

                /*
                 
                 * Bug!! Root element ("Upload") as diferent cases in error and in success responses
                 
                 * Must ask SAPO Photos Team about the latency at User creation
                 
                 * How can an application post photos for a user not being that user? Seems to be possible.
                 
                 * Return usefull information 
                 
                 */

                string resultCode =
                    xDocument
                        .Descendants("uploadPost")
                        .Select(e => e.Element("Result").Value)
                        .SingleOrDefault();
                return new CreateImageResult(res.image, resultCode);
            }
            throw new Exception("Request cannot be fulfilled");
        }