/
AssetService.cs
166 lines (147 loc) · 5.84 KB
/
AssetService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright (c) Bynder. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Bynder.Sdk.Service.Upload;
using Bynder.Sdk.Api.Requests;
using Bynder.Sdk.Api.RequestSender;
using Bynder.Sdk.Model;
using Bynder.Sdk.Query.Asset;
using Bynder.Sdk.Query.Upload;
namespace Bynder.Sdk.Service.Asset
{
/// <summary>
/// Implementation of <see cref="IAssetService"/>
/// </summary>
internal class AssetService : IAssetService
{
/// <summary>
/// Request sender to communicate with the Bynder API
/// </summary>
private IApiRequestSender _requestSender;
/// <summary>
/// Instance to upload file to Bynder
/// </summary>
private FileUploader _uploader;
/// <summary>
/// Initializes a new instance of the class
/// </summary>
/// <param name="requestSender">instance to communicate with the Bynder API</param>
public AssetService(IApiRequestSender requestSender)
{
_requestSender = requestSender;
_uploader = FileUploader.Create(_requestSender);
}
/// <summary>
/// Check <see cref="IAssetService"/> for more information
/// </summary>
/// <returns>Check <see cref="IAssetService"/> for more information</returns>
public Task<IList<Brand>> GetBrandsAsync()
{
var request = new ApiRequest<IList<Brand>>
{
Path = "/api/v4/brands/",
HTTPMethod = HttpMethod.Get
};
return _requestSender.SendRequestAsync(request);
}
/// <summary>
/// Check <see cref="IAssetService"/> for more information
/// </summary>
/// <returns>Check <see cref="IAssetService"/> for more information</returns>
public Task<IDictionary<string, Metaproperty>> GetMetapropertiesAsync()
{
var request = new ApiRequest<IDictionary<string, Metaproperty>>
{
Path = "/api/v4/metaproperties/",
HTTPMethod = HttpMethod.Get
};
return _requestSender.SendRequestAsync(request);
}
/// <summary>
/// Check <see cref="IAssetService"/> for more information
/// </summary>
/// <param name="query">Check <see cref="IAssetService"/> for more information</param>
/// <returns>Check <see cref="IAssetService"/> for more information</returns>
public Task<IList<Media>> GetMediaListAsync(MediaQuery query)
{
var request = new ApiRequest<IList<Media>>
{
Path = "/api/v4/media/",
HTTPMethod = HttpMethod.Get,
Query = query
};
return _requestSender.SendRequestAsync(request);
}
/// <summary>
/// Check <see cref="IAssetService"/> for more information
/// </summary>
/// <param name="query">Check <see cref="IAssetService"/> for more information</param>
/// <returns>Check <see cref="IAssetService"/> for more information</returns>
public async Task<Uri> GetDownloadFileUrlAsync(DownloadMediaQuery query)
{
string path = string.Empty;
if (query.MediaItemId == null)
{
path = $"/api/v4/media/{query.MediaId}/download/";
}
else
{
path = $"/api/v4/media/{query.MediaId}/download/{query.MediaItemId}/";
}
var request = new ApiRequest<DownloadFileUrl>
{
Path = path,
HTTPMethod = HttpMethod.Get
};
var downloadFileInformation = await _requestSender.SendRequestAsync(request).ConfigureAwait(false);
return downloadFileInformation.S3File;
}
/// <summary>
/// Check <see cref="IAssetService"/> for more information
/// </summary>
/// <param name="query">Check <see cref="IAssetService"/> for more information</param>
/// <returns>Check <see cref="IAssetService"/> for more information</returns>
public Task UploadFileAsync(UploadQuery query)
{
return _uploader.UploadFile(query);
}
/// <summary>
/// Check <see cref="IAssetService"/> for more information
/// </summary>
/// <param name="query">Check <see cref="IAssetService"/> for more information</param>
/// <returns>Check <see cref="IAssetService"/> for more information</returns>
public Task<Media> GetMediaInfoAsync(MediaInformationQuery query)
{
if (string.IsNullOrEmpty(query.MediaId))
{
throw new ArgumentNullException(nameof(query), "Parameter cannot be null or empty.");
}
var request = new ApiRequest<Media>
{
Path = $"/api/v4/media/{query.MediaId}/",
HTTPMethod = HttpMethod.Get,
Query = query
};
return _requestSender.SendRequestAsync(request);
}
/// <summary>
/// Check <see cref="IAssetService"/> for more information
/// </summary>
/// <param name="query">Check <see cref="IAssetService"/> for more information</param>
/// <returns>Check <see cref="IAssetService"/> for more information</returns>
public Task ModifyMediaAsync(ModifyMediaQuery query)
{
var request = new ApiRequest<string>
{
Path = $"/api/v4/media/{query.MediaId}/",
HTTPMethod = HttpMethod.Post,
Query = query,
DeserializeResponse = false
};
return _requestSender.SendRequestAsync(request);
}
}
}