如何返回Mega帐户信息表Mega API [英] How to return Mega Account Information Form Mega API

查看:117
本文介绍了如何返回Mega帐户信息表Mega API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手,这是我第一次使用此API

我想从API获取超级帐户信息.

I want to get Mega Account Information from the API.

我可以获得( TotalQuota UsedQuota AvailableQuota ="TotalQuota-UsedQuota"),并附带代码打击(我可以将其转换为GB).

I can get (TotalQuota , UsedQuota , AvailableQuota = "TotalQuota - UsedQuota") with the code blow (and I can convert it to GB).

MegaApiClient myMegaClient = new MegaApiClient();

public void megaAccLogIn(string megaAccUserName,string megaAccPassword)
{
    if (myMegaClient.IsLoggedIn == true)
    {
        return;
    }
    else
    {
        myMegaClient.Login(megaAccUserName, megaAccPassword);
    }
}

public struct AccountInfo : IAccountInformation
{
    public long TotalQuota { get; set; }
    public long UsedQuota { get; set; }
    public IEnumerable<IStorageMetrics> Metrics { get; set; }

    public long AvailableQuota;
}

public AccountInfo getfoldercount()
{
    AccountInfo myAccountInformation = new AccountInfo();

    megaAccLogIn(megaAccUserName, megaAccPassword);

    var myMegaClintGetAccInfo = myMegaClient.GetAccountInformation();

    //IEnumerable<IStorageMetrics> test = myMegaClintGetAccInfo.Metrics;




    myAccountInformation.TotalQuota = myMegaClintGetAccInfo.TotalQuota;
    myAccountInformation.UsedQuota = myMegaClintGetAccInfo.UsedQuota;
    myAccountInformation.AvailableQuota = myMegaClintGetAccInfo.TotalQuota - myMegaClintGetAccInfo.UsedQuota;
    myAccountInformation.Metrics = myMegaClintGetAccInfo.Metrics;

    return myAccountInformation;
}

这是运行方法的代码

private void button5_Click(object sender, EventArgs e)
{
    var myAccountInformation = getfoldercount();

    textBox1.Text = FormatBytes(myAccountInformation.TotalQuota) + "\r\n" + FormatBytes(myAccountInformation.UsedQuota) + "\r\n" + myAccountInformation.Metrics;

}

图片运行"myAccountInformation.Metrics"时出现的错误

这是我要搜索的数据图片

我遇到了此异常尝试最后的出门或以其他方式获得此例外

"指标"包含一些重要数据,我想获取它,但是我无法实现将其作为字符串接收的代码.

"Metrics" has some important data and I want to get it and I cannot implement the code to receive it as string.

我想获取( FoldersCount FilesCount BytesUsed NodeId )

我尝试过每次搜索样品的过程,但没有发现.

I have tried every searching for samples and found none.

我来自" Github ",但是我仍然不知道如何使用它以及 接口 是什么.我不需要C#的课程,只需对正在发生的事情有一点了解,就可以继续.

I have this from "Github" but I still don't understand how to use it and what an Interface is. I don't need a lesson in C# just small insight about what is going on so I can continue.

来自此页面

如果有人能帮助我,我会很感激.

If any one would help me I will be thankful.

推荐答案

根据您提供给MegaApiClient的链接,您没有正确设置AccountInfo.我可能是错的;此答案仅基于您提供的内容以及我认为应该匹配的到源MegaApiClient的链接.

Based on the link you've provided to MegaApiClient you are not setting up your AccountInfo properly. I could be wrong; this answer is based only on what you've provided and the link to the source MegaApiClient, which I assume is supposed to match.

我首先要写我的AccountInfo来匹配IAccountInformation接口,如下所示:

I would first write my AccountInfo to match the IAccountInformation interface like so:

public struct AccountInfo : IAccountInformation
{
    public long TotalQuota { get; set; }
    public long UsedQuota { get; set; }
    public IEnumerable<IStorageMetrics> Metrics { get; set; }
}

我相信,只需更改一下即可解决您遇到的错误.在您的代码中,您指出Metricslong,而实际上它必须是IEnumerable<IStorageMetrics>.如果您需要进一步的解释,请告诉我,我很乐意发布/解释更多.

Just changing that, I believe, will fix the error you're having. In your code you're stating that Metrics is a long when in fact it needs to be IEnumerable<IStorageMetrics>. If you need further explaining let me know and I'll gladly post / explain more.

自从您询问有关Interface的见解以来,我将尝试简短地进行解释.

Since you asked about insight about Interface I will try to explain it short and brief.

Interface本质上是object外观的蓝图.它说明object的功能.例如:在IAccountInformation界面中(通过您提供的链接),它声明一个对象将至少具有一个只读属性long TotalQouta.它还指出还有两个其他属性long UserQoutaIEnumerable<IStorageMetrics> Metrics.

An Interface is basically a blue print of what an object should look like. It explains features of the object. For example: in the IAccountInformation interface (via the link you provided) it states that an object will have at least a read only property long TotalQouta. It also states there are two other properties long UserQouta and IEnumerable<IStorageMetrics> Metrics.

