-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathQuery.cs
More file actions
134 lines (123 loc) · 4.73 KB
/
Query.cs
File metadata and controls
134 lines (123 loc) · 4.73 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
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
using System;
using System.Threading.Tasks;
using Contentstack.Management.Core.Models;
using Contentstack.Management.Core.Services;
using Contentstack.Management.Core.Utils;
namespace Contentstack.Management.Core.Queryable
{
public class Query
{
private readonly Stack _stack;
private readonly string _resourcePath;
private readonly ParameterCollection _collection = new ParameterCollection();
private readonly string _apiVersion;
internal Query(Stack stack, string resourcePath, string apiVersion = null)
{
if(stack == null)
{
throw new ArgumentNullException("stack", CSConstants.StackRequired);
}
if (resourcePath== null)
{
throw new ArgumentNullException("resourcePath", CSConstants.ResourcePathRequired);
}
_stack = stack;
_resourcePath = resourcePath;
_apiVersion = apiVersion;
}
#region Public
/// <summary>
/// The ‘limit’ parameter will return a specific number of Objects in the output.
/// </summary>
/// <param name="value">Number of object in limit</param>
/// <example>
/// <pre><code>
/// ContentstackClient client = new ContentstackClient("<AUTHTOKEN>", "<API_HOST>");
/// ContentstackResponse contentstackResponse = client.Stack().Query().Limit(5).Find();
/// </code></pre>
/// </example>
/// <returns>The <see cref="Query"/>.</returns>
public Query Limit(double value)
{
_collection.Add("limit", value);
return this;
}
/// <summary>
/// The ‘skip’ parameter will skip a specific number of Object in the output.
/// </summary>
/// <param name="value">Number of object to skip</param>
/// <example>
/// <pre><code>
/// ContentstackClient client = new ContentstackClient("<AUTHTOKEN>", "<API_HOST>");
/// ContentstackResponse contentstackResponse = client.Stack().Query().Skip(5).Find();
/// </code></pre>
/// </example>
/// <returns>The <see cref="Query"/>.</returns>
public Query Skip(double value)
{
_collection.Add("skip", value);
return this;
}
/// <summary>
/// The ‘include_count’ parameter returns the total number of object related to the user.
/// </summary>
/// <example>
/// <pre><code>
/// ContentstackClient client = new ContentstackClient("<AUTHTOKEN>", "<API_HOST>");
/// ContentstackResponse contentstackResponse = client.Stack().Query().IncludeCount().Find();
/// </code></pre>
/// </example>
/// <returns>The <see cref="Query"/>.</returns>
public Query IncludeCount()
{
_collection.Add("include_count", true);
return this;
}
/// <summary>
/// The Find all object call fetches the list of all objects owned by a particular user account.
/// </summary>
/// <returns>The <see cref="ContentstackResponse"/></returns>
public ContentstackResponse Find(ParameterCollection collection = null)
{
_stack.ThrowIfNotLoggedIn();
this.ThrowIfAPIKeyEmpty();
if (collection != null)
{
foreach (var kvp in collection)
{
_collection.Add(kvp.Key, kvp.Value);
}
}
var service = new QueryService(_stack, _collection, _resourcePath);
return _stack.client.InvokeSync(service, false, _apiVersion);
}
/// <summary>
/// The Find all object call fetches the list of all objects owned by a particular user account.
/// </summary>
/// <returns>The Task</returns>
public Task<ContentstackResponse> FindAsync(ParameterCollection collection = null)
{
_stack.ThrowIfNotLoggedIn();
this.ThrowIfAPIKeyEmpty();
if (collection != null)
{
foreach (var kvp in collection)
{
_collection.Add(kvp.Key, kvp.Value);
}
}
var service = new QueryService(_stack, _collection, _resourcePath);
return _stack.client.InvokeAsync<QueryService, ContentstackResponse>(service, false, _apiVersion);
}
#endregion
#region Throw Error
internal void ThrowIfAPIKeyEmpty()
{
if (string.IsNullOrEmpty(_stack.APIKey))
{
throw new InvalidOperationException(CSConstants.MissingAPIKey);
}
}
#endregion
}
}