-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtils.cs
80 lines (74 loc) · 3.84 KB
/
Utils.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
using System;
using System.Collections.Generic;
using System.Linq;
using MondayApi.Schema;
namespace MondayApi.Utils {
public class Utils {
// Copied from old version of RestSharp.Validation
/// <summary>Require a parameter to not be null</summary>
/// <param name="argumentName">Name of the parameter</param>
/// <param name="argumentValue">Value of the parameter</param>
public static void RequireArgument(string argumentName, object argumentValue) {
if (argumentValue == null)
throw new ArgumentNullException(argumentName);
}
public static GraphQlQueryParameter<T> GetParameter<T>(T value, bool isNullable = true) =>
value == null ? null : new GraphQlQueryParameter<T>(null, defaultValue: value, isNullable);
private static readonly Newtonsoft.Json.Serialization.SnakeCaseNamingStrategy snakeCaseNamingStrategy = new Newtonsoft.Json.Serialization.SnakeCaseNamingStrategy();
private static readonly Newtonsoft.Json.JsonSerializerSettings settings = new Newtonsoft.Json.JsonSerializerSettings() {
ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver() { NamingStrategy = snakeCaseNamingStrategy },
NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore,
};
static Utils() => settings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter() { NamingStrategy = snakeCaseNamingStrategy });
private static object convertColumn(IColumnValue column) {
column.ID = null;
switch (column) {
case TextValue tv:
return tv.Text ?? tv.Value;
case NumbersValue nv:
return nv.Number ?? nv.Text ?? nv.Value;
case PeopleValue pv:
return new PeopleValueForUpdate() {
PersonsAndTeams = pv.PersonsAndTeams,
};
case BoardRelationValue bv:
return new BoardRelationValueForUpdate() {
ItemIDs = bv.LinkedItemIDs,
};
case PhoneValue pv:
return new PhoneValueForUpdate() {
Phone = pv.Phone,
CountryShortName = pv.CountryShortName?.ToUpperInvariant(),
};
case DropdownValue dv:
return new DropdownValueForUpdate() {
IDs = dv.Values.Select(v => v.ID).ToList(),
Labels = dv.Values.Select(v => v.Label).ToList(),
};
case CheckboxValue cv:
return cv.Checked.HasValue && cv.Checked.Value
? new CheckboxValueForUpdate() { Checked = "true" }
: null;
default:
return column;
}
}
public static string SerializeColumnValues(IEnumerable<IColumnValue> columnValues) {
if (columnValues == null)
return null;
var dict = columnValues.ToDictionary<IColumnValue, string, object>(column => column.ID, convertColumn);
return Newtonsoft.Json.JsonConvert.SerializeObject(dict, settings);
}
public static string SerializeColumnValue(IColumnValue columnValue) =>
Newtonsoft.Json.JsonConvert.SerializeObject(convertColumn(columnValue), settings);
internal static bool TryDeserializeMondayApiError(string response, out MondayApiError mondayApiError) {
try {
mondayApiError = Newtonsoft.Json.JsonConvert.DeserializeObject<MondayApiError>(response, settings);
return true;
} catch (Newtonsoft.Json.JsonReaderException) {
mondayApiError = null;
return false;
}
}
}
}