Skip to content

Commit

Permalink
В категорию Polls добавлены отсутствующие методы (#1194)
Browse files Browse the repository at this point in the history
* Реализовал метод getBackgrounds #1130

* Добавил Json для теста метода getBackgrounds #1130

* Реализовал метода getPhotoUploadServer #1130

* Реализовал метод savePhoto #1130
  • Loading branch information
Hiccstrid2019 authored Aug 19, 2021
1 parent c04208c commit 9721e52
Show file tree
Hide file tree
Showing 14 changed files with 407 additions and 7 deletions.
32 changes: 32 additions & 0 deletions VkNet.Tests/Categories/Polls/GetBackgroundsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using NUnit.Framework;
using VkNet.Enums.SafetyEnums;
using VkNet.Tests.Infrastructure;

namespace VkNet.Tests.Categories.Polls
{
[TestFixture]
public class GetBackgroundsTest : CategoryBaseTest
{
protected override string Folder => "Polls";

[Test]
public void GetBackgrounds()
{
Url = "https://api.vk.com/method/polls.getBackgrounds";

ReadCategoryJsonPath(nameof(Api.PollsCategory.GetBackgrounds));

var result = Api.PollsCategory.GetBackgrounds();

Assert.That(result[0].Type, Is.TypeOf<PollBackgroundType>());
Assert.That(result[0].Angle, Is.EqualTo("225"));
Assert.That(result[0].Points[0].Color, Is.EqualTo("f24973"));
Assert.That(result[0].Points[0].Position, Is.EqualTo(0));

Assert.That(result[1].Type, Is.TypeOf<PollBackgroundType>());
Assert.That(result[1].Angle, Is.EqualTo("180"));
Assert.That(result[1].Points[1].Color, Is.EqualTo("2f733f"));
Assert.That(result[1].Points[1].Position, Is.EqualTo(1));
}
}
}
23 changes: 23 additions & 0 deletions VkNet.Tests/Categories/Polls/GetPhotoUploadServerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using NUnit.Framework;
using VkNet.Tests.Infrastructure;

namespace VkNet.Tests.Categories.Polls
{
[TestFixture]
public class GetPhotoUploadServerTest : CategoryBaseTest
{
protected override string Folder => "Polls";

[Test]
public void GetPhotoUploadServer()
{
Url = "https://api.vk.com/method/polls.getPhotoUploadServer";

ReadCategoryJsonPath(nameof(Api.PollsCategory.GetPhotoUploadServer));

var result = Api.PollsCategory.GetPhotoUploadServer(450138623);

Assert.IsNotEmpty(result.UploadUrl);
}
}
}
31 changes: 31 additions & 0 deletions VkNet.Tests/Categories/Polls/PollsSavePhotoTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using NUnit.Framework;
using VkNet.Model.RequestParams;
using VkNet.Tests.Infrastructure;

namespace VkNet.Tests.Categories.Polls
{
[TestFixture]
public class PollsSavePhotoTest : CategoryBaseTest
{
protected override string Folder => "Polls";

[Test]
public void PollsSavePhoto()
{
Url = "https://api.vk.com/method/polls.savePhoto";

ReadCategoryJsonPath(nameof(Api.PollsCategory.SavePhoto));

var result = Api.PollsCategory.SavePhoto(new SavePhotoParams
{
Photo = "242344",
Hash = "fe8f7aaa03ff650cc2"
});

Assert.That(result.Id, Is.EqualTo(457245390));
Assert.That(result.Color, Is.EqualTo("BE272E"));
Assert.That(result.Images[0].Height, Is.EqualTo(600));
Assert.IsNotEmpty(result.Images[0].Url.ToString());
}
}
}
29 changes: 29 additions & 0 deletions VkNet.Tests/TestData/Categories/Polls/GetBackgrounds.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"response": [{
"angle": 225,
"color": "6248cb",
"id": 1,
"name": "фон в тёплых тонах",
"points": [{
"color": "f24973",
"position": 0
}, {
"color": "3948e6",
"position": 1
}],
"type": "gradient"
}, {
"angle": 180,
"color": "4b8642",
"id": 2,
"name": "зелёный фон",
"points": [{
"color": "679945",
"position": 0
}, {
"color": "2f733f",
"position": 1
}],
"type": "gradient"
}]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"response": {
"upload_url": "https://pu.vk.com...jYjEwNzRjMTY4ZTIifQ"
}
}
15 changes: 15 additions & 0 deletions VkNet.Tests/TestData/Categories/Polls/SavePhoto.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"response": {
"color": "BE272E",
"id": 457245390,
"images": [{
"height": 600,
"url": "https://sun9-79.userapi.com/impf/M1JmC0VPcPYhRKB-emOIZwNX3ERNBdhpXHZBzw/gabphYGgM_Y.jpg",
"width": 1001
}, {
"height": 200,
"url": "https://sun9-79.userapi.com/impf/M1JmC0VPcPYhRKB-emOIZwNX3ERNBdhpXHZBzw/gabphYGgM_Y.jpg",
"width": 510
}]
}
}
36 changes: 36 additions & 0 deletions VkNet/Abstractions/Category/Async/IPollsCategoryAsync.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using VkNet.Model;
using VkNet.Model.Attachments;
using VkNet.Model.RequestParams;
Expand Down Expand Up @@ -86,5 +87,40 @@ public interface IPollsCategoryAsync
/// Страница документации ВКонтакте https://vk.com/dev/polls.create
/// </remarks>
Task<Poll> CreateAsync(PollsCreateParams @params);

