This repository has been archived by the owner on Sep 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtils.cs
92 lines (78 loc) · 2.71 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
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using OpenApiBrowser.Model;
namespace ApiToDart
{
public static class Utils
{
public static string ApplyIndent(string input, int indentSize, char newLine = '\n')
{
IEnumerable<string> lines = input.Split(newLine);
lines = lines.Select(line => line.PadLeft(line.Length + indentSize));
return string.Join(newLine, lines);
}
public static async Task<string> DownloadString(string url)
{
using (var httpClient = new HttpClient())
{
var response = await httpClient.GetAsync(url);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
}
public static async Task<string> FetchJson(Job job)
{
if (!string.IsNullOrWhiteSpace(job.RemoteSpecification))
{
Console.WriteLine("Fetching " + job.RemoteSpecification + "...");
return await DownloadString(job.RemoteSpecification);
}
if (!string.IsNullOrWhiteSpace(job.SpecificationPath))
{
return await File.ReadAllTextAsync(job.SpecificationPath);
}
throw new Exception("Job didn't specify the location of the API specification.");
}
public static JobSettings GetJobSettings(Job job, string schemaName = null)
{
if (!string.IsNullOrWhiteSpace(schemaName))
{
var @override = job.Overrides?.FirstOrDefault(js => js.EntityNames.Contains(schemaName));
if (@override != null)
return @override.Settings;
}
return job.Default;
}
public static string GetPropertyReference(Property property)
{
if (property == null)
{
return null;
}
if (property.Items != null)
{
return GetPropertyReference(property.Items);
}
if (!string.IsNullOrWhiteSpace(property.Reference))
{
return property.Reference;
}
if (!string.IsNullOrWhiteSpace(property.JsonReference))
{
return property.JsonReference.Split('/').Skip(3).First();
}
return null;
}
public static void WriteWarning(string line)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(line);
Console.ResetColor();
}
}
}