Gett.NET - Ge.tt Web Services 库
使用 Ge.tt Web Services 进行实时文件发布和共享。
https://nuget.net.cn/packages/Gett.NET/
引言
Gett.NET 是一个 C# 库,用于 Ge.tt Web Services REST API 和 Live API。
Ge.tt(http://ge.tt)是一项即时、实时的文件发布和共享服务。
使用 Ge.tt,您可以将任何类型的文件转化为 Web 内容并即时共享。您可以共享文档、视频、音乐和照片,使其立即可用。
Ge.tt 适用于专业和个人用途。
- 实时共享
一旦选择了文件,它们就可以立即发布或共享!无需等待文件上传。 - 实时统计
您可以轻松跟踪有多少人下载或查看了您的文件。
请访问 http://ge.tt/about 阅读更多内容。
要开始使用,您必须在 Ge.tt 创建或使用现有用户账户。
访问 http://ge.tt 并点击“SIGN UP”。注册基本账户是免费的,填写表格即可开始。
在 http://ge.tt 成功登录后,访问 http://ge.tt/developers 并点击“Create app”,填写表格以获取您自己的 API 密钥,然后点击“Create app”按钮。
现在您已经获得了最重要的信息——API 密钥!没有 API 密钥,您就无法使用 Gett.NET 库。请记住,您的 API 密钥是保密的和私有的,请妥善保管。
背景
创建 Gett.NET 库的想法是为了学习如何使用 RESTful Web Services 和 JSON 数据。
我当时正在做一个大型项目,其中数据访问是通过 REST API 和 JSON 数据进行的。
在开始我的大型项目之前,我想通过实践来学习,并认为通过创建一个能使用 Ge.tt Web Services 的 C# 库会很有趣。
我经常使用 http://ge.tt 从客户站点传输日志文件,将文件分享给其他人或家人。
不知怎么的,我想到直接在我的程序中实现共享功能会很酷。通过程序中的一个按钮,我可以即时将日志文件或备份文件上传到我在 Ge.tt 的账户。
Ge.tt 有什么特别之处
Ge.tt 与互联网上“所有其他上传服务”有何不同?
最独特的是即时上传和下载服务。当您通过 Gett.NET 库或网站开始上传大文件时,其他人可以立即开始下载该大文件,无需等待大文件上传完成。
如果您需要 Ge.tt 高级队列功能,那么请了解 Live API。
Live API 的理念是,您可以通过 Gett.NET 库创建一个新的共享文件,但您不上传该文件。
只要 Gett.NET 库与 Live API 有连接,所有这样创建的文件都会被列为可供下载。
但是,当用户点击 Ge.tt 的下载链接时,Live API 会发送一个事件,被 Gett.NET 库捕获。
这个事件使您能够选择要上传的文件,并可以共享一个始终更新的文件。对于其他已上传的文件,每次用户点击下载链接时,Live API 都会发送一个事件,使您能够收集实时统计信息。
Using the Code
Gett.NET 库有一个基类 GettUser
。从 GettUser
,您可以访问 GettShares
类中的所有共享,并从 GettLive
访问 Live API 事件。
GettShares
可以包含零个或多个共享 GettShare
对象,而 GettShare
可以包含零个或多个文件 GettFile
。
所有错误都会抛出异常。请记住使用 try-catch
块。
让我们开始登录 Ge.tt Web Services。请准备好您的 API 密钥、登录信息和密码。
在所有示例中,API 密钥为“apitest
”,登录信息为“apitest@ge.tt
”,密码为“secret
”。
// 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);
如果您想获取当前存储信息,请调用 GettUser.RefreshMe()
方法。
登录会话不是永久的,它会在 GettUser.SessionExpires
秒后过期。请调用 GettUser.RefreshLogin()
方法来保持登录会话的活动。
现在,让我们创建一个共享并上传一个文件。
要上传文件,您必须创建或使用现有的共享。通过 GettShares
,您可以创建一个新的共享,创建共享后,将提供一个 GettShare
对象。
从 GettShare
,您可以通过 GettFile
创建一个新文件。
// 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);
您就完成了!第一个文件已上传。
Ge.tt 上的共享有一个唯一的 URL,例如 http://ge.tt/4ddfds,此 URL 将列出共享中的所有文件。文件有自己的 URL,例如 http://ge.tt/4ddfds/v/0。
让我们再次下载文件。
// 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.");
要删除共享中的文件,请调用 GettFile.Destroy()
方法;如果您想删除所有文件和共享,请调用 GettShare.Destroy()
方法。
要列出您拥有的所有共享,请访问 GettUser.Shares.GetShares()
方法。这将返回一个 GettShare
对象数组。
// 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);
}
}
如果您只想查看或搜索共享或文件信息而不进行操作,请使用 GettShare.Info
和 GettShare.FileInfo[]
属性。
GettShare.Info
包含共享的信息。例如创建时间、标题、Ge.tt URL 等。
GettShare.FileInfo[]
包含该共享中所有文件的信息,例如创建时间、文件名、文件大小、下载次数等。
GettShare.Info
和 GettShare.FileInfo[]
的信息可能已过时。要获取最新信息,请调用 GettShare.Refresh()
方法。
使用 Live API
想象一下,您想提供最新的文件给其他用户,或者知道您的某个文件何时被下载。
这时 Live API 就派上用场了。首先,您需要使用 GettUser
类登录 Ge.tt Web Services。然后,您将创建一个 GettLive
对象。
GettLive
有一个事件处理程序 LiveFileEvent
。每次收到 LiveFileEvent
事件时,Ge.tt Live API 都会为您提供信息。
Live API 消息通过 WebSocket 发送。以下事件可以从 Live API 接收。
下载
当有人尝试下载文件时,您将收到一个download
消息,其中包含关于共享和文件的信息。storagelimit
当您上传文件并超出存储限制时,您将收到一个storagelimit
消息。filestat
当文件开始上传时,您将收到一个filestat
消息,其中包含共享、文件和文件大小信息。violatedterms
如果上传的文件违反使用条款,您将收到一个violatedterms
消息,其中包含原因。这可能是病毒、盗版音乐等。
让我们尝试 Live API。
// 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");
现在我们已准备好接收 Live API 事件。但是,等等,“mysessionid”是什么?
为了支持那些已列出但尚未上传的文件,您必须先使用“sessionid
”创建文件。
// 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.
当用户点击“MyLiveDataFile.txt”的下载链接时,会在 gett_LiveFileEvent
方法中收到一个类型为“download
”的 Live API 事件。
然后您可以开始上传您的文件。
// 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);
}
}
}
并非所有内容都必须是文件,您可以直接从自己的程序中上传任何类型的数据。也许您需要从数据库动态创建数据,或者从其他数据源创建数据。
在这里,您将使用 GettFile.UploadData()
或 GettFile.DownloadData()
方法。
这些方法接受字节数组,因此所有类型的数据都可以直接从您的程序中共享。
// 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 库具有异步编程模型 (APM),其中异步操作由一组 Begin
/End
方法表示,例如 GettFile.BeginUploadFile()
和 GettFile.EndUploadFile()
方法。
对于 GettFile.UploadFileAsync()
和 GettFile.DownloadFileAsync()
,它支持基于事件的异步模式 (EAP)。这允许您跟踪上传和下载数据的进度。
订阅 GettFile.DownloadAsyncProgressChanged
或 GettFile.UploadProgressChanged
事件处理程序。
请查看该测试程序,它能够创建/重命名/删除共享并上传/下载/删除共享中的文件。
该测试程序仅用于演示如何使用 Gett.NET 库。
程序设置将 API 密钥、登录名和密码保存在“%LOCALAPPDATA%\GettTest”下,未加密,是纯文本格式。
Gett.NET 库如何处理 JSON 数据和 WebSocket
JSON 数据处理由这个很棒的库 Json.NET 完成 - http://json.codeplex.com/。
用于使 Gett.NETT 库中的 Live API 正常工作的 WebSocket 可以在 https://github.com/sta/websocket-sharp 找到。
关注点
我了解到 REST API 和 JSON 数据并不难处理,而 Json.NET 承担了 JSON 对象 Serialize
和 Deserialize
的繁重工作。
这是我第一次接触 WebSocket,我以前从未听说过 WebSocket。
历史
- 初始发布:2012-01-15