- Home /
Making an interface for web requests
I have some web requests that are working correctly, but I'm trying to extract them out into an interface to tidy up my code.
I'm having trouble with the return statements for these calls, because the async request needs a return and it doesn't match the type I'm passing back. I'm 90% sure this problem stems from my lack of understanding of asynchronous operations, but I'm hoping someone will be able to help me out.
public interface IWebRequest
{
IEnumerable<WebResult> Register(WWWForm form);
}
public class WebResult
{
public long ResponseCode { get; set; }
public bool IsError { get; set; }
public string Value { get; set; }
}
I've defined an interface IWebRequest
and a return object WebResult
.
When I try to implement these functions, I get an error due to the yield return www.SendWebRequest()
call not being a type of IEnumerable<WebResult>
public class WebRequestSender : IWebRequest
{
public IEnumerable<WebResult> Register(WWWForm form)
{
using (UnityWebRequest www = UnityWebRequest.Post("REGISTER_URL", form))
{
www.certificateHandler = new SelfSignedCertificateHandler();
yield return www.SendWebRequest();
if (www.isHttpError || www.isNetworkError)
{
yield return new WebResult
{
IsError = true,
ResponseCode = www.responseCode,
Value = null
};
}
else
{
yield return new WebResult
{
IsError = false,
ResponseCode = www.responseCode,
Value = www.downloadHandler.text
};
}
}
}
}
Does anyone know the proper way to code this so I can have my return type object and still make the asynchronous call?
Thank you for any help.