using System.Linq;
using System.Net.Http;
using System.Text;
+using System.Text.RegularExpressions;
using System.Threading.Tasks;
using OpenTween.Connection;
{
public static readonly Uri ApiBase = new Uri("https://api-ssl.bitly.com/");
+ public string EndUserLoginName { get; set; }
+ public string EndUserApiKey { get; set; }
+
private HttpClient http => this.localHttpClient ?? Networking.Http;
private readonly HttpClient localHttpClient;
this.localHttpClient = http;
}
+ public async Task<Uri> ShortenAsync(Uri srcUri, string domain = null)
+ {
+ var query = new Dictionary<string, string>
+ {
+ ["login"] = this.EndUserLoginName,
+ ["apiKey"] = this.EndUserApiKey,
+ ["format"] = "txt",
+ ["longUrl"] = srcUri.OriginalString,
+ };
+
+ if (!string.IsNullOrEmpty(domain))
+ query["domain"] = domain;
+
+ var uri = new Uri(ApiBase, "/v3/shorten?" + MyCommon.BuildQueryString(query));
+ using (var response = await this.http.GetAsync(uri).ConfigureAwait(false))
+ {
+ response.EnsureSuccessStatusCode();
+
+ var result = await response.Content.ReadAsStringAsync()
+ .ConfigureAwait(false);
+
+ if (!Regex.IsMatch(result, @"^https?://"))
+ throw new WebApiException("Failed to create URL.", result);
+
+ return new Uri(result.TrimEnd());
+ }
+ }
+
public bool ValidateApiKey(string login, string apiKey)
=> this.ValidateApiKeyAsync(login, apiKey).Result;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
+using OpenTween.Api;
using OpenTween.Connection;
namespace OpenTween
if (string.IsNullOrEmpty(this.BitlyId) || string.IsNullOrEmpty(this.BitlyKey))
return srcUri;
- var query = new Dictionary<string, string>
+ var bitly = new BitlyApi
{
- ["login"] = this.BitlyId,
- ["apiKey"] = this.BitlyKey,
- ["format"] = "txt",
- ["domain"] = domain,
- ["longUrl"] = srcUri.OriginalString,
+ EndUserLoginName = this.BitlyId,
+ EndUserApiKey = this.BitlyKey,
};
- var uri = new Uri("https://api-ssl.bitly.com/v3/shorten?" + MyCommon.BuildQueryString(query));
- using (var response = await this.http.GetAsync(uri).ConfigureAwait(false))
- {
- response.EnsureSuccessStatusCode();
-
- var result = await response.Content.ReadAsStringAsync()
- .ConfigureAwait(false);
-
- if (!Regex.IsMatch(result, @"^https?://"))
- throw new WebApiException("Failed to create URL.", result);
-
- return new Uri(result.TrimEnd());
- }
+ return await bitly.ShortenAsync(srcUri, domain)
+ .ConfigureAwait(false);
}
private async Task<Uri> ShortenByUxnuAsync(Uri srcUri)