Skip to content
Permalink
feature-editor…
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
namespace Documentation.Plugin.Core.Repository
{
public class BaseDocumentationRepository
{
protected HttpClient _client;
/// <summary>
/// builds http client with a basic auth header using the provided username and token.
/// </summary>
/// <param name="userName">api user</param>
/// <param name="token">api authorization token</param>
protected void BuildHttpClient(string userName, string token, string apiUrl)
{
_client = new HttpClient();
_client.BaseAddress = new Uri(apiUrl);
_client.DefaultRequestHeaders.Authorization = GetBasicAuthHeader(userName, token);
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
_client.DefaultRequestHeaders.Add("User-Agent", "Anything");
}
private AuthenticationHeaderValue GetBasicAuthHeader(string userName, string token)
{
string format = $"{userName}:{token}";
string authString = Convert.ToBase64String(Encoding.ASCII.GetBytes(format));
return new AuthenticationHeaderValue("Basic", authString);
}
/// <summary>
/// Returns the response of the request url as a string on status code 200 reponse. Otherwise returns empty string
/// </summary>
/// <param name="requestUrl">url of request</param>
/// <returns>string response</returns>
protected string GetDocumentationData(string requestUrl)
{
using (var httpResponse = _client.GetAsync(requestUrl).Result)
{
if (httpResponse.StatusCode != HttpStatusCode.OK)
return string.Empty;
return httpResponse.Content.ReadAsStringAsync().Result;
}
}
}
}