-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathContentstack.cs
More file actions
105 lines (94 loc) · 3.81 KB
/
Contentstack.cs
File metadata and controls
105 lines (94 loc) · 3.81 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using Contentstack.Management.Core.Tests.Helpers;
using Contentstack.Management.Core.Tests.Model;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Contentstack.Management.Core.Tests
{
public class Contentstack
{
private static readonly Lazy<IConfigurationRoot>
config =
new Lazy<IConfigurationRoot>(() =>
{
return new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
});
private static readonly Lazy<NetworkCredential> credential =
new Lazy<NetworkCredential>(() =>
{
return Config.GetSection("Contentstack:Credentials").Get<NetworkCredential>();
});
private static readonly Lazy<OrganizationModel> organization =
new Lazy<OrganizationModel>(() =>
{
return Config.GetSection("Contentstack:Organization").Get<OrganizationModel>();
});
public static IConfigurationRoot Config{ get { return config.Value; } }
public static NetworkCredential Credential { get { return credential.Value; } }
public static OrganizationModel Organization { get { return organization.Value; } }
public static StackModel Stack { get; set; }
/// <summary>
/// Creates a new ContentstackClient, logs in via the Login API (never from config),
/// and returns the authenticated client. Callers are responsible for calling Logout()
/// when done.
/// </summary>
public static ContentstackClient CreateAuthenticatedClient()
{
ContentstackClientOptions options = Config.GetSection("Contentstack").Get<ContentstackClientOptions>();
options.Authtoken = null;
var handler = new LoggingHttpHandler();
var httpClient = new HttpClient(handler);
var client = new ContentstackClient(httpClient, options);
client.Login(Credential);
return client;
}
public static T serialize<T>(JsonSerializer serializer, string filePath)
{
string response = GetResourceText(filePath);
JObject jObject = JObject.Parse(response);
return jObject.ToObject<T>(serializer);
}
public static T serializeArray<T>(JsonSerializer serializer, string filePath)
{
string response = GetResourceText(filePath);
JArray jObject = JArray.Parse(response);
return jObject.ToObject<T>(serializer);
}
public static string GetResourceText(string resourceName)
{
using (StreamReader reader = new StreamReader(GetResourceStream(resourceName)))
{
return reader.ReadToEnd();
}
}
public static Stream GetResourceStream(string resourceName)
{
Assembly assembly = typeof(Contentstack).Assembly;
var resource = FindResourceName(resourceName);
Stream stream = assembly.GetManifestResourceStream(resource);
return stream;
}
public static string FindResourceName(string partialName)
{
return FindResourceName(s => s.IndexOf(partialName, StringComparison.OrdinalIgnoreCase) >= 0).Single();
}
public static IEnumerable<string> FindResourceName(Predicate<string> match)
{
Assembly assembly = typeof(Contentstack).Assembly;
var allResources = assembly.GetManifestResourceNames();
foreach (var resource in allResources)
{
if (match(resource))
yield return resource;
}
}
}
}