如何以字符串形式获取SendGrid响应状态?

时间:2019-07-04 12:31:50

标签: c# asynchronous sendgrid

我在从SendGrid检索HTTP响应代码,然后基于该响应更新标签时遇到问题。由于SendGrid调用使用异步方法,因此我无法返回response.statuscode;

这是我的代码:

def parse(self, response):
    propertes = response.xpath("//div[@class='card__main']")
    for prop in propertes:
        property_url = prop.xpath(
            "./div[@class='card__body']/h3[@class='card__title']/a/@href").extract_first()
        title = prop.xpath(
            "./div[@class='card__body']/h3[@class='card__title']/a/text()").extract_first()
        price = prop.xpath(
            "./div[@class='card__body']/div[@class='card__footer card__footer--primary']/div[@class='card__price']/text()").extract_first()
        description = prop.xpath(
            "./div[@class='card__body']/div[@class='card__synopsis']/p/text()").extract_first()
        bedrooms = prop.xpath(
            "./div[@class='card__body']/div[@class='card__footer card__footer--primary']/div[@class='features features--inline']/ol[@class ='features__list']/li[@class ='features__item'][1]/div[@class='features__label']/text()").extract_first()

        yield scrapy.Request(
            url=property_url,
            callback=self.parse_property,
            meta={
                'title': title,
                'price': price,
                'description': description,
                'bedrooms': bedrooms,
            }
        )

def parse_property(self, response):

    title = response.meta["title"]
    price = response.meta["price"]
    description = response.meta["description"]
    bedrooms = response.meta["bedrooms"]

    images = response.xpath('//a[contains(@class, "gallery__link ")]/@href').getall()

    yield {'title': title, 'price':price, "description": description, 'bedrooms': bedrooms, 'bathrooms': bathrooms, 'garages': garages, 'images':images}

2 个答案:

答案 0 :(得分:1)

您的方法不允许返回值,因为您将其声明为async Task。这意味着它是一个async方法,什么也不返回(它只是返回一个Task,因此调用方知道何时完成,但没有实际值)。

如果要通过async方法返回内容,则需要使用Task<T>返回类型,其中T是要返回的值的类型。

所以在这种情况下,应该是:

async Task<HttpStatusCode> Execute(String EmailId, String Message)

然后您可以return response.StatusCode

这里有一些其他的读物,可以帮助您更好地理解async代码:

答案 1 :(得分:0)

要从异步方法返回值,必须等待它。请参见下面的示例方法调用:

private async Task<System.Net.HttpStatusCode> Execute(String EmailId, String Message)
        {
            var apiKey = "abcdefghijklmnopqrstuvwxyz1234567890";
            var client = new SendGridClient(apiKey);
            var from = new EmailAddress("myemail@gmail.com", "Sender");
            var subject = "Testing Email";
            var to = new EmailAddress(EmailId, "Reciever");
            var plainTextContent = "You have recieved this message from me";
            var htmlContent = Message + "<br><i>-Message sent by me</i>";
            var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
            var response = await client.SendEmailAsync(msg).ConfigureAwait(false);
            return response.StatusCode;

        }

private async Task GetStatusCodeExample(String EmailId, String Message)
{
    var statusCode = await Execute(EmailId, Message);
}

编辑:

更新您的代码以使用以下内容以及上面更新的Execute方法:

protected void BtnSend_Click(object sender, EventArgs e)
{
     lblmsg.InnerText = SendMail(txtEmailId.Text.ToString(), 
     txtMessage.Text.ToString());  //------------


private String SendMail(String EmailId, String Message)
{
     var task = Execute(EmailId, Message);
     task.Wait();
     return ((int)task.Result).ToString();  
}