コード例 #1
0
ファイル: Album.cs プロジェクト: DL444/DL444.ImgurUwp
        public async Task <(string id, string deleteHash)> CreateAlbumAsync(IEnumerable <string> imageIds = null, string title   = null, string description                = null,
                                                                            AlbumPrivacy?privacy          = null, string coverId = null, IEnumerable <string> deleteHashes = null)
        {
            if (coverId != null)
            {
                if (imageIds == null && deleteHashes == null)
                {
                    // All null
                    throw new ArgumentException("The specified cover is not in this album.");
                }
                else if (deleteHashes == null)
                {
                    // imageIds not null
                    if (!imageIds.Contains(coverId))
                    {
                        throw new ArgumentException("The specified cover is not in this album.");
                    }
                }
                else if (imageIds == null)
                {
                    // deleteHashes not null
                    if (!deleteHashes.Contains(coverId))
                    {
                        throw new ArgumentException("The specified cover is not in this album.");
                    }
                }
                else
                {
                    // All not null
                    if ((!imageIds.Contains(coverId)) && (!deleteHashes.Contains(coverId)))
                    {
                        throw new ArgumentException("The specified cover is not in this album.");
                    }
                }
            }

            AlbumCreateParams album = new AlbumCreateParams(title, description, privacy, imageIds, deleteHashes, coverId);

            HttpRequestMessage msg = new HttpRequestMessage(HttpMethod.Post, "/3/album");

            msg.Content = new StringContent(JsonConvert.SerializeObject(album));
            msg.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
            var response = await client.SendAsync(msg);

            (bool success, int status, string dataJson) = GetDataToken(await response.Content.ReadAsStringAsync());
            if (success)
            {
                JObject obj = JObject.Parse(dataJson);
                return(obj["id"].ToObject <string>(), obj["deletehash"].ToObject <string>());
            }
            else
            {
                throw new ApiRequestException(dataJson)
                      {
                          Status = status
                      };
            }
        }
コード例 #2
0
ファイル: Album.cs プロジェクト: DL444/DL444.ImgurUwp
        public async Task <bool> UpdateAlbumInfoAsync(string id, IEnumerable <string> imageIds = null, string title = null, string description = null,
                                                      AlbumPrivacy?privacy = null, string coverId = null, IEnumerable <string> deleteHashes    = null)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }

            // Cover not in album is not important after all.
            //if (coverId != null)
            //{
            //    if (imageIds == null && deleteHashes == null)
            //    {
            //        // All null
            //        throw new ArgumentException("The specified cover is not in this album.");
            //    }
            //    else if (deleteHashes == null)
            //    {
            //        // imageIds not null
            //        if (!imageIds.Contains(coverId))
            //        {
            //            throw new ArgumentException("The specified cover is not in this album.");
            //        }
            //    }
            //    else if (imageIds == null)
            //    {
            //        // deleteHashes not null
            //        if (!deleteHashes.Contains(coverId))
            //        {
            //            throw new ArgumentException("The specified cover is not in this album.");
            //        }
            //    }
            //    else
            //    {
            //        // All not null
            //        if ((!imageIds.Contains(coverId)) && (!deleteHashes.Contains(coverId)))
            //        {
            //            throw new ArgumentException("The specified cover is not in this album.");
            //        }
            //    }
            //}

            AlbumCreateParams album = new AlbumCreateParams(title, description, privacy, imageIds, deleteHashes, coverId);

            HttpRequestMessage msg = new HttpRequestMessage(HttpMethod.Post, $"/3/album/{id}");

            msg.Content = new StringContent(JsonConvert.SerializeObject(album));
            msg.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
            var response = await client.SendAsync(msg);

            (bool success, int status, string dataJson) = GetDataToken(await response.Content.ReadAsStringAsync());
            if (success)
            {
                return(JsonConvert.DeserializeObject <bool>(dataJson.ToLower()));
            }
            else
            {
                throw new ApiRequestException(dataJson)
                      {
                          Status = status
                      };
            }
        }