-1

I use code to get data from Atlassian Jira and put the data from the response into a container, how can I make this method asynchronous? I have tried various methods with webrequest, but they all point to an error in the code, perhaps I do not understand how it works at all. You can suggest documentation or specify in the code what I can do with asynchrony for such a request.

 public IEnumerable<JiraDataModel> GetData(string dateFrom, string dateTo)
        {
            int allTicketsCount = 0;
            int devTicketsCount = 0;
            int slaTicketsCount = 0;

            List<JiraRequestUrl> urlArray = new List<JiraRequestUrl>();
urlArray.AddRange(new List<JiraRequestUrl>

        {
            new JiraRequestUrl{type = "all", //*data*//},
            new JiraRequestUrl(){type = "dev",//*data*//});

            try
        {
            foreach (JiraRequestUrl u in urlArray)
            {
                WebRequest request = WebRequest.Create(_jiraUrl + u.url);
                request.ContentType = "application/json; charset=utf-8";
                request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(_credentials)));
                request.Headers.Add("maxResults", "100");

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
                    JiraGetDataModel.RootObject DeserializedResponse = (JiraGetDataModel.RootObject)JsonConvert.DeserializeObject(responseString, typeof(JiraGetDataModel.RootObject));

                    if (u.type.Equals("all"))
                    {
                        allTicketsCount = DeserializedResponse.total;
                    }
                    if (u.type.Equals("dev"))
                    {
                        devTicketsCount = DeserializedResponse.total;
                    }
                }
            }
just
  • 1
  • 5
  • 1
    Can you share the full code of the action method? Also what is the error that you are getting? Share the error message too. – Rahatur Feb 26 '20 at 08:26
  • @ Rahatur, hi, added ful code. I didn't save any errors with async, because I tried to use the methods incorrectly, and returned the code to its original state – just Feb 26 '20 at 08:48
  • If you do not state what problem you are facing and at which line then the community members are not going to find the error. The question would be flagged as of poor quality and closed off. Since you are new, to get help I suggest put as much information as possible. See the question guideline here: https://stackoverflow.com/help/how-to-ask – Rahatur Feb 26 '20 at 08:51
  • if you don't show us what you tried and what the error was, we don't stand much chance of helping you. We cannot fix invisible problems with invisible code. And anyway what exactly do you want to make asynchronous? The HTTP request I assume? And/or something else? It isn't fully clear. – ADyson Feb 26 '20 at 08:51
  • P.S. Assuming you want to use webrequest asynchronously, have you looked at other existing material online? e.g. https://stackoverflow.com/a/49225438/5947043 for example. – ADyson Feb 26 '20 at 08:53
  • Has the problem resolved? – Md Farid Uddin Kiron Feb 26 '20 at 10:48
  • @Md Farid Uddin Kironprivate, hi, no, I tried using the method for example "async Task MakeRequestAsync", but didn't quite understand how I could integrate it into my code – just Feb 26 '20 at 11:02
  • if you have a problem with your code, you need to show what you tried. Otherwise we cannot know exactly what you did wrong, and cannot help you fix it. We have already made suggestions, but if they are not helping you, we need to see your version so we can understand exactly what the issue is. – ADyson Feb 27 '20 at 12:58

1 Answers1

0

sorry, I didn't quite understand that I didn't insert my attempts to use async, I did it, and it seems to work, can I improve something in my code? added 2 lines

  public async Task<IEnumerable<JiraDataModel>> GetData(string dateFrom, string dateTo)
        {
            int allTicketsCount = 0;
            int devTicketsCount = 0;
            int slaTicketsCount = 0;

            List<JiraRequestUrl> urlArray = new List<JiraRequestUrl>();

            urlArray.AddRange(new List<JiraRequestUrl>
        {
            new JiraRequestUrl{type = "all", //*data*//},
            new JiraRequestUrl(){type = "dev",//*data*//});

            try
        {
            foreach (JiraRequestUrl u in urlArray)
            {
                WebRequest request = WebRequest.Create(_jiraUrl + u.url);
                request.ContentType = "application/json; charset=utf-8";
                request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(_credentials)));
                request.Headers.Add("maxResults", "100");

                HttpWebResponse response = (HttpWebResponse)(await request.GetResponseAsync()request.GetResponse();

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
                    JiraGetDataModel.RootObject DeserializedResponse = (JiraGetDataModel.RootObject)JsonConvert.DeserializeObject(responseString, typeof(JiraGetDataModel.RootObject));

                    if (u.type.Equals("all"))
                    {
                        allTicketsCount = DeserializedResponse.total;
                    }
                    if (u.type.Equals("dev"))
                    {
                        devTicketsCount = DeserializedResponse.total;
                    }
                }
            }
just
  • 1
  • 5