Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some examples in C# #1

Open
cGiesen opened this issue Dec 24, 2019 · 9 comments
Open

Some examples in C# #1

cGiesen opened this issue Dec 24, 2019 · 9 comments

Comments

@cGiesen
Copy link

cGiesen commented Dec 24, 2019

Dear Nikki,
I just start to use your API.
I'm a really newby in NextCloud! Thats why I run in lot of errors.
Are thre some examples for C# and FW472 for testing an looking?

In the momentI allways run in this error:
"the requested address is not valid in its context" in line 670 api.cs and I didn't know, is that a error from my NextCloud server or an error of my coding.

Regards Carsten

@nikkilocke
Copy link
Owner

nikkilocke commented Dec 24, 2019 via email

@cGiesen
Copy link
Author

cGiesen commented Dec 24, 2019

Thats the problem. I see the folder, but no setting.json or TestSettings.json

for that, save must be called, but isn't

I change the code, if no JSON file, create one.

But I get the error by this row: "listener.Bind(localEndPoint);"

The requested address is not valid in its context

Perhaps wrong settings in JSON, but I didn't have any idea, whats the correct on.

@nikkilocke
Copy link
Owner

nikkilocke commented Dec 28, 2019 via email

@trossner
Copy link

trossner commented May 7, 2020

a Demo for the TestSettings.json would be Nice....
The folder is generated, but not the json.

@Pilzinsel64
Copy link

a Demo for the TestSettings.json would be Nice....

Yes, that would be nice! Even a bit more descriptions of how to.

@DevAdcoop
Copy link

a Demo for the TestSettings.json would be Nice....

Yes, that would be nice! Even a bit more descriptions of how to.

Hi, news about this issue? we have a demo of this file?

@manuelkroiss
Copy link

Hi,

I just managed to create a TestSettings.json file in the C:\Users\<USERNAME>\AppData\Local\NextcloudApi folder on a windows machine.

{
  "ModifyTests": true,
  "DestructiveTests": false,
  "TestUser": null,
  "TestGroup": null,
  "ServerUri": "https://<nextcloud-url>", <-- SET
  "ApplicationName": "<ServiceName>", <-- SET
  "RedirectUri": "https://<nextcloud-url>", <-- SET
  "RedirectAfterLogin": null,
  "PageToSendAfterLogin": null,
  "LoginExpiryTime": 108000,
  "ClientId": null,
  "ClientSecret": null,
  "AccessToken": null,
  "CsrfToken": null,
  "RefreshToken": null,
  "TokenExpires": "0001-01-01T00:00:00",
  "User": null,
  "Username": "<username>", <-- SET
  "Password": "<password>", <-- SET
  "RefreshTokenIfDueToExpireBefore": "1.00:00:00",
  "LogRequest": 0,
  "LogResult": 0
}

I did set all the fields marked with <-- SET and was able to run the ShowList test.

[TestClass]
public class NextCloudDeploymentServiceTest : TestBase
{

    [TestMethod]
    public void ShowList()
    {
        ShowList(CloudFolder.List(Api, Settings.Username));
    }
}

I think it would be nice to include this settings file in the repo too.

Can anyone test and confirm this?

@ArnieBerg
Copy link

This is great stuff! Here is a short C# console app written in VS 2022 (.Net Framework 4.8) that illustrates one function:

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using NextcloudApi;

namespace NextcloudHelper
{
internal class Program
{
public static Settings _settings;
public static Api _api;

static void Main(string[] args)
{
  Task<List<CloudInfo>> cloudInfo = CloudFolder.List(Api, Settings.Username);
  List<CloudInfo> result = RunTask(cloudInfo);

  foreach (CloudInfo o in result)
  {
    Console.WriteLine(o);
  }
  Console.ReadKey();
}

public static T RunTask<T>(Task<T> task)
{
  T t = task.Result;
  Console.WriteLine(t);
  return t;
}


public static Api Api
{
  get
  {
    if (_api == null)
    {
      _api = new Api(Settings);
    }
    return _api;
  }
}

public static Settings Settings
{
  get
  {
    if (_settings == null)
    {
      string dataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "NextcloudApi");
      Directory.CreateDirectory(dataPath);
      string filename = Path.Combine(dataPath, "TestSettings.json"); // C:\Users\Administrator\AppData\Local\NextCloudAPI
      _settings = new Settings();
      _settings.Load(filename);
      List<string> errors = _settings.Validate();
      if (errors.Count > 0)
        throw new ApplicationException(string.Join("\r\n", errors));
    }
    return _settings;
  }
}

}
}

You will need to reference Newtonsoft.Json 12.0.2 and MimeMapping 1.0.1.12 from Nuget.

What I would like to know is how do you form the link to a file that I can provide to an eligible user for them to access/download the file from Nextcloud? You can copy the internal link to the clipboard in Nextcloud, which looks something like:
http://localhost:8080/f/320
When you put this URL in the browser, it shows as:
http://localhost:8080/apps/files/?dir=/&fileid=6
It then displays the contents of the referenced file.

How can I derive this internal link programmatically?

@ArnieBerg
Copy link

Answer to my question (pretty simple) - Insert these lines:

  CloudInfo props = RunTask(CloudInfo.GetProperties(Api, Settings.Username + "/ChangeLog.txt", CloudInfo.Properties.FileId));
  Console.WriteLine("FileId for ChangeLog.txt is " + props.FileId.ToString() + ", Id is " + (props.Id == null ? "(null)" : props.Id.ToString()) + ".");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants