/// <summary> /// Gets the collector URI. /// </summary> /// <returns>The collector URI.</returns> /// <param name="endpoint">Endpoint.</param> /// <param name="protocol">Protocol.</param> /// <param name="method">Method.</param> protected Uri MakeCollectorUri(string endpoint, HttpProtocol protocol, Enums.HttpMethod method) { string path = (method == Enums.HttpMethod.GET) ? Constants.GET_URI_SUFFIX : Constants.POST_URI_SUFFIX; string requestProtocol = (protocol == HttpProtocol.HTTP) ? "http" : "https"; return(new Uri($"{requestProtocol}://{endpoint}{path}")); }
public string HttpRequest(Enums.HttpMethod method) { var httpWebRequest = (HttpWebRequest)WebRequest.Create(_url); httpWebRequest.ContentType = "application/json"; httpWebRequest.Method = (Enum.GetName(typeof(Enums.HttpMethod), method)) ?? throw new InvalidOperationException(""); ServicePointManager.SecurityProtocol = Tls12 | Tls11 | Tls | Ssl3; using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { if (method == Enums.HttpMethod.POST) { string json = JsonConvert.SerializeObject(""); streamWriter.Write(json); streamWriter.Flush(); streamWriter.Close(); } } var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream() ?? throw new InvalidOperationException(""))) { var result = streamReader.ReadToEnd(); return(result.ToString()); } }
public async Task <Results <object> > GateWayInvoke <TBody>( Enums.HttpMethod method, string token, string path, TBody body, string query = "" ) { string jsonBody = body == null ? null : JsonConvert.SerializeObject(body); var result = await _gatewayLogic.Caller( method, token, path, jsonBody, query ); return(result); }
private HttpMethod GetMethod(Enums.HttpMethod requestMethod) { switch (requestMethod) { case Enums.HttpMethod.GET: return(HttpMethod.Get); case Enums.HttpMethod.POST: return(HttpMethod.Post); case Enums.HttpMethod.PUT: case Enums.HttpMethod.PATCH: return(HttpMethod.Put); case Enums.HttpMethod.DELETE: return(HttpMethod.Delete); default: throw new Exception($"Unknown method: {requestMethod}"); } }
public MethodNotAllowedException(string controller, string action, Enums.HttpMethod method) : base() { Controller = controller; Action = action; RequestedMethod = method; }
//TODO : beware Overhead of Rest Http public async Task <Results <object> > Caller( Enums.HttpMethod method, string token, string path, string jsonBody = "", string query = "" ) { var controllerName = path.Replace("/api/", ""); int index = controllerName.IndexOf("/"); if (index > 0) { controllerName = controllerName.Substring(0, index); } path = path.Replace("/api/", "api/"); hostUrl = _configuration[$"URLSub:{controllerName}"]; StringContent stringContent = null; pandaSecureKey = _configuration[$"SecureKey:Sub:PandaBank{controllerName}"]; //ex "https://localhost:44357/api/account/me?queryparam=x&" var fullUrl = $"{hostUrl}{path}{query}"; using (var client = new HttpClient()) { if (!string.IsNullOrEmpty(pandaSecureKey)) { client.DefaultRequestHeaders.Add("Panda", pandaSecureKey); } if (!string.IsNullOrEmpty(token)) { client.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}"); } if (!string.IsNullOrEmpty(jsonBody)) { stringContent = new StringContent ( jsonBody, Encoding.UTF8, "application/json"); } HttpResponseMessage responseMessage = null; switch (method) { case Enums.HttpMethod.GET: responseMessage = await client.GetAsync(fullUrl); break; case Enums.HttpMethod.POST: responseMessage = await client.PostAsync(fullUrl, stringContent); break; case Enums.HttpMethod.DELETE: responseMessage = await client.DeleteAsync(fullUrl); break; case Enums.HttpMethod.PUT: responseMessage = await client.PutAsync(fullUrl, stringContent); break; } string resultContent = await responseMessage.Content.ReadAsStringAsync(); var objectResult = JsonConvert.DeserializeObject <Results <object> >(resultContent); return(objectResult); } }
public static System.Net.Http.HttpMethod ToNetHttpMethod(this Enums.HttpMethod httpMethod) { return(_httpMethodMap[httpMethod]); }
/// <summary> /// /// </summary> /// <param name="httpMethod"></param> /// <param name="apiEndpoint"></param> /// <param name="apiKey"></param> /// <param name="data"></param> internal async Task <T> PerformRequest <T>(Enums.HttpMethod httpMethod, string apiEndpoint, string apiKey, object data) where T : class { if (apiEndpoint == null) { throw new ArgumentNullException("apiEndpoint"); } if ((httpMethod == Enums.HttpMethod.POST || httpMethod == Enums.HttpMethod.PATCH) && data == null) { throw new ArgumentNullException("data"); } var uri = BuildApiUri(apiEndpoint); using (var httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Add("User-Agent", string.Format("Baelor.Net/{0} (yolo)", "0.69")); httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); if (apiKey != null) { httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", apiKey); } if (data != null) { httpClient.DefaultRequestHeaders.Add("Content-Type", "application/json"); } HttpResponseMessage response = null; switch (httpMethod) { case Enums.HttpMethod.POST: response = await httpClient.PostAsync(uri, new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json")); break; case Enums.HttpMethod.PATCH: response = await httpClient.PatchAsync(uri, new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json")); break; case Enums.HttpMethod.GET: response = await httpClient.GetAsync(uri); break; case Enums.HttpMethod.DELETE: response = await httpClient.DeleteAsync(uri); break; default: throw new NotSupportedException("Only POST, PATCH, GET, and DELETE methods are supported."); } try { var content = await response.Content.ReadAsStringAsync(); var parsed = JsonConvert.DeserializeObject <ResponseBase <T> >(content); if (parsed.Success) { return(parsed.Result); } throw new BaelorException(parsed.Error.Description, parsed.Error.StatusCode, parsed.Error.Details); } catch (JsonReaderException) { throw new BaelorException("json_parsing_excpetion", 0x00); } } }
/// <summary> /// /// </summary> /// <param name="httpMethod"></param> /// <param name="apiEndpoint"></param> /// <param name="apiKey"></param> internal async Task <T> PerformRequest <T>(Enums.HttpMethod httpMethod, string apiEndpoint, string apiKey) where T : class { return(await PerformRequest <T>(httpMethod, apiEndpoint, apiKey, null)); }
/// <summary> /// /// </summary> /// <param name="httpMethod"></param> /// <param name="apiEndpoint"></param> /// <param name="data"></param> internal async Task <T> PerformRequest <T>(Enums.HttpMethod httpMethod, string apiEndpoint, object data) where T : class { return(await PerformRequest <T>(httpMethod, apiEndpoint, null, data)); }
public HttpRequest(Enums.HttpMethod method, Uri collectorUri, HttpContent content = null) { Method = method; CollectorUri = collectorUri; Content = content; }
/// <summary> /// Sets the http method. /// </summary> /// <param name="httpMethod">Http method.</param> public void SetHttpMethod(Enums.HttpMethod httpMethod) { this.httpMethod = httpMethod; collectorUri = MakeCollectorUri(this.endpoint, this.httpProtocol, this.httpMethod); }