Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 42 additions & 27 deletions MinusEngine/MinusApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public class MinusApi
#region Constants
public static readonly String USER_AGENT = "MinusEngine_0.2";
public static readonly String BASE_URL = "http://minus.com/api/";
public static readonly String PAGE_BASE_URL = "http://minus.com";
public static readonly String PAGE_COOKIE = "__utmc=125622038;";
public static readonly Uri CREATE_GALLERY_URL = new Uri(BASE_URL + "CreateGallery");
public static readonly Uri UPLOAD_ITEM_URL = new Uri(BASE_URL + "UploadItem");
public static readonly Uri SAVE_GALLERY_URL = new Uri(BASE_URL + "SaveGallery");
Expand Down Expand Up @@ -107,27 +109,15 @@ public MinusApi(String apiKey)
#endregion

#region Public methods

/// <summary>
/// Creates an empty new gallery.
/// </summary>
public void CreateGallery()
{
CreateGallery(null);
}


/// <summary>
/// Creates an empty new gallery.
/// </summary>
public void CreateGallery(String cookieHeader)
{

CookieAwareWebClient client = this.CreateAndSetupWebClient();
if (!String.IsNullOrEmpty(cookieHeader))
{
client.setCookieHeader(new Uri(BASE_URL), cookieHeader);
}

CookieAwareWebClient client = this.CreateAndSetupWebClient(cookieHeader);

client.DownloadStringCompleted += delegate(object sender, DownloadStringCompletedEventArgs e) {
if (e.Error != null)
{
Expand Down Expand Up @@ -197,26 +187,30 @@ public void CreateGallery(String cookieHeader)
/// abort this operation.
/// </returns>
#if !WINDOWS_PHONE
public void UploadItem(String editorId, String key, String filename, String desiredFilename = null)
public void UploadItem(String cookieHeader, String editorId, String key, String filename, String desiredFilename = null)
{
// Not worth checking for file existence or other stuff, as either Path.GetFileName or the upload
// will check & fail
String name = desiredFilename == null ? Path.GetFileName(filename) : desiredFilename;
Stream data = new FileStream(filename, FileMode.Open, FileAccess.Read);
UploadItem(editorId, key, name, data);
UploadItem(cookieHeader, editorId, key, name, data);
}
#endif

public void UploadItem(String editorId, String key, String filename, Stream data)
public void UploadItem(String cookieHeader, String editorId, String key, String filename, Stream data)
{
UriBuilder ub = new UriBuilder(UPLOAD_ITEM_URL);
ub.Query = string.Format("filename={0}&key={1}&editor_id={2}", filename, key, editorId);
ub.Query = string.Format("filename={0}&key={1}&editor_id={2}&reader_id={2}", filename, key, editorId);

try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(ub.Uri);
request.Method = "POST";

request.ContentType = "application/octet-stream";
request.Headers.Add("Content-Disposition", "attachment; filename=" + filename);
request.CookieContainer = new CookieContainer();
request.CookieContainer.SetCookies(new Uri(BASE_URL), cookieHeader);

ThreadPool.QueueUserWorkItem((object state) =>
{

Expand Down Expand Up @@ -272,11 +266,11 @@ public void UploadItem(String editorId, String key, String filename, Stream data
/// If you fail to include items that were uploaded to this gallery, those items will be
/// discarded by the server.
/// </param>
public void SaveGallery(String name, String galleryEditorId, String key, String[] items)
public void SaveGallery(String cookieHeader, String name, String galleryEditorId, String key, String[] items)
{
// Get a pre-configured web client
WebClient client = this.CreateAndSetupWebClient();

CookieAwareWebClient client = this.CreateAndSetupWebClient(cookieHeader);
string jsonItems;

// build the item list (the order in which the items will be shown)
Expand All @@ -299,7 +293,7 @@ public void SaveGallery(String name, String galleryEditorId, String key, String[
.Append("&items=").Append(UrlEncode(jsonItems));

client.Headers["Content-Type"] = "application/x-www-form-urlencoded";

// register the completion/error listener
client.UploadStringCompleted += delegate(object sender, UploadStringCompletedEventArgs e)
{
Expand Down Expand Up @@ -353,14 +347,14 @@ public void SaveGallery(String name, String galleryEditorId, String key, String[
/// Retrieve all items in a gallery, along with some other info (url and title).
/// </summary>
/// <param name="galleryReaderId">The reader id (public) of the gallery.</param>
public void GetItems(String galleryReaderId)
public void GetItems(String cookieHeader, String galleryReaderId)
{
if (String.IsNullOrEmpty(galleryReaderId))
{
throw new ArgumentException("Gallery Reader Id cannot be null or empty");
}

WebClient client = this.CreateAndSetupWebClient();
WebClient client = this.CreateAndSetupWebClient(cookieHeader);
client.DownloadStringCompleted += delegate(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
Expand Down Expand Up @@ -409,6 +403,18 @@ public void GetItems(String galleryReaderId)
}
}

/// <summary>
/// Sign in as guest
/// </summary>
public void SignIn()
{
SignInResult result = new SignInResult(true)
{
CookieHeaders = PAGE_COOKIE
};
TriggerSignInComplete(result);
}

/// <summary>
/// Signs into minus
/// </summary>
Expand Down Expand Up @@ -495,7 +501,7 @@ public void MyGalleries(String cookieHeader)
throw new ArgumentException("Cookie Header cannot be null or empty");
}

CookieAwareWebClient client = this.CreateAndSetupWebClient();
CookieAwareWebClient client = this.CreateAndSetupWebClient(cookieHeader);
client.setCookieHeader(new Uri(BASE_URL), cookieHeader);
client.DownloadStringCompleted += delegate(object sender, DownloadStringCompletedEventArgs e)
{
Expand Down Expand Up @@ -585,6 +591,10 @@ public static String UrlEncode(String parameter)
#region Private helpers

private CookieAwareWebClient CreateAndSetupWebClient()
{
return CreateAndSetupWebClient(null);
}
private CookieAwareWebClient CreateAndSetupWebClient(String cookieHeader)
{
CookieAwareWebClient client = new CookieAwareWebClient();
#if !WINDOWS_PHONE
Expand All @@ -594,6 +604,11 @@ private CookieAwareWebClient CreateAndSetupWebClient()
}
#endif
client.Headers["User-Agent"] = USER_AGENT;
if(cookieHeader != null)
{
client.setCookieHeader(new Uri(BASE_URL), cookieHeader);
}

return client;
}

Expand Down
4 changes: 2 additions & 2 deletions MinusEngine/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]
Loading