Skip to content

Commit

Permalink
Standardized terminology from 'fetch' to 'request' and did minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-jacob committed Feb 4, 2025
1 parent 7fdda35 commit 2c36f8f
Showing 1 changed file with 23 additions and 28 deletions.
51 changes: 23 additions & 28 deletions Trell/CliCommands/RunCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public override ValidationResult Validate() {
} else if (this.HandlerFn == "request") {
if (string.IsNullOrWhiteSpace(this.DataFile)
|| !File.Exists(Path.GetFullPath(this.DataFile))) {
return ValidationResult.Error("Fetching requires a valid path for an existing file be passed as an argument");
return ValidationResult.Error("A valid path to an existing file is required for making requests");
}
if (this.Url is not null && !Uri.IsWellFormedUriString(this.Url, UriKind.Absolute)) {
return ValidationResult.Error("An ill-formed URL was given as an argument");
Expand Down Expand Up @@ -123,51 +123,46 @@ Rpc.ServerWorkOrder GetServerWorkOrder(RunCommandSettings settings, TrellConfig
};
}

static Rpc.Upload GenerateUpload(RunCommandSettings settings) {
static Rpc.Request GenerateRequest(RunCommandSettings settings) {
if (string.IsNullOrWhiteSpace(settings.DataFile)) {
return new();
}
var uploadDataPath = Path.GetFullPath(settings.DataFile);
var fileName = Path.GetFileName(uploadDataPath);
var uploadDataBytes = File.ReadAllBytes(uploadDataPath);
var requestDataPath = Path.GetFullPath(settings.DataFile);
var fileName = Path.GetFileName(requestDataPath);
var requestDataBytes = File.ReadAllBytes(requestDataPath);
if (!new FileExtensionContentTypeProvider().TryGetContentType(fileName, out var fileType)) {
// Fallback to ASP.Net's default MIME type for binary files
fileType = "application/octet-stream";
}

Dictionary<string, string> headers = settings.ValidatedHeaders ?? [];
headers["Content-Encoding"] = fileType;

return new() {
Filename = fileName,
Content = ByteString.CopyFrom(uploadDataBytes),
Type = fileType,
Url = settings.Url ?? "http://www.example.com/fetch",
Method = "POST",
Headers = {
headers,
},
Body = ByteString.CopyFrom(requestDataBytes),
};
}

static Rpc.Request GenerateFetch(RunCommandSettings settings) {
static Rpc.Upload GenerateUpload(RunCommandSettings settings) {
if (string.IsNullOrWhiteSpace(settings.DataFile)) {
return new();
}
var fetchDataPath = Path.GetFullPath(settings.DataFile);
var fileName = Path.GetFileName(fetchDataPath);
var fetchDataBytes = File.ReadAllBytes(fetchDataPath);
var uploadDataPath = Path.GetFullPath(settings.DataFile);
var fileName = Path.GetFileName(uploadDataPath);
var uploadDataBytes = File.ReadAllBytes(uploadDataPath);
if (!new FileExtensionContentTypeProvider().TryGetContentType(fileName, out var fileType)) {
// Fallback to ASP.Net's default MIME type for binary files
fileType = "application/octet-stream";
}

Dictionary<string, string> headers;
if (settings.ValidatedHeaders is null) {
headers = new() { { "Content-Encoding", fileType } };
} else {
settings.ValidatedHeaders["Content-Encoding"] = fileType;
headers = settings.ValidatedHeaders;
}

return new() {
Url = settings.Url ?? "/fetch",
Method = "POST",
Headers = {
headers,
},
Body = ByteString.CopyFrom(fetchDataBytes),
Filename = fileName,
Content = ByteString.CopyFrom(uploadDataBytes),
Type = fileType,
};
}

Expand All @@ -181,7 +176,7 @@ Rpc.Function GetFunction(RunCommandSettings settings) {
},
},
"request" => new() {
OnRequest = GenerateFetch(settings),
OnRequest = GenerateRequest(settings),
},
"upload" => new() {
OnUpload = GenerateUpload(settings),
Expand Down

0 comments on commit 2c36f8f

Please sign in to comment.