/// <summary>
/// Получает варианты фонового изображения для опросов.
/// </summary>
/// <returns>
/// Возвращает массив объектов, описывающих фоновое изображение опроса.
/// </returns>
/// <remarks>
/// Страница документации ВКонтакте https://vk.com/dev/polls.getBackgrounds
/// </remarks>
Task<ReadOnlyCollection<GetBackgroundsResult>> GetBackgroundsAsync();

/// <summary>
/// Получает адрес сервера для загрузки фоновой фотографии в опрос.
/// </summary>
/// <param name="ownerId"></param>
/// <returns>
/// Возвращает объект с полем содержащим URL для загрузки фотографии
/// </returns>
/// <remarks>
/// Страница документации ВКонтакте http://vk.com/dev.php?method=polls.getPhotoUploadServer
/// </remarks>
Task<PhotoUploadServer> GetPhotoUploadServerAsync(long ownerId);

/// <summary>
/// Сохраняет фотографию, загруженную в опрос.
/// </summary>
/// <param name="params"></param>
/// <returns>
/// В случае успешного сохранения возвращает объект описывающий фотографию
/// </returns>
/// <remarks>
/// Страница документации ВКонтакте http://vk.com/dev/polls.savePhoto
/// </remarks>
public Task<SavePhotoResult> SavePhotoAsync(SavePhotoParams @params);
}
}
12 changes: 11 additions & 1 deletion VkNet/Abstractions/Category/IPollsCategory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using VkNet.Model;
using System.Collections.ObjectModel;
using VkNet.Model;
using VkNet.Model.Attachments;
using VkNet.Model.RequestParams;
using VkNet.Model.RequestParams.Polls;
Expand Down Expand Up @@ -26,5 +27,14 @@ public interface IPollsCategory : IPollsCategoryAsync

/// <inheritdoc cref="IPollsCategoryAsync.CreateAsync"/>
Poll Create(PollsCreateParams @params);

/// <inheritdoc cref="IPollsCategoryAsync.GetBackgroundsAsync"/>
ReadOnlyCollection<GetBackgroundsResult> GetBackgrounds();

/// <inheritdoc cref="IPollsCategoryAsync.GetPhotoUploadServerAsync"/>
PhotoUploadServer GetPhotoUploadServer(long ownerId);

/// <inheritdoc cref="IPollsCategoryAsync.SavePhotoAsync"/>
SavePhotoResult SavePhoto(SavePhotoParams @params);
}
}
31 changes: 25 additions & 6 deletions VkNet/Categories/Async/PollsCategoryAsync.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using VkNet.Model;
using VkNet.Model.Attachments;
using VkNet.Model.RequestParams;
Expand All @@ -13,37 +14,55 @@ public partial class PollsCategory
/// <inheritdoc />
public Task<Poll> GetByIdAsync(PollsGetByIdParams @params)
{
return TypeHelper.TryInvokeMethodAsync(func: () =>GetById(@params: @params));
return TypeHelper.TryInvokeMethodAsync(func: () => GetById(@params: @params));
}

