public static IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, TraceWriter log) { log.Info("C# HTTP trigger function processed a request."); try { // {"issue":{"project_id":165,"subject":"Test Ticket","tracker_id":2,"priority_id":4,"assigned_to_id":127,"status_id":1,"description":"My Test Description"}} //var (wrapper, issue) = CreateTestData(); //return new OkObjectResult(wrapper.SerializeToJson()); var wrapper = new RedmineIssueWrapper(req); var ret = CreateIssue(wrapper); if (ret.IsCompleted && ret.Result.Success) { var content = ret.Result.Content; log.Info(content); return(new OkObjectResult(content)); // also add the predicted URL of the new issue } else { return(new BadRequestObjectResult(ret.Result.Content)); } } catch (Exception ex) { return(new BadRequestObjectResult(ex.Message)); } }
private static (RedmineIssueWrapper wrapper, DtoIssue issue) CreateTestData() { //for testing var issue = CreateTestRedmineIssue(); var wrapper = new RedmineIssueWrapper() { issue = issue }; var i = CreateTestDtoIssue(); return(wrapper : wrapper, issue : i); }
private static async Task <(bool Success, string Content)> CreateIssue(RedmineIssueWrapper issue) { //var issuesEndPoint = "issues.json"; // Read values from config // https://docs.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings // https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-csharp#environment-variables var config = new CreateRedmineIssueConfiguration(); var headers = new List <KeyValuePair <string, string> >(new KeyValuePair <string, string>[] { new KeyValuePair <string, string>(config.RedmineApiTokenHeaderName, config.RedmineApiTokenValue) }); HttpClientInstance.Init(config.RedmineBaseUrl, headers); var _client = HttpClientInstance.Instance; var resp = await _client.PostAsync(config.RedmineIssuesEndPoint, new StringContent(issue.SerializeToJson(), System.Text.Encoding.UTF8, "application/json")); var success = resp.IsSuccessStatusCode; string ret; if (success) { ret = await resp.Content.ReadAsStringAsync(); } else { ret = resp.ReasonPhrase + ". "; var content = await resp.Content.ReadAsStringAsync(); if (!String.IsNullOrWhiteSpace(content)) { try { dynamic json = JObject.Parse(content); ret += json?.errors[0]; } catch { } } } return(Success : success, Content : ret); }