因此,如果制作使用此Interfaceobject,则必须实现这些功能以匹配Interface.您会看到我为您添加了Interfacestruct并精确地实现了它. (我确实也向属性添加了setter,因为您是通过这种方式使用它们的.)

So if you make an object that uses this Interface you must implement those features to match the Interface. You'll see I added the Interface to the struct for you and implemented it precisely. (I did add setters to the properties as well since you are using them this way.)

多态性的使用是摆脱这种有趣的事情.现在,您可以通过直接类型或通过IAccountInformation接口作为类型来访问对象.这对于仅知道Interface而不是您自己编写的Type(或"object"")的方法很有用.

Something interesting to take away from this is the use of polymorphism. You can now access your object via the direct type or via the IAccountInformation interface as a type. This is useful for methods that are only aware of the Interface and not the Type (or ```object````) that you're going to write on your own.

另一个有趣的事情是,IAccountInformation Interface中已经在使用多态了. property IEnumerable<IStoreMetrics> Metrics { get; }表示从IAccountInformation派生的任何Type还将具有从IStorageMetrics派生的类型的枚举.

Another interesting thing to take away is how polymorphism is being used in the IAccountInformation Interface already. The property IEnumerable<IStoreMetrics> Metrics { get; } is stating that any Type that derives from IAccountInformation will also have an enumeration of types that derive from IStorageMetrics.

当我们通过提供的链接查看IStorageMetrics时,将看到4个属性(NodeId,BytesUsed,FilesCount,FoldersCount).

When we look at IStorageMetrics (via the link you provided) you will see 4 properties (NodeId, BytesUsed, FilesCount, FoldersCount).

代码中的Interface类似于娱乐接收机,电视或计算机背面的端口.您有HDMI,RCA,光纤,USB等.如果您的接收器的RCA和光纤输出为,则您知道可以将RCA电缆插入其中,也可以将光纤电缆插入其中,因为这是接收者. RCA插孔旨在做一些特别的事情,但接收器不知道您要插入什么插孔.它只知道它必须匹配该接口.代码是相同的方式...我们不知道谁在使用IAccountInformation Interface,但是我们确实知道它将具有IAccountInformation所说的设置,并且我们可以通过这些通信.

An Interface in code is like the ports on the back of an entertainment receiver, television, or computer. You have HDMI, RCA, Optic, USB, etc... If you have a receiver that has RCA out and Optic out then you know you can plug either RCA cables into it or a fiber optic cable into it because that's the interface for the receiver. The RCA jacks are meant to do something particular but the receiver has no idea what you're going to plug into it. It just knows it has to match that interface. Code is the same way... We don't know who is using the IAccountInformation Interface but we do know that it will have the setup that IAccountInformation says and we can communicate to it via those.

好的,所以您已经对问题进行了一些更新.我将保留已发布的内容,并在此处发布更多内容.

别忘了MetricsIEnumerable<IStorageMetrics>,这意味着它是IStorageMetrics类型的枚举. (如果有助于可视化,可以将Metrics视为数组或IStorageMetrics的列表.IEnumerable仅表示可以枚举类型.

Don't forget that Metrics is IEnumerable<IStorageMetrics> which means it's an enumeration of IStorageMetrics types. (If it helps to visualize it think of Metrics as an array or list of IStorageMetrics. IEnumerable just means that the type can be enumerated on.

枚举有多种用法,但最常见的一种是在循环中,例如foreach循环.因此,仅作为示例,您可以像这样从Metrics获取信息.

Enumerations get used in many ways but one of the most common is in a loop such as a foreach loop. So for example only, you could get the information from Metrics like so.

foreach (var storageMetric in myAccountInformation.Metrics)
{
    var bytesUsed = storageMetric.BytesUsed;
    var filesCount = storageMetric.FilesCount;
    var foldersCount = storageMetric.FoldersCount;
    var nodeId = storageMetric.NodeId;
    // do what you want with the info here
}

IEnumerable的另一种流行用法是使用System.Linq,它在所有查询的核心都使用IEnumerable. 您可以像以前那样使用linq,就像这样...

Another popular use of IEnumerable is using System.Linq which uses IEnumerable at the heart of all queries. You could use linq, the way you had it earlier, like so...

 // The following will result in totalBytes being the sum of all Metrics
 var totalBytes = myAccountInformation.Metrics.Sum(storageMetric => storageMetric.BytesUsed);

因此,您可以按照以下内容写一些文字:(如果您想要的值当然是BytesUsed的总和.)

So you could write the text with something along the lines of this: (if the values you want are the sum of BytesUsed of course.)

var totalBytesUsed = myAccountInformation.Metrics.Sum(storageMetric => storageMetric.BytesUsed);
textBox1.Text = FormatBytes(myAccountInformation.TotalQuota) + "\r\n" + FormatBytes(myAccountInformation.UsedQuota) + "\r\n" + totalBytesUsed;

这有意义吗?

这篇关于如何返回Mega帐户信息表Mega API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