2014-02-06 52 views
1

我利用Bing Ads API從bing上運行的在線廣告獲取廣告系列統計信息(點擊次數,展示次數等)。減慢取決於先前請求結果的API請求

要檢索這些統計信息,您需要通過報告服務提出2個API請求。你通過統計領域/日期範圍並返回一個ReportRequestId您傳遞的ReportRequestId到

  • A「PollGenerateReport」請求,並返回一個下載網址爲您請求的報告
    1. A「SubmitGenerateReport」請求

      我遇到的問題是,有時,在SubmitGenerateReport要求接受額外的第二或兩個這樣的頁面遇到錯誤「無效ReportRequestId」返回此ReportRequestId - 因爲它沒有一個呢。當我重新加載頁面時,它可以解決大部分時間(如果不是,則需要額外的重新加載或兩次加載)。

      我嘗試的第一個請求後添加一個睡眠命令(您將見下文),但它似乎並沒有解決問題。

      有沒有更好的選擇來處理這個問題? Bing API團隊沒有太多幫助。

      adwordscampaign_controller.rb

      #Start Bing Reporting Code - acquire report request ID number 
      client = Savon.client("https://adcenterapi.microsoft.com/Api/Advertiser/v8/Reporting/ReportingService.svc?wsdl") 
      @response = client.request :v8, :submit_generate_report, xmlns: "https://adcenter.microsoft.com/v8" do 
          soap.namespaces["xmlns:v8"] = "https://adcenter.microsoft.com/v8" 
          soap.namespaces["xmlns:i"] = "http://www.w3.org/2001/XMLSchema-instance" 
          soap.namespaces["xmlns:a1"] ="http://schemas.microsoft.com/2003/10/Serialization/Arrays" 
          soap.header = "<v8:UserName>#{binguser}</v8:UserName><v8:Password>#{bingpass}</v8:Password><v8:DeveloperToken>[removed]</v8:DeveloperToken>" 
          soap.body = "<ReportRequest i:nil=\"false\" i:type=\"CampaignPerformanceReportRequest\"><Format i:nil=\"false\">Xml</Format><Language i:nil=\"false\">English</Language>[shortened for the sake of reader sanity]</ReportRequest>" 
      end 
      
      #adding delay to compensate for slow turnaround by report generation 
      sleep 1.5 
      
      @responsehash = @response.to_hash 
      @responsehashdeep = @responsehash[:submit_generate_report_response][:report_request_id] 
      @report_req_id = @responsehashdeep.to_s 
      reportreq = @report_req_id 
      #report request ID acquired 
      
      #acquire Bing report download URL 
      reportclient = Savon.client("https://adcenterapi.microsoft.com/Api/Advertiser/v8/Reporting/ReportingService.svc?wsdl") 
      @reportresponse = reportclient.request :v8, :poll_generate_report do 
          soap.namespaces["xmlns:v8"] = "https://adcenter.microsoft.com/v8" 
          soap.header = "<v8:UserName>[removed]</v8:UserName><v8:Password>[removed]</v8:Password><v8:DeveloperToken>[removed]</v8:DeveloperToken><v8:CustomerId>[removed]</v8:CustomerId><v8:CustomerAccountId>[removed]</v8:CustomerAccountId>" 
          soap.body = "<v8:ReportRequestId>#{reportreq}</v8:ReportRequestId>" 
      end 
      @reportresponsehash = @reportresponse.to_hash 
      @reportresponsehashdeep = @reportresponsehash[:poll_generate_report_response][:report_request_status][:report_download_url] 
      reporturl = @reportresponsehashdeep.to_s 
      #report download URL acquired 
      

    回答

    1

    如果兵沒有一個API來檢查報告的狀態,你可以捕獲該異常,然後重試。這可能是在後一種情況下https://github.com/nfedyashev/retryable

    鑑於你在哪裏把睡眠呼我想你可能也困惑,到底是怎麼回事很有幫助。當請求返回時,響應始終存在,但當您進行第二次API調用時,報告尚未準備好。

    +0

    如果報告未準備好它會返回一個狀態=>「待定」,但我不知道如何重新運行該請求,沒有想到它,我要求一份新的報告,在頁面加載之前。我只是確保在第二次請求之前有睡眠呼叫,我明白,無論報告是否準備好,都會有某種反應。那寶石看起來非常有趣,謝謝你。你認爲這是處理這個問題的最好方法嗎? – macoughl

    +0

    http://msdn.microsoft.com/en-us/library/jj879320.aspx – bridiver

    +0

    我會在上面的鏈接中使用api調用進行輪詢。但是,通過等待報告生成,您可能會導致頁面加載時間過長。我會定期刷新頁面使用JavaScript或元刷新,直到報告準備好或使用ajax輪詢,直到它準備好。 – bridiver