|
| 1 | +using Newtonsoft.Json.Linq; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Net; |
| 7 | +using System.Net.Http; |
| 8 | +using System.Net.Http.Headers; |
| 9 | +using System.Text; |
| 10 | +using System.Threading.Tasks; |
| 11 | + |
| 12 | +namespace Appwrite |
| 13 | +{ |
| 14 | + public class Client |
| 15 | + { |
| 16 | + private readonly HttpClient http; |
| 17 | + private readonly Dictionary<string, string> headers; |
| 18 | + private readonly Dictionary<string, string> config; |
| 19 | + private string endPoint; |
| 20 | + private bool selfSigned; |
| 21 | + |
| 22 | + public Client() : this("https://appwrite.io/v1", false, new HttpClient()) |
| 23 | + { |
| 24 | + } |
| 25 | + |
| 26 | + public Client(string endPoint, bool selfSigned, HttpClient http) |
| 27 | + { |
| 28 | + this.endPoint = endPoint; |
| 29 | + this.selfSigned = selfSigned; |
| 30 | + this.headers = new Dictionary<string, string>() |
| 31 | + { |
| 32 | + { "content-type", "application/json" }, |
| 33 | + { "x-sdk-name", ".NET" }, |
| 34 | + { "x-sdk-platform", "server" }, |
| 35 | + { "x-sdk-language", "dotnet" }, |
| 36 | + { "x-sdk-version", "0.4.1"}, |
| 37 | + { "X-Appwrite-Response-Format", "1.0.0" } |
| 38 | + }; |
| 39 | + this.config = new Dictionary<string, string>(); |
| 40 | + this.http = http; |
| 41 | + } |
| 42 | + |
| 43 | + public Client SetSelfSigned(bool selfSigned) |
| 44 | + { |
| 45 | + this.selfSigned = selfSigned; |
| 46 | + return this; |
| 47 | + } |
| 48 | + |
| 49 | + public Client SetEndPoint(string endPoint) |
| 50 | + { |
| 51 | + this.endPoint = endPoint; |
| 52 | + return this; |
| 53 | + } |
| 54 | + |
| 55 | + public string GetEndPoint() |
| 56 | + { |
| 57 | + return endPoint; |
| 58 | + } |
| 59 | + |
| 60 | + public Dictionary<string, string> GetConfig() |
| 61 | + { |
| 62 | + return config; |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary>Your project ID</summary> |
| 66 | + public Client SetProject(string value) { |
| 67 | + config.Add("project", value); |
| 68 | + AddHeader("X-Appwrite-Project", value); |
| 69 | + return this; |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary>Your secret API key</summary> |
| 73 | + public Client SetKey(string value) { |
| 74 | + config.Add("key", value); |
| 75 | + AddHeader("X-Appwrite-Key", value); |
| 76 | + return this; |
| 77 | + } |
| 78 | + |
| 79 | + /// <summary>Your secret JSON Web Token</summary> |
| 80 | + public Client SetJWT(string value) { |
| 81 | + config.Add("jWT", value); |
| 82 | + AddHeader("X-Appwrite-JWT", value); |
| 83 | + return this; |
| 84 | + } |
| 85 | + |
| 86 | + public Client SetLocale(string value) { |
| 87 | + config.Add("locale", value); |
| 88 | + AddHeader("X-Appwrite-Locale", value); |
| 89 | + return this; |
| 90 | + } |
| 91 | + |
| 92 | + public Client AddHeader(String key, String value) |
| 93 | + { |
| 94 | + headers.Add(key, value); |
| 95 | + return this; |
| 96 | + } |
| 97 | + |
| 98 | + public async Task<HttpResponseMessage> Call(string method, string path, Dictionary<string, string> headers, Dictionary<string, object> parameters) |
| 99 | + { |
| 100 | + if (selfSigned) |
| 101 | + { |
| 102 | + ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true; |
| 103 | + } |
| 104 | + |
| 105 | + bool methodGet = "GET".Equals(method, StringComparison.InvariantCultureIgnoreCase); |
| 106 | + |
| 107 | + string queryString = methodGet ? "?" + parameters.ToQueryString() : string.Empty; |
| 108 | + |
| 109 | + HttpRequestMessage request = new HttpRequestMessage(new HttpMethod(method), endPoint + path + queryString); |
| 110 | + |
| 111 | + if ("multipart/form-data".Equals(headers["content-type"], StringComparison.InvariantCultureIgnoreCase)) |
| 112 | + { |
| 113 | + MultipartFormDataContent form = new MultipartFormDataContent(); |
| 114 | + |
| 115 | + foreach (var parameter in parameters) |
| 116 | + { |
| 117 | + if (parameter.Key == "file") |
| 118 | + { |
| 119 | + FileInfo fi = parameters["file"] as FileInfo; |
| 120 | + |
| 121 | + var file = File.ReadAllBytes(fi.FullName); |
| 122 | + |
| 123 | + form.Add(new ByteArrayContent(file, 0, file.Length), "file", fi.Name); |
| 124 | + } |
| 125 | + else if (parameter.Value is IEnumerable<object>) |
| 126 | + { |
| 127 | + List<object> list = new List<object>((IEnumerable<object>) parameter.Value); |
| 128 | + for (int index = 0; index < list.Count; index++) |
| 129 | + { |
| 130 | + form.Add(new StringContent(list[index].ToString()), $"{parameter.Key}[{index}]"); |
| 131 | + } |
| 132 | + } |
| 133 | + else |
| 134 | + { |
| 135 | + form.Add(new StringContent(parameter.Value.ToString()), parameter.Key); |
| 136 | + } |
| 137 | + } |
| 138 | + request.Content = form; |
| 139 | + |
| 140 | + } |
| 141 | + else if (!methodGet) |
| 142 | + { |
| 143 | + string body = parameters.ToJson(); |
| 144 | + |
| 145 | + request.Content = new StringContent(body, Encoding.UTF8, "application/json"); |
| 146 | + } |
| 147 | + |
| 148 | + foreach (var header in this.headers) |
| 149 | + { |
| 150 | + if (header.Key.Equals("content-type", StringComparison.InvariantCultureIgnoreCase)) |
| 151 | + { |
| 152 | + http.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(header.Value)); |
| 153 | + } |
| 154 | + else |
| 155 | + { |
| 156 | + if (http.DefaultRequestHeaders.Contains(header.Key)) { |
| 157 | + http.DefaultRequestHeaders.Remove(header.Key); |
| 158 | + } |
| 159 | + http.DefaultRequestHeaders.Add(header.Key, header.Value); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + foreach (var header in headers) |
| 164 | + { |
| 165 | + if (header.Key.Equals("content-type", StringComparison.InvariantCultureIgnoreCase)) |
| 166 | + { |
| 167 | + request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(header.Value)); |
| 168 | + } |
| 169 | + else |
| 170 | + { |
| 171 | + if (request.Headers.Contains(header.Key)) { |
| 172 | + request.Headers.Remove(header.Key); |
| 173 | + } |
| 174 | + request.Headers.Add(header.Key, header.Value); |
| 175 | + } |
| 176 | + } |
| 177 | + try |
| 178 | + { |
| 179 | + var httpResponseMessage = await http.SendAsync(request); |
| 180 | + var code = (int) httpResponseMessage.StatusCode; |
| 181 | + var response = await httpResponseMessage.Content.ReadAsStringAsync(); |
| 182 | + |
| 183 | + if (code >= 400) { |
| 184 | + var message = response.ToString(); |
| 185 | + var isJson = httpResponseMessage.Content.Headers.GetValues("Content-Type").FirstOrDefault().Contains("application/json"); |
| 186 | + |
| 187 | + if (isJson) { |
| 188 | + message = (JObject.Parse(message))["message"].ToString(); |
| 189 | + } |
| 190 | + |
| 191 | + throw new AppwriteException(message, code, response.ToString()); |
| 192 | + } |
| 193 | + |
| 194 | + return httpResponseMessage; |
| 195 | + } |
| 196 | + catch (System.Exception e) |
| 197 | + { |
| 198 | + throw new AppwriteException(e.Message, e); |
| 199 | + } |
| 200 | + |
| 201 | + } |
| 202 | + } |
| 203 | +} |
0 commit comments