Skip to content

Commit

Permalink
Improve repository files API (#443)
Browse files Browse the repository at this point in the history
* Add `JsonPropertyName` for `FileDelete` and `FileUpsert`

* Remove query params and use body to pass parameters in repository files api
  • Loading branch information
Sjhzjxc authored May 15, 2023
1 parent ecea587 commit 8b6fea5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 22 deletions.
25 changes: 3 additions & 22 deletions NGitLab/Impl/FilesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,17 @@ public FilesClient(API api, string repoPath)

public void Create(FileUpsert file)
{
var url = AppendQueryParams($"{_repoPath}/files/{EncodeFilePath(file.Path)}", file);
_api.Post().With(file).Execute(url);
_api.Post().With(file).Execute($"{_repoPath}/files/{EncodeFilePath(file.Path)}");
}

public void Update(FileUpsert file)
{
var url = AppendQueryParams($"{_repoPath}/files/{EncodeFilePath(file.Path)}", file);
_api.Put().With(file).Execute(url);
_api.Put().With(file).Execute($"{_repoPath}/files/{EncodeFilePath(file.Path)}");
}

public void Delete(FileDelete file)
{
var url = AppendQueryParams($"{_repoPath}/files/{EncodeFilePath(file.Path)}", file);
_api.Delete().Execute(url);
_api.Delete().With(file).Execute($"{_repoPath}/files/{EncodeFilePath(file.Path)}");
}

public FileData Get(string filePath, string @ref)
Expand Down Expand Up @@ -67,21 +64,5 @@ private static string EncodeFilePath(string path)
{
return Uri.EscapeDataString(path);
}

private static string AppendQueryParams(string url, FileUpsert fileUpsert)
{
url = Utils.AddParameter(url, "branch", fileUpsert.Branch);
url = Utils.AddParameter(url, "encoding", fileUpsert.Encoding);
url = Utils.AddParameter(url, "content", fileUpsert.Content);
url = Utils.AddParameter(url, "commit_message", fileUpsert.CommitMessage);
return url;
}

private static string AppendQueryParams(string url, FileDelete fileDelete)
{
url = Utils.AddParameter(url, "branch", fileDelete.Branch);
url = Utils.AddParameter(url, "commit_message", fileDelete.CommitMessage);
return url;
}
}
}
4 changes: 4 additions & 0 deletions NGitLab/Models/FileDelete.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;

namespace NGitLab.Models
{
public class FileDelete
{
[Required]
[JsonPropertyName("path")]
public string Path;

[Required]
[JsonPropertyName("branch")]
public string Branch;

[Required]
[JsonPropertyName("commit_message")]
public string CommitMessage;
}
}
6 changes: 6 additions & 0 deletions NGitLab/Models/FileUpsert.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;

namespace NGitLab.Models
{
public class FileUpsert
{
[Required]
[JsonPropertyName("path")]
public string Path;

[Required]
[JsonPropertyName("branch")]
public string Branch;

[JsonPropertyName("encoding")]
public string Encoding;

[Required]
[JsonPropertyName("content")]
public string Content;

/// <summary>
Expand All @@ -29,6 +34,7 @@ public string RawContent
}

[Required]
[JsonPropertyName("commit_message")]
public string CommitMessage;

public static string Base64Encode(string plainText)
Expand Down

0 comments on commit 8b6fea5

Please sign in to comment.