Permalink
Browse files

Added login support to Lithiio

  • Loading branch information...
Jaex committed Nov 10, 2017
1 parent a7aea34 commit 7388cd3ee2cb719626315b209932c465152f4d94
@@ -592,9 +592,10 @@ public static void RefreshSelectedItem(this ListBox lb)
}
}
public static void ShowError(this Exception e)
public static void ShowError(this Exception e, bool fullError = true)
{
MessageBox.Show(e.ToString(), "ShareX - " + Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
string error = fullError ? e.ToString() : e.Message;
MessageBox.Show(error, "ShareX - " + Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
@@ -27,6 +27,7 @@
using Newtonsoft.Json;
using ShareX.UploadersLib.Properties;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
@@ -57,41 +58,79 @@ public sealed class Lithiio : FileUploader
{
public LithiioSettings Config { get; private set; }
public Lithiio()
{
}
public Lithiio(LithiioSettings config)
{
Config = config;
}
public override UploadResult Upload(Stream stream, string fileName)
{
Dictionary<string, string> arguments = new Dictionary<string, string>();
arguments.Add("key", Config.UserAPIKey);
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("key", Config.UserAPIKey);
UploadResult result = SendRequestFile("https://upload.lithi.io/v1.php", stream, fileName, "file", arguments);
UploadResult result = SendRequestFile("https://upload.lithi.io/v1.php", stream, fileName, "file", args);
if (result.IsSuccess)
{
LithiioResponse response = JsonConvert.DeserializeObject<LithiioResponse>(result.Response);
LithiioUploadResponse uploadResponse = JsonConvert.DeserializeObject<LithiioUploadResponse>(result.Response);
if (response.Success)
if (uploadResponse.Success)
{
result.URL = response.URL;
result.URL = uploadResponse.URL;
}
else
{
Errors.Add(response.Error);
Errors.Add(uploadResponse.Error);
}
}
return result;
}
public class LithiioResponse
public string FetchAPIKey(string email, string password)
{
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("email", email);
args.Add("password", password);
string response = SendRequestMultiPart("https://lithi.io/api/v1/fetch-api-key.php", args);
if (!string.IsNullOrEmpty(response))
{
LithiioFetchAPIKeyResponse apiKeyResponse = JsonConvert.DeserializeObject<LithiioFetchAPIKeyResponse>(response);
if (apiKeyResponse.Success)
{
return apiKeyResponse.APIKey;
}
else
{
throw new Exception(apiKeyResponse.Error);
}
}
return null;
}
private class LithiioResponse
{
public bool Success { get; set; }
public string URL { get; set; }
public string Error { get; set; }
}
private class LithiioUploadResponse : LithiioResponse
{
public string URL { get; set; }
}
private class LithiioFetchAPIKeyResponse : LithiioResponse
{
public string APIKey { get; set; }
}
}
public class LithiioSettings

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -2807,6 +2807,26 @@ private void txtLithiioApiKey_TextChanged(object sender, EventArgs e)
Config.LithiioSettings.UserAPIKey = txtLithiioApiKey.Text;
}
private void btnLithiioLogin_Click(object sender, EventArgs e)
{
try
{
Cursor = Cursors.WaitCursor;
Lithiio lithiio = new Lithiio();
string apiKey = lithiio.FetchAPIKey(txtLithiioEmail.Text, txtLithiioPassword.Text);
txtLithiioApiKey.Text = apiKey ?? "";
}
catch (Exception ex)
{
ex.ShowError(false);
}
finally
{
Cursor = Cursors.Default;
}
}
private void btnLithiioGetAPIKey_Click(object sender, EventArgs e)
{
URLHelpers.OpenURL("https://lithi.io/my-account.php");
Oops, something went wrong.

0 comments on commit 7388cd3

Please sign in to comment.