-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathApiOperation.cs
More file actions
73 lines (61 loc) · 2.13 KB
/
ApiOperation.cs
File metadata and controls
73 lines (61 loc) · 2.13 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using CheckCloudSupport.Extensions;
using Markdig.Helpers;
namespace CheckCloudSupport.Docs;
/// <summary>
/// Represents an API operation.
/// </summary>
public class ApiOperation
{
/// <summary>
/// Gets the HTTP method for the API operation.
/// </summary>
public HttpMethod? Method { get; private set; }
/// <summary>
/// Gets the API path for the API operation.
/// </summary>
public string? Path { get; private set; }
/// <summary>
/// Gets the API version for the API operation.
/// </summary>
public ApiVersion Version { get; private set; } = ApiVersion.Unknown;
/// <summary>
/// Creates an instance of the <see cref="ApiOperation"/> class from a <see cref="StringLine"/> instance.
/// </summary>
/// <param name="line">The <see cref="StringLine"/> instance to create from.</param>
/// <returns><see cref="ApiOperation"/>.</returns>
/// <exception cref="ArgumentException">Thrown if the <see cref="StringLine"/> does not contain valid data.</exception>
public static ApiOperation? CreateFromStringLine(StringLine line)
{
var lineText = line.Slice.ToString();
if (string.IsNullOrWhiteSpace(lineText))
{
return null;
}
var parts = lineText.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length < 2)
{
throw new ArgumentException($"Invalid line text: {lineText}");
}
var operation = new ApiOperation();
try
{
operation.Method = new HttpMethod(parts[0]);
}
catch (Exception)
{
throw new ArgumentException($"Invalid HTTP operation: {parts[0]}");
}
operation.Path = parts[1]
.MakePathRelativeToVersion()
.NormalizeIdSegments()
.NormalizeParameters()
.FixUserDrivePath()
.FixDriveShortcut()
.FixDriveShareId()
.FixWellKnownMailFoldersId();
operation.Version = parts[1].ExtractApiVersion();
return operation;
}
}