0

This is the code from the service i generated.Everything work fine. I get the list of photos, but i want to get the status_code and make logic in the ViewModel and show message to the user.

 public async Task<IList<Photo>> GetAllPosts()
        {
            try
            {
                if (CrossConnectivity.Current.IsConnected)
                {
                    instagramCloneClient.DefaultRequestHeaders.Clear();
                    instagramCloneClient.DefaultRequestHeaders.Add("Accept", "application/json");
                    var response = await instagramCloneClient.GetAsync($"/photos");
                    var status_code=(int)response.StatusCode;
                    if (response.IsSuccessStatusCode)
                    {
                        string jsonMessage;
                        using (Stream responseStream = await response.Content.ReadAsStreamAsync())
                        {
                            jsonMessage = new StreamReader(responseStream).ReadToEnd();
                        }

                        IList<Photo> photos = JsonConvert.DeserializeObject<IList<Photo>>(jsonMessage);
                        return photos;
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {

                    return null;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                string error = ex.Message;
                return null;
            }
        } 

I return list of photos. I also want response.StatusCode to the ViewModel from which i call the function.I must return IList, how to include some int status_code? What is the best practice?

3
  • generally, you would return a wrapper object that contains both status info and the data, OR if there is a failure you can throw an exception instead of returning the data Commented Feb 21, 2021 at 20:39
  • @Jason u mean like Object witch contain IList<Photos> and int status_code? Commented Feb 21, 2021 at 20:59
  • 1
    yes, a custom class that contains whatever set of data and status info you want to return to the caller Commented Feb 21, 2021 at 21:00

1 Answer 1

1

You have multiple options. You can create a new model with your List and an integer/enum for the StatusCode. Another option is to return a List of ArrayList.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.