-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEntities.cs
158 lines (147 loc) · 7.25 KB
/
Entities.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WitSharp.Objects;
namespace WitSharp
{
public class Entities : Base
{
internal Entities()
{
}
/// <summary>Returns a list of all entities.</summary>
public async Task<IEnumerable<string>> GetAllAsync()
{
return await ProcessAsync<IEnumerable<string>>(await RestClient.GetAsync("entities"));
}
/// <summary>Creates a new entity.</summary>
/// <param name="name">ID or name of the requested entity.</param>
/// <param name="description">Short sentence describing this entity.</param>
public async Task CreateAsync(string name, string description)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentNullException(nameof(name));
if (string.IsNullOrWhiteSpace(description))
throw new ArgumentNullException(nameof(description));
Process(await RestClient.PostAsync("entities", CreateContent(new
{
Id = name,
doc = description
})));
}
/// <summary>Returns all the expressions validated for an entity.</summary>
/// <param name="name">ID or name of the requested entity..</param>
public async Task<EntityValuesObject> GetAsync(string name)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentNullException(nameof(name));
return await ProcessAsync<EntityValuesObject>(
await RestClient.GetAsync($"entities/{name}"));
}
/// <summary>Updates an entity.</summary>
/// <param name="id">ID or name of the entity.</param>
/// <param name="description">Short sentence describing this entity.</param>
/// <param name="lookups">
/// Short sentence describing this entity. Current lookup strategies are: free_text, keywords.
/// You can add both as well.
/// </param>
/// <param name="values">Possible values if this is a keyword entity.</param>
public async Task UpdateAsync(string id, string description = null,
string[] lookups = null, Values[] values = null)
{
if (string.IsNullOrWhiteSpace(id))
throw new ArgumentNullException(nameof(id));
var isKeyword =
(lookups ?? throw new ArgumentNullException(nameof(lookups))).Any(x => x?.ToLower() == "keywords");
if (values != null && !isKeyword)
throw new InvalidOperationException("Values are only allowed if lookup method is keywords.");
var get = await RestClient.PutAsync($"entities/{id}", CreateContent(
new
{
Id = id,
doc = description,
lookups,
values
}));
Process(get);
}
/// <summary>Permanently deletes the entity.</summary>
/// <param name="id">ID or name of the entity.</param>
public async Task DeleteAsync(string id)
{
if (string.IsNullOrWhiteSpace(id))
throw new ArgumentNullException(nameof(id));
Process(await RestClient.DeleteAsync($"entities/{id}"));
}
/// <summary>Permanently delete the role associated to the entity.</summary>
/// <param name="entityId">ID or name of the entity.</param>
/// <param name="roleId">ID or name of the role associate to the entity.</param>
public async Task DeleteRoleAsync(string entityId, string roleId)
{
if (string.IsNullOrWhiteSpace(entityId))
throw new ArgumentNullException(nameof(entityId));
if (string.IsNullOrWhiteSpace(roleId))
throw new ArgumentNullException(nameof(roleId));
Process(await RestClient.DeleteAsync($"entities/{entityId}:{roleId}"));
}
/// <summary>Add a possible value into the list of values for the keyword entity.</summary>
/// <param name="id">ID or name of the entity</param>
/// <param name="value">
/// <see cref="Values" />
/// </param>
public async Task<KeywordObject> AddValueAsync(string id, Values value)
{
if (string.IsNullOrWhiteSpace(value.Value))
throw new ArgumentNullException(nameof(id));
if (value == null)
throw new ArgumentNullException(nameof(value));
var get = await RestClient.PostAsync($"entities/{id}/values", CreateContent(value));
return await ProcessAsync<KeywordObject>(get);
}
/// <summary>Delete a canonical value from the entity</summary>
/// <param name="id">ID or name of the entity</param>
/// <param name="value">Id or name of the value.</param>
public async Task DeleteValueAsync(string id, string value)
{
if (string.IsNullOrWhiteSpace(id))
throw new ArgumentNullException(nameof(id));
if (string.IsNullOrWhiteSpace(value))
throw new ArgumentNullException(nameof(value));
Process(await RestClient.DeleteAsync($"entities/{id}/values/{value}"));
}
/// <summary>Create a new expression of the canonical value of the keyword entity</summary>
/// <param name="id">ID or name of the entity</param>
/// <param name="value">Id or name of associated entity value.</param>
/// <param name="expression">
/// new expression for the canonical value of the entity.
/// Must be shorter than 256 characters.
/// </param>
public async Task<KeywordObject> AddExpressionAsync(string id, string value, string expression)
{
if (string.IsNullOrWhiteSpace(id))
throw new ArgumentNullException(nameof(id));
if (string.IsNullOrWhiteSpace(value))
throw new ArgumentNullException(nameof(value));
if (expression.Length > 256)
throw new ArgumentOutOfRangeException(nameof(expression), "Must be less than 256 characters.");
var get = await RestClient.PostAsync($"entities/{id}/values/{value}/expressions",
CreateContent(new {expression}));
return await ProcessAsync<KeywordObject>(get);
}
/// <summary>Delete an expression of the canonical value of the entity.</summary>
/// <param name="id">ID or name of the entity</param>
/// <param name="value">Id or name of associated entity value.</param>
/// <param name="expression">new expression for the canonical value of the entity. Must be shorter than 256 characters.</param>
public async Task DeleteExpressionAsync(string id, string value, string expression)
{
if (string.IsNullOrWhiteSpace(id))
throw new ArgumentNullException(nameof(id));
if (string.IsNullOrWhiteSpace(value))
throw new ArgumentNullException(nameof(value));
if (string.IsNullOrWhiteSpace(expression))
throw new ArgumentNullException(expression);
Process(await RestClient.DeleteAsync($"entities/{id}/values/{value}/expressions/{expression}"));
}
}
}