/// <inheritdoc />
public Task<bool> EditAsync(PollsEditParams @params)
{
return TypeHelper.TryInvokeMethodAsync(func: () =>Edit(@params: @params));
return TypeHelper.TryInvokeMethodAsync(func: () => Edit(@params: @params));
}

/// <inheritdoc />
public Task<bool> AddVoteAsync(PollsAddVoteParams @params)
{
return TypeHelper.TryInvokeMethodAsync(func: () =>AddVote(@params: @params));
return TypeHelper.TryInvokeMethodAsync(func: () => AddVote(@params: @params));
}

/// <inheritdoc />
public Task<bool> DeleteVoteAsync(PollsDeleteVoteParams @params)
{
return TypeHelper.TryInvokeMethodAsync(func: () =>DeleteVote(@params: @params));
return TypeHelper.TryInvokeMethodAsync(func: () => DeleteVote(@params: @params));
}

/// <inheritdoc />
public Task<VkCollection<PollAnswerVoters>> GetVotersAsync(PollsGetVotersParams @params)
{
return TypeHelper.TryInvokeMethodAsync(func: () =>GetVoters(@params: @params));
return TypeHelper.TryInvokeMethodAsync(func: () => GetVoters(@params: @params));
}

/// <inheritdoc />
public Task<Poll> CreateAsync(PollsCreateParams @params)
{
return TypeHelper.TryInvokeMethodAsync(func: () =>Create(@params: @params));
return TypeHelper.TryInvokeMethodAsync(func: () => Create(@params: @params));
}

/// <inheritdoc />
public Task<ReadOnlyCollection<GetBackgroundsResult>> GetBackgroundsAsync()
{
return TypeHelper.TryInvokeMethodAsync(func: () => GetBackgrounds());
}

/// <inheritdoc />
public Task<PhotoUploadServer> GetPhotoUploadServerAsync(long ownerId)
{
return TypeHelper.TryInvokeMethodAsync(func: () => GetPhotoUploadServer(ownerId));
}

/// <inheritdoc />
public Task<SavePhotoResult> SavePhotoAsync(SavePhotoParams @params)
{
return TypeHelper.TryInvokeMethodAsync(func: () => SavePhoto(@params: @params));
}
}
}
26 changes: 26 additions & 0 deletions VkNet/Categories/PollsCategory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using VkNet.Abstractions;
using VkNet.Model;
Expand Down Expand Up @@ -129,5 +130,30 @@ public Poll Create(PollsCreateParams @params)
{ "background_id", @params.BackgroundId }
});
}

/// <inheritdoc />
public ReadOnlyCollection<GetBackgroundsResult> GetBackgrounds()
{
return _vk.Call<ReadOnlyCollection<GetBackgroundsResult>>("polls.getBackgrounds",
new VkParameters());
}

/// <inheritdoc />
public PhotoUploadServer GetPhotoUploadServer(long ownerId)
{
return _vk.Call<PhotoUploadServer>("polls.getPhotoUploadServer",
new VkParameters { {"owner_id", ownerId} });
}

/// <inheritdoc />
public SavePhotoResult SavePhoto(SavePhotoParams @params)
{
return _vk.Call<SavePhotoResult>("polls.savePhoto",
new VkParameters
{
{ "photo", @params.Photo },
{ "hash", @params.Hash }
});
}
}
}
22 changes: 22 additions & 0 deletions VkNet/Model/RequestParams/Polls/SavePhotoParams.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using Newtonsoft.Json;

namespace VkNet.Model.RequestParams
{
/// <summary>
/// Параметры для метода SavePhoto
/// </summary>
[Serializable]
public class SavePhotoParams
{
/// <summary>
/// Строка полученная в результате загрузки фотографии.
/// </summary>
public string Photo { get; set; }

/// <summary>
/// Хеш полученный в результате загрузки фотографии.
/// </summary>
public string Hash { get; set; }
}
}
Loading

0 comments on commit 9721e52

Please sign in to comment.