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
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.Net;
using System.Text;
using Newtonsoft.Json;

namespace Gett.Sharing
{
  /// <summary>
  /// Represent a user on Ge.tt.
  /// http://ge.tt/developers - REST API for Ge.tt Web service - users API.
  /// </summary>
  [System.Diagnostics.DebuggerDisplay("UserMe = {Me.FullName}, {Me.Email}, UserStorage Total = {Me.Storage.Total}, UserStorage Free = {Me.Storage.Free}")]
  public class GettUser : GettBaseUri
  {
    #region HashCode and Equals overrides
    public override int GetHashCode()
    {
      return this.Me == null ? base.GetHashCode() : this.Me.UserId.GetHashCode();
    }

    public override bool Equals(object obj)
    {
      GettUser user = obj as GettUser;
      return user == null ? false : user.Me.UserId == this.Me.UserId;
    }
    #endregion

    #region JSON Serialize/Deserialize classes
    /// <summary>
    /// User token from first login
    /// </summary>
    [JsonObject(MemberSerialization.OptOut)]
    internal class UserToken
    {
      [JsonIgnore]
      public readonly DateTime Timestamp = DateTime.Now;
      [JsonProperty("accesstoken", NullValueHandling = NullValueHandling.Ignore)]
      public string AccessToken = null;
      [JsonProperty("refreshtoken")]
      public string RefreshToken = null;
      [JsonProperty("expires", NullValueHandling = NullValueHandling.Ignore)]
      public int? Expires = null;
    }

    /// <summary>
    /// Login token
    /// </summary>
    [JsonObject(MemberSerialization.OptOut)]
    internal class LoginToken
    {
      [JsonProperty("apikey")]
      public string ApiKey;
      [JsonProperty("email")]
      public string Email;
      [JsonProperty("password")]
      public string Password;
    }

    /// <summary>
    /// User informations from Ge.tt
    /// </summary>
    [JsonObject(MemberSerialization.OptOut)]
    public class UserMe
    {
      [JsonIgnore]
      public readonly DateTime Timestamp = DateTime.Now;
      [JsonProperty("userid")]
      public string UserId;
      [JsonProperty("fullname")]
      public string FullName;
      [JsonProperty("email")]
      public string Email;

      [JsonProperty("storage")]
      public UserStorage Storage;
    }

    /// <summary>
    /// Storage information for user.
    /// </summary>
    [JsonObject(MemberSerialization.OptIn)]
    public class UserStorage
    {
      [JsonProperty("used")]
      public long Used;
      [JsonProperty("limit")]
      public long Limit;
      [JsonProperty("extra")]
      public long Extra;

      public long Free { get { return this.Limit - this.Used; } }
      public long Total { get { return this.Limit; } }
    }
    #endregion

    #region Variables
    /// <summary>
    /// Login token. Used in all REST API.
    /// </summary>
    internal UserToken Token
    {
      get { return this.token; }
      set
      {
        this.token = value;
        this.SessionExpires = (int)this.token.Expires;
      }
    }
    private UserToken token;

    /// <summary>
    /// When does login session expires.
    /// Call RefreshLogin(), to keep login session alive.
    /// </summary>
    public int SessionExpires { get; private set; }
    #endregion

    #region ctor
    /// <summary>
    /// Public ctor.
    /// </summary>
    /// <param name="maxConcurrentConnections">The maximum number of concurrent connections allowed</param>
    public GettUser(int maxConcurrentConnections = 2)
    {
      // The maximum number of concurrent connections allowed. The default value is 2.
      ServicePointManager.DefaultConnectionLimit = maxConcurrentConnections;
      this.shares = new GettShares(this);
    }
    #endregion

    #region Login API - /users/login
    /// <summary>
    /// Login to Ge.tt Web Service.
    /// The first method to call before any other service.
    /// </summary>
    /// <param name="apiKey">API Key</param>
    /// <param name="email">Email</param>
    /// <param name="password">Password</param>
    public void Login(string apiKey, string email, string password)
    {
      // Argument
      LoginToken loginToken = new LoginToken() { ApiKey = apiKey, Email = email, Password = password };
      string jsonArgument = JsonConvert.SerializeObject(loginToken);

      // 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(base.UsersLogin, request);

      // Response
      this.Token = JsonConvert.DeserializeObject<UserToken>(Encoding.UTF8.GetString(response));
    }

    /// <summary>
    /// Begins an asynchronous Login to Ge.tt Web Service.
    /// The first method to call before any other service.
    /// </summary>
    /// <param name="apiKey">API Key</param>
    /// <param name="email">Email</param>
    /// <param name="password">Password</param>
    /// <param name="callBack">Callback</param>
    /// <param name="state">User state</param>
    public IAsyncResult BeginLogin(string apiKey, string email, string password, AsyncCallback callBack, object state)
    {
      System.Threading.Tasks.Task t = System.Threading.Tasks.Task.Factory.StartNew(_ => Login(apiKey, email, password), state);

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

      return t;
    }

    /// <summary>
    /// Handles the end of an asynchronous Login. 
    /// </summary>
    /// <param name="ar"></param>
    public void EndLogin(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;
      }
    }

    /// <summary>
    /// Refresh previous login token.
    /// Please see Token.Expires.
    /// </summary>
    public void RefreshLogin()
    {
      if (this.Token == null)
        throw new UnauthorizedAccessException("Login is required");

      // Argument
      UserToken refreshToken = new UserToken() { RefreshToken = this.Token.RefreshToken };
      string jsonArgument = JsonConvert.SerializeObject(refreshToken);

      // 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(base.UsersLogin, request);

      // Response
      this.Token = JsonConvert.DeserializeObject<UserToken>(Encoding.UTF8.GetString(response));
    }

    /// <summary>
    /// Begins an asynchronous refresh previous login token.
    /// Please see Token.Expires.
    /// </summary>
    public IAsyncResult BeginRefreshLogin(AsyncCallback callBack, object state)
    {
      System.Threading.Tasks.Task t = System.Threading.Tasks.Task.Factory.StartNew(_ => RefreshLogin(), state);

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

      return t;
    }

    /// <summary>
    /// Handles the end of an asynchronous RefreshLogin. 
    /// </summary>
    /// <param name="ar"></param>
    public void EndRefreshLogin(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 User information API - /users/me
    /// <summary>
    /// Refresh Me informations. User storage etc.
    /// </summary>
    public void RefreshMe()
    {
      if (this.Token == null)
        throw new UnauthorizedAccessException("Login is required");

      // Build Uri
      UriBuilder baseUri = new UriBuilder(base.UsersMe);
      baseUri.Query = string.Format("accesstoken={0}", this.Token.AccessToken);

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

      // Response
      this.Me = JsonConvert.DeserializeObject<UserMe>(Encoding.UTF8.GetString(response));
    }

    /// <summary>
    /// Begins an asynchronous refresh Me informations. User storage etc..
    /// </summary>
    public IAsyncResult BeginRefreshMe(AsyncCallback callBack, object state)
    {
      System.Threading.Tasks.Task t = System.Threading.Tasks.Task.Factory.StartNew(_ => RefreshMe(), state);

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

      return t;
    }

    /// <summary>
    /// Handles the end of an asynchronous RefreshMe. 
    /// </summary>
    /// <param name="ar"></param>
    public void EndRefreshMe(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;
      }
    }

    /// <summary>
    /// User information.
    /// </summary>
    public UserMe Me
    {
      get
      {
        if (this.Token == null)
          throw new UnauthorizedAccessException("Login is required");

        return this.me;
      }
      private set
      {
        this.me = value;
      }
    }
    private UserMe me;
    #endregion

    #region Access to Shares
    /// <summary>
    /// Get instance for GettShares class. Provides access to Shares and files.
    /// </summary>
    public GettShares Shares
    {
      get
      {
        if (this.Token == null)
          throw new UnauthorizedAccessException("Login is required");

        return this.shares;
      }
    }
    private GettShares shares;
    #endregion

    #region Access to Live API
    /// <summary>
    /// Get new instance for GettLive class.
    /// </summary>
    public Live.GettLive GetLive()
    {
      if (this.Token == null)
        throw new UnauthorizedAccessException("Login is required");

      return new Live.GettLive(this);
    }
    #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 | Mobile
Web03 | 2.8.140814.1 | Last Updated 13 Jun 2014
Article Copyright 2012 by Kim Togo
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid