-
Notifications
You must be signed in to change notification settings - Fork 1
/
Media.cs
64 lines (53 loc) · 2.28 KB
/
Media.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
using System;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using SitecoreGraphqlmport.Model;
namespace SitecoreGraphqlmport
{
internal class Media
{
/// <summary>
/// Sample method which calls a GraphQL endpoint.
/// </summary>
internal static async Task RequestUploadMedia(CancellationToken cancellationToken, string itemPath, string file)
{
string graphqlendpoint = System.Configuration.ConfigurationManager.AppSettings.Get("graphqlAuthoringUrl");
string accessToken = System.Configuration.ConfigurationManager.AppSettings.Get("accessToken");
Console.WriteLine("mutation uploadMedia");
// Call GraphQL endpoint here, specifying return data type, endpoint, method, query, and variables
var result = await Request.CallGraphQLAsync<UploadMedia>(
new Uri(graphqlendpoint),
HttpMethod.Post,
accessToken,
"",
"mutation { uploadMedia(input: { itemPath: \"" + itemPath + "\" }) { presignedUploadUrl }}",
new
{
},
cancellationToken);
// Examine the GraphQL response to see if any errors were encountered
if (result.Errors?.Count > 0)
{
Console.WriteLine($"GraphQL returned errors:\n{string.Join("\n", result.Errors.Select(x => $" - {x.Message}"))}");
return;
}
// Use the response data
Console.WriteLine($"Found url: {result.Data.uploadMedia.presignedUploadUrl}");
var result2 = await Request.CallGraphQLUploadAsync<Uploaded>(
HttpMethod.Post,
accessToken,
result.Data.uploadMedia.presignedUploadUrl,
file,
cancellationToken);
// Examine the response to see if any errors were encountered
if (!string.IsNullOrEmpty(result2.Message))
{
Console.WriteLine($"upload returned errors:{result2.Message}");
return;
}
Console.WriteLine($"media uploaded id=: {result2.Id}");
}
}
}