Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Gett.NET - A library for Ge.tt Web Services

, 13 Jun 2014 CPOL
Use Ge.tt Web Services for real-time file publishing and sharing.
Gett.NET_demo.zip
Debug
Gett.NET.dll
GettTest.exe
GettTest.vshost.exe
GettTest.vshost.exe.manifest
Newtonsoft.Json.dll
Gett.NET_src-noexe.zip
Gett.NET
Gett.NET.suo
Gett.NET.v11.suo
Gett.NET
bin
Json.Net
Properties
WebSocket
GettTest
bin
Debug
GettTest.vshost.exe.manifest
obj
x86
Debug
Properties
Settings.settings
Gett.NET_src.zip
Gett.NET.suo
Gett.NET.v11.suo
Newtonsoft.Json.dll
GettTest.vshost.exe
GettTest.vshost.exe.manifest
Settings.settings
#region License information
/*

  Copyright (c) 2012 Togocoder (http://www.codeproject.com/Members/Kim-Togo)
 
  This file is part of Gett.NET library that uses the Ge.tt REST API, http://ge.tt/developers

  Gett.NET is a free library: you can redistribute it and/or modify as nessery
  it under the terms of The Code Project Open License (CPOL) as published by
  the The Code Project, either version 1.02 of the License, or (at your option) any later version.

  Gett.NET is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY OF ANY KIND. The software is provided "as-is".
 
  Please read the The Code Project Open License (CPOL) at http://www.codeproject.com/info/cpol10.aspx

  I would appreciate getting an email, if you choose to use this library in your own work.
  Send an email to togocoder(at)gmail.com with a little description of your work, thanks! :-)

  ---
  Gett.NET library makes heavy use of Json.NET - http://json.codeplex.com/ for serialize and deserialize
  of JSON class, Newtonsoft.Json.dll.
 
  License information on Json.NET, see http://json.codeplex.com/license.
  Copyright (c) 2007 James Newton-King

  Permission is hereby granted, free of charge, to any person obtaining a copy of
  this software and associated documentation files (the "Software"), to deal in the Software without restriction,
  including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
  subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
  ---
  Gett.NET library make use of WebSocket protocol client from https://github.com/sta/websocket-sharp
  The library is used to make a connection to Ge.tt Live API.
  Please see WsStream.cs for license information.

*/
#endregion

