-
Notifications
You must be signed in to change notification settings - Fork 17
Added support for adding, deleting and listing share links #17
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
Open
cniccolo
wants to merge
1
commit into
renber:master
Choose a base branch
from
cniccolo:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| using System; | ||
| using System.Globalization; | ||
| using System.Net; | ||
| using System.Net.Http; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using SeafClient.Requests.Files; | ||
| using SeafClient.Types; | ||
|
|
||
| namespace SeafClient.Tests | ||
| { | ||
| [TestClass] | ||
| public class ShareLinks : SeafTestClassBase | ||
| { | ||
| [TestMethod] | ||
| public void Test_GetShareLink_Success() | ||
| { | ||
| var request = new CreateShareLinkRequest(FakeToken, FakeRepoId, "/"); | ||
|
|
||
| var message = new HttpResponseMessage(HttpStatusCode.OK) | ||
| { | ||
| Content = new StringContent(@"{ | ||
| ""username"": ""lian@lian.com"", | ||
| ""repo_id"": ""c474a093-19dc-4ddf-b0b0-72b33214ba33"", | ||
| ""ctime"": ""2017-04-01T02:35:57+00:00"", | ||
| ""expire_date"": """", | ||
| ""token"": ""6afa667ff2c248378b70"", | ||
| ""view_cnt"": 0, | ||
| ""link"": ""https://cloud.seafile.com/d/6afa667ff2c248378b70/"", | ||
| ""obj_name"": ""/"", | ||
| ""path"": ""/"", | ||
| ""is_dir"": true, | ||
| ""permissions"": { | ||
| ""can_edit"": false, | ||
| ""can_download"": true | ||
| }, | ||
| ""is_expired"": false, | ||
| ""repo_name"": ""seacloud.cc.124"" | ||
| }]") | ||
| }; | ||
|
|
||
| Assert.IsTrue(request.WasSuccessful(message)); | ||
| var result = ExecuteSync(() => request.ParseResponseAsync(message)); | ||
|
|
||
| Assert.AreEqual("/", result.Name); | ||
|
|
||
| //Assert.AreEqual(FakeRepoId, result.LibraryId); | ||
| //Assert.AreEqual("/test/subfolder/foo.py", result.Path); | ||
|
|
||
| Assert.AreEqual("6afa667ff2c248378b70", result.Id); | ||
|
|
||
| Assert.AreEqual(true, result.IsDirectory); | ||
| //Assert.AreEqual(22, result.Size); | ||
| //// converted the timestamp 1398148877 using http://www.onlineconversion.com/unix_time.htm | ||
| //// note: comparison is done in local time | ||
| //Assert.AreEqual(DateTime.Parse("Tue, 22 Apr 2014 06:41:17 GMT", CultureInfo.InvariantCulture), result.Timestamp); | ||
| } | ||
| } | ||
| } |
103 changes: 103 additions & 0 deletions
103
SeafClient/Requests/ShareLinks/CreateShareLinkRequest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| using Newtonsoft.Json; | ||
| using SeafClient.Requests; | ||
| using SeafClient.Types; | ||
| using System; | ||
| using System.Net; | ||
| using System.Net.Http; | ||
| using System.Text; | ||
|
|
||
| namespace SeafClient | ||
| { | ||
|
|
||
| public class CreateShareLinkRequest : SessionRequest<SeafShareLink> | ||
| { | ||
| public string LibraryId { get; set; } | ||
|
|
||
| public string Path { get; set; } | ||
|
|
||
| public string Password { get; set; } | ||
|
|
||
| public int ExpiryDays { get; set; } | ||
|
|
||
| public bool CanEdit { get; set; } | ||
|
|
||
| public bool CanDownload { get; set; } | ||
|
|
||
|
|
||
| public override string CommandUri | ||
| { | ||
| get { return "api/v2.1/share-links/";} | ||
| } | ||
|
|
||
| public override HttpAccessMethod HttpAccessMethod | ||
| { | ||
| get { return HttpAccessMethod.Custom; } | ||
| } | ||
|
|
||
| public CreateShareLinkRequest( | ||
| string authToken, | ||
| string pLibraryId, | ||
| string pPath, | ||
| string pPassword="", | ||
| int pExpiryDays = 0, | ||
| bool pCanEdit = false, | ||
| bool pCanDownload = true | ||
| ) : base(authToken) | ||
| { | ||
| LibraryId = pLibraryId; | ||
| Path = pPath; | ||
|
|
||
| if (!Path.StartsWith("/")) | ||
| Path = "/" + Path; | ||
|
|
||
| Password = pPassword; | ||
| ExpiryDays = pExpiryDays; | ||
| CanEdit = pCanEdit; | ||
| CanDownload = pCanDownload; | ||
| } | ||
|
|
||
| public override SeafError GetSeafError(HttpResponseMessage msg) | ||
| { | ||
| switch (msg.StatusCode) | ||
| { | ||
| case HttpStatusCode.BadRequest: | ||
| return new SeafError(msg.StatusCode, SeafErrorCode.PathDoesNotExist); | ||
| case HttpStatusCode.NotFound: | ||
| return new SeafError(msg.StatusCode, SeafErrorCode.FileNotFound); | ||
| default: | ||
| return base.GetSeafError(msg); | ||
| } | ||
| } | ||
|
|
||
| public override HttpRequestMessage GetCustomizedRequest(Uri serverUri) | ||
| { | ||
|
|
||
| Uri uri = new Uri(serverUri, CommandUri); | ||
|
|
||
| HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, uri); | ||
|
|
||
| message.Headers.Referrer = uri; | ||
| foreach (var hi in GetAdditionalHeaders()) | ||
| message.Headers.Add(hi.Key, hi.Value); | ||
|
|
||
| var data = new SeafShareLinkRequest() | ||
| { | ||
| LibraryId = LibraryId, | ||
| Path = Path, | ||
| ExpireDays = ExpiryDays, | ||
| Permission = new SeafShareLinkPermissions() | ||
| { | ||
| CanDownload = CanDownload, | ||
| CanEdit = CanEdit | ||
| } | ||
|
|
||
| }; | ||
|
|
||
| message.Content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json"); | ||
| return message; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace SeafClient.Requests.ShareLinks | ||
| { | ||
| public class DeleteShareLinkRequest : SessionRequest<bool> | ||
| { | ||
| public string ShareLinkToken { get; set; } | ||
|
|
||
| public override string CommandUri | ||
| { | ||
| get { return String.Format("api/v2.1/share-links/{0}/", ShareLinkToken); } | ||
| } | ||
|
|
||
| public override HttpAccessMethod HttpAccessMethod | ||
| { | ||
| get { return HttpAccessMethod.Delete; } | ||
| } | ||
|
|
||
| public DeleteShareLinkRequest(string authToken, string pShareLinkToken) | ||
| : base(authToken) | ||
| { | ||
| ShareLinkToken = pShareLinkToken; | ||
| } | ||
|
|
||
| public override async Task<bool> ParseResponseAsync(System.Net.Http.HttpResponseMessage msg) | ||
| { | ||
| string content = await msg.Content.ReadAsStringAsync(); | ||
| return content == "\"success\""; | ||
| } | ||
|
|
||
|
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| using SeafClient.Types; | ||
| using SeafClient.Utils; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Net; | ||
| using System.Net.Http; | ||
|
|
||
| namespace SeafClient.Requests.ShareLinks | ||
| { | ||
| public class ListShareLinksRequest : SessionRequest<List<SeafShareLink>> | ||
| { | ||
| public string LibraryId {get;set;} | ||
| public string Path { get; set; } | ||
|
|
||
| public override string CommandUri | ||
| { | ||
| get | ||
| { | ||
| var tmp = new List<string>(); | ||
| if (LibraryId!="") | ||
| { | ||
| tmp.Add("repo_id=" + LibraryId); | ||
| } | ||
| if (Path != "") | ||
| { | ||
| tmp.Add("path=" + WebUtility.UrlEncode(Path)); | ||
| } | ||
|
|
||
| var add = ""; | ||
| if (tmp.Count > 0) | ||
| { | ||
| add = "?" + String.Join("&", tmp); | ||
| } | ||
|
|
||
| return "api/v2.1/share-links/"+add; | ||
| } | ||
| } | ||
|
|
||
| public ListShareLinksRequest(string authToken, string pLibraryId="", string pPath="") | ||
| : base(authToken) | ||
| { | ||
| ParamUtils.ThrowOnNull(pLibraryId, "pLibraryId"); | ||
| ParamUtils.ThrowOnNull(pPath, "pPath"); | ||
|
|
||
| LibraryId = pLibraryId; | ||
| Path = pPath; | ||
| } | ||
|
|
||
|
|
||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Restarting a dead project here: the response returned from seafile is a JSON object with a single "success" boolean property.
https://download.seafile.com/published/web-api/v2.1/share-links.md#user-content-Delete%20Share%20Link
{"success":true}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for contributing. The library is not really dead in the actual sense as I am using it daily in production. However, all the features which are needed there have been implemented already and I lack time to continue development for other parts of the API.
So, any help is much appreciated :)
I will check your review and hopefully this merge request can finally find its way into the library.