Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#4.0

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

4.98/5 (14 votes)
19 Jan 2016CPOL7 min read 56.1K   1.3K  
Use Ge.tt Web Services for real-time file publishing and sharing.

Image 1
https://www.nuget.org/packages/Gett.NET/

 

GettNET/GettNET_new.PNG

Introduction

Gett.NET is a C# library for Ge.tt Web Services REST API and Live API.

Ge.tt (http://ge.tt) is an instant, real-time file publishing and sharing service.
With Ge.tt, you can turn any type of file into web content and share it instantly. You can share documents, video, music and photos making them instantly available.

Ge.tt is suitable for professional and personal use.

  • Real-time sharing:
    Your files are ready to be published or shared as soon as you select them! No need to wait for files to upload.
  • Real-time statistics:
    You can easily track how many people have downloaded or viewed your files.

Read more at http://ge.tt/about.

To get started, you have to create or use an existing user account at Ge.tt.

Go to http://ge.tt and click on “SIGN UP”. It is free to sign up for a basic account, fill out the form and you are off.
After a successful login at http://ge.tt, you then access http://ge.tt/developers and click “Create app”, fill in the form for your own API key and click “Create app” button.

Now you have the most important information, the API key! Without the API key you cannot use Gett.NET library. Just remember that your API key is secret and personal. Use it as such.

Background

The idea of creating Gett.NET library was to learn how to use RESTful web services and JSON data.
I was working on a large project where data access was through REST API and JSON data.
Before I began to work on my large project, I wanted to do some learning by doing, and I thought it would be fun to start by making a library written in C# that can use Ge.tt Web Services.

I use http://ge.tt a lot to transfer log files from customers site, share file back to other persons and families members.
And somehow I thought it could be cool to implement the sharing facilities directly in my own programs. With a click of a button in my program, I can instantly upload log files or backup files to my own account at Ge.tt.

What is So Special about Ge.tt

What marks Ge.tt special from "all other upload services" on the internet?

The most unique is the instant upload and download service. When you start to upload a large file via Gett.NET library or the web site, others are able to start downloading the large file immediately, no waiting for the large file to complete the upload.

And if you need the advanced queuing nature of Ge.tt, then say hello to the Live API.
The idea behind Live API is that you create a new file on a share via Gett.NET library, but you do not upload the file.

As long as there is a connection to Live API from Gett.NET library, all files created that way is listed as available for download.
But when a user clicks on the download link from Ge.tt, Live API sent out an event that is picked up by Gett.NET library.

This event enables you to choose what file to upload, and you can share an always updated file. For other files, that is uploaded, Live API sent out an event every time a user clicks on the download link which will enable you to collect real-time statistics.

Using the Code

Gett.NET library has a base class GettUser. From GettUser, you have access to all shares from class GettShares and access to Live API events from GettLive.

GettShares can have zero or more share GettShare objects and GettShare can have zero or more files GettFile.

All errors will throw exceptions. Remember to use try-catch blocks.

Let us start with logging in at Ge.tt Web Services. Have you API key, login and password information ready.
In all examples, API key is "apitest", login information is "apitest@ge.tt" and password is "secret".

C#
// Login to Ge.tt, create a GettUser object.
// You have to provide a valid API key, login and password.
//
Gett.Sharing.GettUser user = new Gett.Sharing.GettUser();

// Try to login
user.Login("apitest", "apitest@ge.tt", "secret");

// Now we are logged in at Ge.tt. Let us retrieve information for our user.
user.RefreshMe();
Console.WriteLine("Login user: {0} ({1])", user.Me.Email, user.Me.FullName);
Console.WriteLine("Storage, you are using {0} of {1} bytes, 
    you still have {2} bytes free.", user.Me.Storage.Used, 
    user.Me.Storage.Total, user.Me.Storage.Free);

If you wish to get current storage information, call GettUser.RefreshMe() method.

A login session does not last forever, it expires in GettUser.SessionExpires seconds. Call GettUser.RefreshLogin() method to keep login session alive.

Now, let us create a share and upload a file.
To upload a file, you have to create or use an existing share. Through GettShares you can create a new share, when the share is created a GettShare object is available.
From GettShare you create a new file through GettFile.

C#
// First create a new share. 
Gett.Sharing.GettShare share1 = user.Shares.CreateShare("My own share");
Console.WriteLine("Share created, with share name: {0} and title:{1}", 
        share1.Info. ShareName, share1.Info.Title);

// Then create a new file.
Gett.Sharing.GettFile file1 = share1.CreateFile("MyDataFile.txt");
Console.WriteLine("New file created at share {0}, with file name: 
  {1} readystate:{2}", file1.Info.ShareName, file1.Info.FileName, file1.Info.ReadyState);

// Upload content of the file to Ge.tt
file1.UploadFile(@"c:\myfolder\MyDataFile.txt");

// Upload is completed. Print out the Ge.tt URL string, that you can give 
// to other users so they can start downloading the file.
Console.WriteLine("Upload completed. Ge.tt URL: {0}", file1.Info.GettUrl);

// Refresh file info
file1.Refresh();
Console.WriteLine("File created at share {0}, with file name: {1} readystate:{2}", 
    file1.Info.ShareName, file1.Info.FileName, file1.Info.ReadyState); 

And you are done! First file uploaded.

Shares on Ge.tt has an unique URL, like http://ge.tt/4ddfds, this URL will list all files in a share. A file has its own URL like http://ge.tt/4ddfds/v/0.

Let us download the file again.

C#
// First you have to get access to the share you want to download a file from.
Gett.Sharing.GettShare share2 = user.Shares.GetShare("4ddfds"); // the path from the 
                        // http://ge.tt/4ddfds - 4ddfds

// Get access to the file.
Gett.Sharing.GettFile file2 = share2.FindFileId("0"); // Get the first file in the share.
Console.WriteLine("File created at share {0}, with file name: {1} readystate:{2}", 
    file2.Info.ShareName, file2.Info.FileName, file2.Info.ReadyState);

// Download file content from Ge.tt to a local file.
file2.DownloadFile(@"c:\myfolder\MyDownloadDataFile.txt");

// Download is completed.
Console.WriteLine("Download completed."); 

To delete a file in a share, you call method GettFile.Destroy() or if you want to delete all files and the share, you call method GettShare.Destroy().

To list all shares you have, you access the method GettUser.Shares.GetShares(). This will return an array of GettShare objects.

C#
// List all shares and files under each share.
Gett.Sharing.GettShare[] shares = user.Shares.GetShares();

foreach (Gett.Sharing.GettShare share in shares)
{
  Console.WriteLine("Share {0} with title {1} has {2} files", 
    share.Info.ShareName, share.Info.Title, share.FilesInfo.Length);
  foreach (Gett.Sharing.GettFile.FileInfo fileInfo in share.FileInfo)
  {
    Console.WriteLine("+  File name: {0}, File size: {1}, 
    Download count: {2}", fileInfo.FileName, fileInfo.Size, fileInfo.Downloads);
  }
} 

If you just want to look at or search for a share or file information and do not wish to manipulate, use GettShare.Info and GettShare.FileInfo[] properties.

GettShare.Info has information about the share. Like when it was created, the title, Ge.tt URL etc.
GettShare.FileInfo[] has information on all files in that share like when it was created, file name, file size, download count, etc.

GettShare.Info and GettShare.FileInfo[] information can be outdated. To get up-to-date information, call GettShare.Refresh() method.

Using Live API

Imagine that you would like to give other users the newest file or know when one of your files is downloaded.

Here Live API comes in play. First you will have to login to Ge.tt Web Services using class GettUser. From there, you create GettLive object.
GettLive has an event handler LiveFileEvent. Every time you get a LiveFileEvent event, Ge.tt Live API has information for you.

Live API messages are sent through WebSocket. The following events can be received from Live API.

  • download
    When someone is attempting to download a file, you will receive a download message that contains information on what share and what file.
  • storagelimit
    If you are uploading a file and you are going above your storage limit you will receive a storagelimit message.
  • filestat
    When a file is starting to upload, you will receive a filestat message containing what share, what file and the size of the file.
  • violatedterms
    If a file is being uploaded that violates the terms of use you will receive a violatedterms message with a reason. This can be a virus, copyrighted music, etc.

Let us try Live API.

C#
// To get Live API events, setup an event handler first.
Gett.Sharing.Live.GettLive live = user.GetLive();

// Define what method to be called on Live API events.
live.LiveFileEvent += 
    new Action<Gett.Sharing.Live.GettLive.LiveFileEventInfo>(gett_LiveFileEvent);

// Startup connection to Live API using WebSocket.
live.Startup("mysessionid"); 

Now we are ready to receive Live API events. But wait, what is "mysessionid"?

To support files that are listed as available for download, but have not yet been uploaded. You have to create the file with a "sessionid" first.

C#
// Create a file, that has not yet been uploaded, but will be available for download. 
Gett.Sharing.GettFile file1 = share1.CreateFile("MyLiveDataFile.txt", "mysessionid");

// Do not upload file content. Live API events will tell when a user 
// wants download the file. 

When a user clicks download for "MyLiveDataFile.txt", a Live API event is received in gett_LiveFileEvent method with type “download”.

You can then start uploading your file.

C#
// Live API event handler 
private void gett_LiveFileEvent(Gett.Sharing.Live.GettLive.LiveFileEventInfo info)
{ 
  // "download" message.
  if (info.Type == "download")
  {
    // Check and see if we have to start upload file content.
    Gett.Sharing.GettFile liveFile = user.Shares.GetShare
            (info.ShareName).FindFileInfo(info.FileId);

    // If file is ReadyState is "remote", we have to start upload
    if (liveFile.Info.ReadyState == "remote")
    {
      // Using async upload or else we are blocking WebSocket communication.
      liveFile.UploadFileAsync(@"c:\livefolder\" + liveFile.Info.FileName);
    }
  }
} 

Not everything has to be files, you can upload any kind of data right from your own program. Perhaps you need to create data dynamic from a database, or create data from other data source.

Here you will use GettFile.UploadData() or GettFile.DownloadData() methods.
The methods accepts byte arrays, so all types of data can be shared directly from your program.

C#
// Create a new file.
Gett.Sharing.GettFile file1 = share1.CreateFile("MyDynamicDataFile.txt");
Console.WriteLine("New file created at share {0}, 
    with file name: {1} readystate:{2}", file1.Info.ShareName, 
    file1.Info.FileName, file1.Info.ReadyState);

// Generate file content to Ge.tt
string myDataString = GetDataStringFromExternalSource();
byte[] content = System.Text.Encoding.UTF8.GetBytes(myDataString);
file1.UploadData(content);

// Upload is completed. Print out the Ge.tt URL string, that you can give 
// to other users so they can start downloading the file.
Console.WriteLine("Upload of data completed. Ge.tt URL: {0}", file1.Info.GettUrl);

// Refresh file info
file1.Refresh();
Console.WriteLine("File created at share {0}, with file name: 
  {1} readystate:{2}", file1.Info.ShareName, file1.Info.FileName, file1.Info.ReadyState);
Or download some data to a byte array.

// First you have to get access to the share you want to download a file from.
Gett.Sharing.GettShare share2 = user.Shares.GetShare("4ddfds"); // the path from the 
                    // http://ge.tt/4ddfds - 4ddfds

// Get access to the file.
Gett.Sharing.GettFile file2 = share2.FindFileId("0"); // Get the first file in the share.
Console.WriteLine("File created at share {0}, with file name: 
  {1} readystate:{2}", file2.Info.ShareName, file2.Info.FileName, file2.Info.ReadyState);

// Download file content from Ge.tt to byte array
byte[] content = file2.DownloadData();

// Download is completed.
Console.WriteLine("Download completed.");
string myDataString = System.Text.Encoding.UTF8.GetString(content);
UpdateStringToExternalDataSource(myDataString); 

Gett.NET library has Asynchronous Programming Model (APM) in which asynchronous operations are represented by a pair of Begin/End methods such as GettFile.BeginUploadFile() and GettFile.EndUploadFile() methods.

For GettFile.UploadFileAsync() and GettFile.DownloadFileAsync() it supports Event-Based Asynchronous Pattern (EAP). This allows you to track the progress of upload and download data.
Subscribe to GettFile.DownloadAsyncProgressChanged or GettFile.UploadProgressChanged event handler.

Please check out the testing program that is capable of create/rename/delete shares and upload/download/delete files in share.
The testing program is only to demonstrate how you can use Gett.NET library.
Program settings are saves the API key, login and password under "%LOCALAPPDATA%\GettTest", it is not encrypted and is in plain text.

How does Gett.NET Library Handle JSON Data and WebSocket

JSON data handling is done by this awesome library Json.NET - http://json.codeplex.com/.

WebSocket that is used to make Live API work in Gett.NETT library can be found at https://github.com/sta/websocket-sharp.

Points of Interest

I learnt that REST API and JSON data are not that hard to work with, and Json.NET is doing all the hard work on JSON to object Serialize- and Deserialize of objects.
It is the first time ever that I have worked with WebSocket, and I had never heard of WebSocket before.

History

  • Initial release: 2012-01-15

License

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