|
| 1 | +using Newtonsoft.Json; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Net; |
| 7 | +using System.Reflection; |
| 8 | +using System.Text; |
| 9 | +using System.Threading.Tasks; |
| 10 | + |
| 11 | +namespace NetworkHelper |
| 12 | +{ |
| 13 | + public class Response |
| 14 | + { |
| 15 | + public Response(string message, bool error, StatusCode statusCode) |
| 16 | + { |
| 17 | + Message = message; |
| 18 | + StatusCode = statusCode; |
| 19 | + } |
| 20 | + public StatusCode StatusCode { get; } |
| 21 | + public string Message { get; } |
| 22 | + public List<T> ToList<T>() |
| 23 | + { |
| 24 | + if (StatusCode != StatusCode.OK) |
| 25 | + { |
| 26 | + return new List<T>(); // hiba esetén üres listával tér vissza |
| 27 | + } |
| 28 | + else |
| 29 | + { |
| 30 | + return JsonConvert.DeserializeObject<List<T>>(Message); |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public class RequestBuilder |
| 36 | + { |
| 37 | + WebRequest request; |
| 38 | + public RequestBuilder(string method, string from) |
| 39 | + { |
| 40 | + var request = WebRequest.Create(from); |
| 41 | + request.Method = method; |
| 42 | + this.request = request; |
| 43 | + } |
| 44 | + |
| 45 | + public RequestBuilder Body<T>(T body) |
| 46 | + { |
| 47 | + request.ContentType = "application/json"; |
| 48 | + using (var streamWriter = new StreamWriter(request.GetRequestStream())) |
| 49 | + { |
| 50 | + Dictionary<string, string> dictionary = new Dictionary<string, string>(); |
| 51 | + PropertyInfo[] properties = body.GetType().GetProperties(); |
| 52 | + foreach (PropertyInfo property in properties) |
| 53 | + { |
| 54 | + string propertyName = property.Name; |
| 55 | + object propertyValue = property.GetValue(body); |
| 56 | + if (propertyValue != null) |
| 57 | + { |
| 58 | + dictionary.Add(propertyName, propertyValue.ToString()); |
| 59 | + } |
| 60 | + } |
| 61 | + streamWriter.Write(JsonConvert.SerializeObject(dictionary)); |
| 62 | + } |
| 63 | + return this; |
| 64 | + } |
| 65 | + |
| 66 | + public Response Send() |
| 67 | + { |
| 68 | + var response = request.GetResponse(); |
| 69 | + if (response != null && ((HttpWebResponse)response).StatusCode == HttpStatusCode.OK) |
| 70 | + { |
| 71 | + using (var streamReader = new StreamReader(response.GetResponseStream())) |
| 72 | + { |
| 73 | + string json = streamReader.ReadToEnd(); |
| 74 | + return new Response(message: json, error: false, statusCode: (StatusCode)((HttpWebResponse)response).StatusCode); |
| 75 | + } |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + return new Response(message: ((HttpWebResponse)response).StatusCode.ToString(), error: true, statusCode: (StatusCode)((HttpWebResponse)response).StatusCode); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public static class Backend |
| 85 | + { |
| 86 | + public static RequestBuilder GET(string from) => new RequestBuilder(nameof(GET), from); |
| 87 | + |
| 88 | + public static RequestBuilder POST(string from) => new RequestBuilder(nameof(POST), from); |
| 89 | + |
| 90 | + public static RequestBuilder PUT(string from) => new RequestBuilder(nameof(PUT), from); |
| 91 | + |
| 92 | + public static RequestBuilder DELETE(string from) => new RequestBuilder(nameof(DELETE), from); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | + |
0 commit comments