openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
OpenAI/src/Generated/Internal/ClientUriBuilder.cs
181lines · modecode
| 1 | // <auto-generated/> |
| 2 | |
| 3 | #nullable disable |
| 4 | |
| 5 | using System; |
| 6 | using System.Collections.Generic; |
| 7 | using System.Linq; |
| 8 | using System.Text; |
| 9 | |
| 10 | namespace OpenAI |
| 11 | { |
| 12 | internal partial class ClientUriBuilder |
| 13 | { |
| 14 | private UriBuilder _uriBuilder; |
| 15 | private StringBuilder _pathAndQuery; |
| 16 | private int _pathLength; |
| 17 | |
| 18 | public ClientUriBuilder() |
| 19 | { |
| 20 | } |
| 21 | |
| 22 | private UriBuilder UriBuilder => _uriBuilder ??= new UriBuilder(); |
| 23 | |
| 24 | private StringBuilder PathAndQuery => _pathAndQuery ??= new StringBuilder(); |
| 25 | |
| 26 | public void Reset(Uri uri) |
| 27 | { |
| 28 | _uriBuilder = new UriBuilder(uri); |
| 29 | PathAndQuery.Clear(); |
| 30 | PathAndQuery.Append(UriBuilder.Path); |
| 31 | _pathLength = PathAndQuery.Length; |
| 32 | PathAndQuery.Append(UriBuilder.Query); |
| 33 | } |
| 34 | |
| 35 | public void AppendPath(string value, bool escape) |
| 36 | { |
| 37 | if (escape) |
| 38 | { |
| 39 | value = Uri.EscapeDataString(value); |
| 40 | } |
| 41 | if (_pathLength > 0 && PathAndQuery[_pathLength - 1] == '/' && value[0] == '/') |
| 42 | { |
| 43 | PathAndQuery.Remove(_pathLength - 1, 1); |
| 44 | _pathLength = _pathLength - 1; |
| 45 | } |
| 46 | PathAndQuery.Insert(_pathLength, value); |
| 47 | _pathLength = _pathLength + value.Length; |
| 48 | } |
| 49 | |
| 50 | public void AppendPath(bool value, bool escape = false) => AppendPath(TypeFormatters.ConvertToString(value), escape); |
| 51 | |
| 52 | public void AppendPath(float value, bool escape = true) => AppendPath(TypeFormatters.ConvertToString(value), escape); |
| 53 | |
| 54 | public void AppendPath(double value, bool escape = true) => AppendPath(TypeFormatters.ConvertToString(value), escape); |
| 55 | |
| 56 | public void AppendPath(int value, bool escape = true) => AppendPath(TypeFormatters.ConvertToString(value), escape); |
| 57 | |
| 58 | public void AppendPath(byte[] value, SerializationFormat format = SerializationFormat.Default, bool escape = true) => AppendPath(TypeFormatters.ConvertToString(value, format), escape); |
| 59 | |
| 60 | public void AppendPath(DateTimeOffset value, SerializationFormat format = SerializationFormat.Default, bool escape = true) => AppendPath(TypeFormatters.ConvertToString(value, format), escape); |
| 61 | |
| 62 | public void AppendPath(TimeSpan value, SerializationFormat format = SerializationFormat.Default, bool escape = true) => AppendPath(TypeFormatters.ConvertToString(value, format), escape); |
| 63 | |
| 64 | public void AppendPath(Guid value, bool escape = true) => AppendPath(TypeFormatters.ConvertToString(value), escape); |
| 65 | |
| 66 | public void AppendPath(long value, bool escape = true) => AppendPath(TypeFormatters.ConvertToString(value), escape); |
| 67 | |
| 68 | public void AppendPathDelimited<T>(IEnumerable<T> value, string delimiter, SerializationFormat format = SerializationFormat.Default, bool escape = true) |
| 69 | { |
| 70 | delimiter ??= ","; |
| 71 | IEnumerable<string> stringValues = value.Select(v => TypeFormatters.ConvertToString(v, format)); |
| 72 | AppendPath(string.Join(delimiter, stringValues), escape); |
| 73 | } |
| 74 | |
| 75 | public void AppendQuery(string name, string value, bool escape) |
| 76 | { |
| 77 | if (PathAndQuery.Length == _pathLength) |
| 78 | { |
| 79 | PathAndQuery.Append('?'); |
| 80 | } |
| 81 | if (PathAndQuery.Length > _pathLength && PathAndQuery[PathAndQuery.Length - 1] != '?') |
| 82 | { |
| 83 | PathAndQuery.Append('&'); |
| 84 | } |
| 85 | if (escape) |
| 86 | { |
| 87 | value = Uri.EscapeDataString(value); |
| 88 | } |
| 89 | PathAndQuery.Append(name); |
| 90 | PathAndQuery.Append('='); |
| 91 | PathAndQuery.Append(value); |
| 92 | } |
| 93 | |
| 94 | public void AppendQuery(string name, bool value, bool escape = false) => AppendQuery(name, TypeFormatters.ConvertToString(value), escape); |
| 95 | |
| 96 | public void AppendQuery(string name, float value, bool escape = true) => AppendQuery(name, TypeFormatters.ConvertToString(value), escape); |
| 97 | |
| 98 | public void AppendQuery(string name, DateTimeOffset value, SerializationFormat format = SerializationFormat.Default, bool escape = true) => AppendQuery(name, TypeFormatters.ConvertToString(value, format), escape); |
| 99 | |
| 100 | public void AppendQuery(string name, TimeSpan value, SerializationFormat format = SerializationFormat.Default, bool escape = true) => AppendQuery(name, TypeFormatters.ConvertToString(value, format), escape); |
| 101 | |
| 102 | public void AppendQuery(string name, double value, bool escape = true) => AppendQuery(name, TypeFormatters.ConvertToString(value), escape); |
| 103 | |
| 104 | public void AppendQuery(string name, decimal value, bool escape = true) => AppendQuery(name, TypeFormatters.ConvertToString(value), escape); |
| 105 | |
| 106 | public void AppendQuery(string name, int value, bool escape = true) => AppendQuery(name, TypeFormatters.ConvertToString(value), escape); |
| 107 | |
| 108 | public void AppendQuery(string name, long value, bool escape = true) => AppendQuery(name, TypeFormatters.ConvertToString(value), escape); |
| 109 | |
| 110 | public void AppendQuery(string name, TimeSpan value, bool escape = true) => AppendQuery(name, TypeFormatters.ConvertToString(value), escape); |
| 111 | |
| 112 | public void AppendQuery(string name, byte[] value, SerializationFormat format = SerializationFormat.Default, bool escape = true) => AppendQuery(name, TypeFormatters.ConvertToString(value, format), escape); |
| 113 | |
| 114 | public void AppendQuery(string name, Guid value, bool escape = true) => AppendQuery(name, TypeFormatters.ConvertToString(value), escape); |
| 115 | |
| 116 | public void AppendQueryDelimited<T>(string name, IEnumerable<T> value, string delimiter, SerializationFormat format = SerializationFormat.Default, bool escape = true) |
| 117 | { |
| 118 | delimiter ??= ","; |
| 119 | IEnumerable<string> stringValues = value.Select(v => TypeFormatters.ConvertToString(v, format)); |
| 120 | AppendQuery(name, string.Join(delimiter, stringValues), escape); |
| 121 | } |
| 122 | |
| 123 | public void UpdateQuery(string name, string value) |
| 124 | { |
| 125 | if (PathAndQuery.Length == _pathLength) |
| 126 | { |
| 127 | AppendQuery(name, value, false); |
| 128 | } |
| 129 | else |
| 130 | { |
| 131 | int queryStartIndex = _pathLength + 1; |
| 132 | string searchPattern = name + "="; |
| 133 | string queryString = PathAndQuery.ToString(queryStartIndex, PathAndQuery.Length - queryStartIndex); |
| 134 | int paramStartIndex = -1; |
| 135 | if (queryString.StartsWith(searchPattern)) |
| 136 | { |
| 137 | paramStartIndex = 0; |
| 138 | } |
| 139 | if (paramStartIndex == -1) |
| 140 | { |
| 141 | int prefixedIndex = queryString.IndexOf("&" + searchPattern); |
| 142 | if (prefixedIndex >= 0) |
| 143 | { |
| 144 | paramStartIndex = prefixedIndex + 1; |
| 145 | } |
| 146 | } |
| 147 | if (paramStartIndex >= 0) |
| 148 | { |
| 149 | int valueStartIndex = paramStartIndex + searchPattern.Length; |
| 150 | int valueEndIndex = queryString.IndexOf('&', valueStartIndex); |
| 151 | if (valueEndIndex == -1) |
| 152 | { |
| 153 | valueEndIndex = queryString.Length; |
| 154 | } |
| 155 | int globalStart = queryStartIndex + valueStartIndex; |
| 156 | int globalEnd = queryStartIndex + valueEndIndex; |
| 157 | PathAndQuery.Remove(globalStart, globalEnd - globalStart); |
| 158 | PathAndQuery.Insert(globalStart, value); |
| 159 | } |
| 160 | else |
| 161 | { |
| 162 | AppendQuery(name, value, false); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | public Uri ToUri() |
| 168 | { |
| 169 | UriBuilder.Path = PathAndQuery.ToString(0, _pathLength); |
| 170 | if (PathAndQuery.Length > _pathLength) |
| 171 | { |
| 172 | UriBuilder.Query = PathAndQuery.ToString(_pathLength + 1, PathAndQuery.Length - _pathLength - 1); |
| 173 | } |
| 174 | if (PathAndQuery.Length == _pathLength) |
| 175 | { |
| 176 | UriBuilder.Query = ""; |
| 177 | } |
| 178 | return UriBuilder.Uri; |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |