Skip to content

Commit 9812873

Browse files
committed
➕ automatic retrieval of WADL for ICTime plugin API
1 parent 2754b29 commit 9812873

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

jwl.jira.ictime/ComposedResourceMethod.cs renamed to jwl.jira.ictime/ComposedWadlMethodDefinition.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Collections.Generic;
44
using System.Net.Http;
55

6-
public struct ComposedResourceMethod
6+
public struct ComposedWadlMethodDefinition
77
{
88
public string ResourcePath;
99
public HttpMethod HttpMethod;

jwl.jira.ictime/WadlApplicationExt.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77
public static class WadlApplicationExt
88
{
9-
public static IEnumerable<ComposedResourceMethod> AsEnumerable(this WadlApplication self)
9+
public static IEnumerable<ComposedWadlMethodDefinition> AsEnumerable(this WadlApplication self)
1010
{
1111
if (self.Resources == null)
12-
return Enumerable.Empty<ComposedResourceMethod>();
12+
return Enumerable.Empty<ComposedWadlMethodDefinition>();
1313
else
1414
return FlattenWadlResources("/", Enumerable.Empty<WadlResourceParameter>(), self.Resources);
1515
}
1616

17-
private static IEnumerable<ComposedResourceMethod> FlattenWadlResources(string parentPath, IEnumerable<WadlResourceParameter> parentParameters, IEnumerable<WadlResource> resources)
17+
private static IEnumerable<ComposedWadlMethodDefinition> FlattenWadlResources(string parentPath, IEnumerable<WadlResourceParameter> parentParameters, IEnumerable<WadlResource> resources)
1818
{
1919
foreach (WadlResource res in resources)
2020
{
@@ -27,7 +27,7 @@ private static IEnumerable<ComposedResourceMethod> FlattenWadlResources(string p
2727
{
2828
foreach (WadlResourceMethod method in res.Methods)
2929
{
30-
yield return new ComposedResourceMethod()
30+
yield return new ComposedWadlMethodDefinition()
3131
{
3232
Id = method.Id,
3333
ResourcePath = resourcePath,
@@ -41,7 +41,7 @@ private static IEnumerable<ComposedResourceMethod> FlattenWadlResources(string p
4141

4242
if (res.Resources != null)
4343
{
44-
foreach (ComposedResourceMethod resMethod in FlattenWadlResources(resourcePath, resourceParameters, res.Resources))
44+
foreach (ComposedWadlMethodDefinition resMethod in FlattenWadlResources(resourcePath, resourceParameters, res.Resources))
4545
yield return resMethod;
4646
}
4747
}

jwl.jira/JiraWithICTimePluginApi.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
namespace jwl.jira;
22

33
using System.Net.Http.Json;
4+
using System.Xml.Serialization;
45
using jwl.infra;
6+
using jwl.wadl;
57

68
// https://interconcept.atlassian.net/wiki/spaces/ICTIME/pages/31686672/API
79
// https://interconcept.atlassian.net/wiki/spaces/ICBIZ/pages/34701333/REST+Services
@@ -12,6 +14,20 @@ public class JiraWithICTimePluginApi
1214
public string UserName { get; }
1315
public api.rest.common.JiraUserInfo UserInfo => _vanillaJiraApi.UserInfo;
1416

17+
public string PluginBaseUri { get; } = "rest/ictime/1.0";
18+
public Lazy<Dictionary<string, wadl.ComposedWadlMethodDefinition>> Endpoints =>
19+
new Lazy<Dictionary<string, ComposedWadlMethodDefinition>>(() => this.GetWADL().Result
20+
.AsEnumerable()
21+
.Where(res => !string.IsNullOrEmpty(res.Id))
22+
.ToDictionary(res => res.Id ?? string.Empty)
23+
);
24+
25+
public const string CreateWorkLogMethodName = "createWorklog";
26+
public wadl.ComposedWadlMethodDefinition CreateWorkLogMethodDefinition => Endpoints.Value[CreateWorkLogMethodName];
27+
28+
public const string GetActivityTypesForProjectMethodName = "getActivityTypesForProject";
29+
public wadl.ComposedWadlMethodDefinition GetActivityTypesForProjectMethodDefinition => Endpoints.Value[GetActivityTypesForProjectMethodName];
30+
1531
private readonly HttpClient _httpClient;
1632
private readonly VanillaJiraClient _vanillaJiraApi;
1733

@@ -91,4 +107,18 @@ public async Task UpdateWorklog(string issueKey, long worklogId, DateOnly day, i
91107
{
92108
await _vanillaJiraApi.UpdateWorklog(issueKey, worklogId, day, timeSpentSeconds, activity, comment);
93109
}
110+
111+
private async Task<WadlApplication> GetWADL()
112+
{
113+
Uri uri = new Uri($"{PluginBaseUri}/application.wadl", UriKind.Relative);
114+
using Stream response = await _httpClient.GetStreamAsync(uri);
115+
if (response == null || response.Length <= 0)
116+
throw new HttpRequestException($"Empty content received from ${uri}");
117+
118+
XmlSerializer serializer = new XmlSerializer(typeof(WadlApplication));
119+
object resultObj = serializer.Deserialize(response) ?? throw new InvalidDataException($"Empty/null content deserialization result");
120+
121+
WadlApplication result = (WadlApplication)resultObj;
122+
return result;
123+
}
94124
}

0 commit comments

Comments
 (0)