using System;
using System.Linq;
using System.Net;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Gett.Sharing
{
  /// <summary>
  /// Represent a single share for user.
  /// http://ge.tt/developers - REST API for Ge.tt Web service - share API.
  /// </summary>
  [System.Diagnostics.DebuggerDisplay("ShareName = {shareInfo.ShareName}, Title = {shareInfo.Title}, CreatedUtc = {shareInfo.CreatedUtc}")]
  public class GettShare : GettBaseUri
  {
    #region HashCode and Equals overrides
    public override int GetHashCode()
    {
      return this.shareInfo == null ? base.GetHashCode() : this.shareInfo.ShareName.GetHashCode();
    }

    public override bool Equals(object obj)
    {
      GettShare share = obj as GettShare;
      return share == null ? false : share.shareInfo.ShareName == this.shareInfo.ShareName;
    }

    public override string ToString()
    {
      return string.Format("Title:{0}, Files:{1}, CreatedUtc:{2}", string.IsNullOrEmpty(this.shareInfo.Title) ? this.shareInfo.ShareName : this.shareInfo.Title, this.shareInfo.Files.Length, this.shareInfo.CreatedUtc);
    }
    #endregion

    #region JSON Serialize/Deserialize classes
    [JsonObject(MemberSerialization.OptOut)]
    [System.Diagnostics.DebuggerDisplay("ShareInfo = {ShareName}, Title = {Title}, CreatedUtc = {CreatedUtc}")]
    public class ShareInfo
    {
      [JsonIgnore]
      public readonly DateTime Timestamp = DateTime.Now;
      [JsonProperty("sharename")]
      public string ShareName = null;
      [JsonProperty("readystate")]
      public string ReadyState = null;
      [JsonProperty("created")]
      [JsonConverter(typeof(Newtonsoft.Json.Converters.UnixDateTimeConverter))]
      public DateTime? CreatedUtc = null;
      [JsonProperty("title")]
      public string Title = null;
      [JsonProperty("getturl")]
      public string GettUrl = null;
      [JsonProperty("userid")]
      public string UserId = null;
      [JsonProperty("fullname")]
      public string Fullname = null;
      [JsonProperty("files")]
      public GettFile.FileInfo[] Files = new GettFile.FileInfo[0];
    }
    #endregion

    #region Variables
    /// <summary>
    /// Provides access to GettUser object, Token etc.
    /// </summary>
    private GettUser gettUser;

    /// <summary>
    /// Provides information on what Share we are working on.
    /// </summary>
    internal ShareInfo shareInfo;
    #endregion

    #region ctor
    /// <summary>
    /// Internal ctor, GettShares class creates new instance for this class
    /// </summary>
    /// <param name="gettUser">GettUser class</param>
    /// <param name="shareInfo">Information for this share</param>
    internal GettShare(GettUser gettUser, ShareInfo shareInfo)
    {
      this.gettUser = gettUser;
      this.shareInfo = shareInfo;
    }
    #endregion

    #region Common information on share and files in share
    /// <summary>
    /// Parent of this class. GettUser class.
    /// </summary>
    public GettUser Parent { get { return this.gettUser; } }

    /// <summary>
    /// Access to share and file information. Can be used to sort, select etc.
    /// </summary>
    public ShareInfo Info { get { return this.shareInfo; } }

    /// <summary>
    /// Access to files information in this share. Can be used to sort, select etc.
    /// </summary>
    public GettFile.FileInfo[] FilesInfo { get { return this.shareInfo.Files; } }
    #endregion

    #region Refresh share information - /shares/{sharename}
    /// <summary>
    /// Refresh share information and all files.
    /// </summary>
    public void Refresh()
    {
      // Build Uri.
      UriBuilder baseUri = new UriBuilder(GettBaseUri.BaseUri);
      baseUri.Path = baseUri.Path + string.Format(base.ShareList, this.shareInfo.ShareName);
      baseUri.Query = string.Format("accesstoken={0}", this.gettUser.Token.AccessToken);

      // GET request.
      WebClient gett = new WebClient();
      gett.Encoding = System.Text.Encoding.UTF8;
      byte[] response = gett.DownloadData(baseUri.Uri);

      // Response.
      GettShare.ShareInfo shareInfo = JsonConvert.DeserializeObject<GettShare.ShareInfo>(Encoding.UTF8.GetString(response));
      this.shareInfo = shareInfo;
    }

    /// <summary>
    /// Begins an asynchronous refresh share information and all files.
    /// </summary>
    public IAsyncResult BeginRefresh(AsyncCallback callBack, object state)
    {
      System.Threading.Tasks.Task t = System.Threading.Tasks.Task.Factory.StartNew(_ => Refresh(), state);

      if (callBack != null)
        t.ContinueWith((res) => callBack(t));

      return t;
    }

    /// <summary>
    /// Handles the end of an asynchronous Refresh. 
    /// </summary>
    /// <param name="ar"></param>
    public void EndRefresh(IAsyncResult ar)
    {
      try
      {
        System.Threading.Tasks.Task t = (System.Threading.Tasks.Task)ar;

        if (t.IsFaulted)
          throw t.Exception.InnerException;
      }
      catch (AggregateException ae)
      {
        throw ae.InnerException;
      }
    }
    #endregion

    #region Change share information - /shares/{sharename}/update
    /// <summary>
    /// Change title for share.
    /// </summary>
    /// <param name="title">New title</param>
    public void SetTitle(string title)
    {
      // Argument.
      JObject jsonArgument = new JObject(new JProperty("title", string.IsNullOrEmpty(title) ? null : title)); // "title":"my new title" or "title":null to delete it

      // Build Uri
      UriBuilder baseUri = new UriBuilder(GettBaseUri.BaseUri);
      baseUri.Path = baseUri.Path + string.Format(base.ShareUpdate, this.shareInfo.ShareName);
      baseUri.Query = string.Format("accesstoken={0}", this.gettUser.Token.AccessToken);

      // POST request.
      WebClient gett = new WebClient();
      gett.Encoding = System.Text.Encoding.UTF8;
      gett.Headers.Add("Content-Type", "application/json");
      byte[] request = Encoding.UTF8.GetBytes(jsonArgument.ToString());
      byte[] response = gett.UploadData(baseUri.Uri, request);

      // Response.
      GettShare.ShareInfo shareInfo = JsonConvert.DeserializeObject<GettShare.ShareInfo>(Encoding.UTF8.GetString(response));
      this.shareInfo.Title = shareInfo.Title;
    }

    /// <summary>
    /// Begins an asynchronous change title for share.
    /// </summary>
    /// <param name="title">New title</param>
    public IAsyncResult BeginSetTitle(string title, AsyncCallback callBack, object state)
    {
      System.Threading.Tasks.Task t = System.Threading.Tasks.Task.Factory.StartNew(_ => SetTitle(title), state);

      if (callBack != null)
        t.ContinueWith((res) => callBack(t));

      return t;
    }

    /// <summary>
    /// Handles the end of an asynchronous SetTitle. 
    /// </summary>
    /// <param name="ar"></param>
    public void EndSetTitle(IAsyncResult ar)
    {
      try
      {
        System.Threading.Tasks.Task t = (System.Threading.Tasks.Task)ar;

        if (t.IsFaulted)
          throw t.Exception.InnerException;
      }
      catch (AggregateException ae)
      {
        throw ae.InnerException;
      }
    }
    #endregion

    #region Delete a share and all of its files. - /shares/{sharename}/destroy
    /// <summary>
    /// Delete the share and all of its files.
    /// </summary>
    public bool Destroy()
    {
      // Build Uri.
      UriBuilder baseUri = new UriBuilder(GettBaseUri.BaseUri);
      baseUri.Path = baseUri.Path + string.Format(base.ShareDestroy, this.shareInfo.ShareName);
      baseUri.Query = string.Format("accesstoken={0}", this.gettUser.Token.AccessToken);

      // POST request.
      WebClient gett = new WebClient();
      gett.Encoding = System.Text.Encoding.UTF8;
      byte[] request = new byte[0];
      byte[] response = gett.UploadData(baseUri.Uri, request);

      // Response.
      string jsonResponse = Encoding.UTF8.GetString(response);
      return jsonResponse.Contains("true");
    }

    /// <summary>
    /// Begins an asynchronous delete the share and all of its files.
    /// </summary>
    public IAsyncResult BeginDestroy(AsyncCallback callBack, object state)
    {
      System.Threading.Tasks.Task t = System.Threading.Tasks.Task.Factory.StartNew(_ => Destroy(), state);

      if (callBack != null)
        t.ContinueWith((res) => callBack(t));

      return t;
    }

    /// <summary>
    /// Handles the end of an asynchronous Destroy. 
    /// </summary>
    /// <param name="ar"></param>
    public void EndDestroy(IAsyncResult ar)
    {
      try
      {
        System.Threading.Tasks.Task t = (System.Threading.Tasks.Task)ar;

        if (t.IsFaulted)
          throw t.Exception.InnerException;
      }
      catch (AggregateException ae)
      {
        throw ae.InnerException;
      }
    }
    #endregion

    #region Create new file - /files/{sharename}/create
    /// <summary>
    /// To upload files to Ge.tt you must first create the file under a given share.
    /// </summary>
    /// <param name="fileName">Filename</param>
    /// <param name="session">Optional, only used by the Live API</param>
    /// <returns>New GettFile class for manipulation file</returns>
    public GettFile CreateFile(string fileName, string session = null)
    {
      // Argument.
      JObject jsonArgument = new JObject(new JProperty("filename", fileName));

      // Optional - Only used by the Live API - https://open.ge.tt/1/doc/rest#files/{sharename}/create
      if (!string.IsNullOrEmpty(session))
        jsonArgument.Add(new JProperty("session", session));

      // Build Uri.
      UriBuilder baseUri = new UriBuilder(GettBaseUri.BaseUri);
      baseUri.Path = baseUri.Path + string.Format(base.FileCreate, this.shareInfo.ShareName);
      baseUri.Query = string.Format("accesstoken={0}", this.gettUser.Token.AccessToken);

      // POST request.
      WebClient gett = new WebClient();
      gett.Encoding = System.Text.Encoding.UTF8;
      gett.Headers.Add("Content-Type", "application/json");
      byte[] request = Encoding.UTF8.GetBytes(jsonArgument.ToString());
      byte[] response = gett.UploadData(baseUri.Uri, request);

      // Response.
      GettFile.FileInfo fileInfo = JsonConvert.DeserializeObject<GettFile.FileInfo>(Encoding.UTF8.GetString(response));

      // Create a GettFile class for the newly created file.
      return new GettFile(this.gettUser, this, fileInfo);
    }

    /// <summary>
    /// Begins an asynchronous upload file.
    /// </summary>
    /// <param name="fileName">Filename</param>
    /// <param name="session">Optional, only used by the Live API</param>
    public IAsyncResult BeginCreateFile(string fileName, AsyncCallback callBack, object state, string session = null)
    {
      System.Threading.Tasks.Task<GettFile> t = System.Threading.Tasks.Task.Factory.StartNew<GettFile>(_ => CreateFile(fileName, session), state);

      if (callBack != null)
        t.ContinueWith((res) => callBack(t));

      return t;
    }

    /// <summary>
    /// Handles the end of an asynchronous CreateFile. 
    /// </summary>
    /// <param name="ar"></param>
    /// <returns>New GettFile class for manipulation file</returns>
    public GettFile EndCreateFile(IAsyncResult ar)
    {
      try
      {
        System.Threading.Tasks.Task<GettFile> t = (System.Threading.Tasks.Task<GettFile>)ar;

        if (t.IsFaulted)
          throw t.Exception.InnerException;

        return t.Result;
      }
      catch (AggregateException ae)
      {
        throw ae.InnerException;
      }
    }
    #endregion

    #region Access to files under this share
    /// <summary>
    /// Find a specefic file from fileid in this share.
    /// Remember, file information is only refreshed when method Refresh() is called.
    /// </summary>
    /// <param name="fileid">File id to find</param>
    /// <returns>New GettFile class for manipulation file</returns>
    /// <exception>Exception will be thrown if file is not found</exception>
    public GettFile FindFileId(string fileid)
    {
      // Find and create a GettFile class from fileid.
      return (from file in this.shareInfo.Files
              where file.FileId == fileid
              select new GettFile(this.gettUser, this, file)).First();
    }

    /// <summary>
    /// Find a specefic file from filename in this share.
    /// Remember, file information is only refreshed when method Refresh() is called.
    /// </summary>
    /// <param name="fileName">Filename to find</param>
    /// <returns>New GettFile class for manipulation file</returns>
    /// <exception>Exception will be thrown if file is not found</exception>    
    public GettFile FindFileName(string fileName)
    {
      // Find and create a GettFile class from fileid.
      return (from file in this.shareInfo.Files
              where file.FileName == fileName
              select new GettFile(this.gettUser, this, file)).First();
    }

    /// <summary>
    /// List all files under this share.
    /// Remember, file information is only refreshed when method Refresh() is called.
    /// </summary>
    /// <returns>An array of new GettFile class for manipulation files</returns>
    public GettFile[] Files
    {
      get
      {
        // Create an array of GettFile class for all files in share.
        return (from file in this.shareInfo.Files
                select new GettFile(this.gettUser, this, file)).ToArray();
      }
    }
    #endregion
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Kim Togo
Software Developer Miralix
Denmark Denmark
Has worked as a programmer since 1999, starting with C++/MFC, Java, PHP and MySQL. Now it is all about C# (my favorite programming language), MS SQL, Azure and Xamarin (iOS/Android/WP8)
 
My primary work is to create applications that interacts with PABC/PBX, Microsoft Lync / UCMA.
Follow on   Twitter   Google+   LinkedIn

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150326.1 | Last Updated 13 Jun 2014
Article Copyright 2012 by Kim Togo
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid