-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial Commit * Changed to include production, test and development endpoints * tweaking the urls * Moved from incorrectly placed in independent-publisher-connectors * Tweaking the readme * tweaking readme and name of action * Included etag support for PUT requests * - added missing bits to the readme.md - removed empty strings - Made the clientid [DUMMY] * Added the missing definitions * Changed 201 description to be created * Added a sentence in the publisher section * added the word Publisher * - Cosmetic changes to make it clear that an entity will be replaced - Made If-Match property visible and mandatory as it is required - Tweaked readme to change update to replcace - Removed known issue which has been resolved with Microsoft * Added the raw request code * added the requested responses
- Loading branch information
Showing
4 changed files
with
216 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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 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,91 @@ | ||
public class Script : ScriptBase | ||
{ | ||
public override async Task<HttpResponseMessage> ExecuteAsync() | ||
{ | ||
if (Context?.OperationId == "RawRequest") | ||
{ | ||
return await HandleRawRequestAsync().ConfigureAwait(false); | ||
} | ||
|
||
return new HttpResponseMessage(HttpStatusCode.BadRequest) | ||
{ | ||
Content = CreateJsonContent($"Unknown operation ID '{Context?.OperationId}'") | ||
}; | ||
} | ||
|
||
private async Task<HttpResponseMessage> HandleRawRequestAsync() | ||
{ | ||
if (Context == null) | ||
{ | ||
return new HttpResponseMessage(HttpStatusCode.InternalServerError) | ||
{ | ||
Content = CreateJsonContent("Context not defined on request") | ||
}; | ||
} | ||
|
||
var request = Context.Request; | ||
if (request?.Content == null) | ||
{ | ||
return new HttpResponseMessage(HttpStatusCode.BadRequest) | ||
{ | ||
Content = CreateJsonContent("Content not defined on request") | ||
}; | ||
} | ||
var content = await request.Content.ReadAsStringAsync().ConfigureAwait(false); | ||
|
||
var obj = JObject.Parse(content); | ||
var body = GetValue<string>(obj, "body"); | ||
var verb = GetValue<string>(obj, "verb"); | ||
var url = $"{request.RequestUri}{GetValue<string>(obj, "service")}/{GetValue<string>(obj, "relativeUrl")}"; | ||
|
||
if (!Uri.TryCreate(url, UriKind.Absolute, out _)) | ||
{ | ||
return new HttpResponseMessage(HttpStatusCode.BadRequest) | ||
{ | ||
Content = CreateJsonContent($"Invalid Url Format '{url}'") | ||
}; | ||
} | ||
|
||
if (!string.IsNullOrEmpty(body)) | ||
{ | ||
request.Content = CreateJsonContent(body); | ||
} | ||
|
||
if (string.IsNullOrEmpty(verb)) | ||
{ | ||
return new HttpResponseMessage(HttpStatusCode.BadRequest) | ||
{ | ||
Content = CreateJsonContent("Verb must be provided") | ||
}; | ||
} | ||
|
||
request.Method = new HttpMethod(verb); | ||
request.RequestUri = new Uri(url); | ||
|
||
foreach (var token in GetValue<JArray>(obj, "headers") ?? new JArray()) | ||
{ | ||
var item = token.Value<JObject>(); | ||
|
||
var key = GetValue<string>(item, "key"); | ||
if (key == null) | ||
{ | ||
return new HttpResponseMessage(HttpStatusCode.BadRequest) | ||
{ | ||
Content = CreateJsonContent("Headers are invalid") | ||
}; | ||
} | ||
// Need to to add without validation because the etag causes it to fail | ||
request.Headers.TryAddWithoutValidation(key, GetValue<string>(item, "value")); | ||
} | ||
|
||
|
||
return await Context.SendAsync(request, CancellationToken).ConfigureAwait(continueOnCapturedContext: false); | ||
} | ||
|
||
private static T? GetValue<T>(JObject? obj, string key) where T : class | ||
{ | ||
return obj?.GetValue(key, StringComparison.InvariantCulture)?.Value<T>(); | ||
} | ||
|
||
|
||
} |
This file contains 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 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