@@ -56,8 +56,6 @@ func DownloadPublicationMarketData(ctx context.Context, securityToken string, ur
5656 opts := & DownloadOptions {
5757 UserAgent : client .userAgent ,
5858 }
59- ctx , cancel := context .WithTimeout (ctx , 30 * time .Second )
60- defer cancel ()
6159
6260 // Retrieve today's market data – use cache when available.
6361 todayKey := now .Format ("2006-01-02" )
@@ -74,7 +72,7 @@ func DownloadPublicationMarketData(ctx context.Context, securityToken string, ur
7472 fmt .Println (url )
7573 var err error
7674 var rawXML []byte
77- marketDocument , rawXML , err = DownloadPublicationMarketDataWithRaw (ctx , url , opts )
75+ marketDocument , rawXML , err = downloadWithTimeout (ctx , url , opts )
7876 if err != nil {
7977 return nil , err
8078 }
@@ -99,11 +97,18 @@ func DownloadPublicationMarketData(ctx context.Context, securityToken string, ur
9997 urlNextDay := buildPublicationMarketDataURL (securityToken , urlFormat , tomorrow )
10098 var err error
10199 var rawXMLNextDay []byte
102- marketDocumentNextDay , rawXMLNextDay , err = DownloadPublicationMarketDataWithRaw (ctx , urlNextDay , opts )
100+ marketDocumentNextDay , rawXMLNextDay , err = downloadWithTimeout (ctx , urlNextDay , opts )
103101 if err != nil {
104- return nil , err
105- }
106- if cache != nil {
102+ if IsNoMatchingData (err ) {
103+ // Day-ahead prices for tomorrow are not published yet. This is expected
104+ // before the market coupling results are released, so continue with
105+ // today's data only instead of failing the whole forecast.
106+ fmt .Printf ("No market data published yet for %s: %v\n " , tomorrowKey , err )
107+ marketDocumentNextDay = nil
108+ } else {
109+ return nil , err
110+ }
111+ } else if cache != nil {
107112 cache .StoreDocumentWithRaw (tomorrowKey , marketDocumentNextDay , rawXMLNextDay , CacheSourceDownload )
108113 }
109114 }
@@ -116,6 +121,19 @@ func DownloadPublicationMarketData(ctx context.Context, securityToken string, ur
116121 return marketDocument , nil
117122}
118123
124+ // requestTimeout bounds a single ENTSO-E API call. The Transparency Platform can be
125+ // slow, so the budget is applied per request instead of once for the whole
126+ // today + tomorrow sequence.
127+ const requestTimeout = 30 * time .Second
128+
129+ // downloadWithTimeout performs a single download bounded by requestTimeout.
130+ func downloadWithTimeout (ctx context.Context , apiURL string , opts * DownloadOptions ) (* PublicationMarketData , []byte , error ) {
131+ reqCtx , cancel := context .WithTimeout (ctx , requestTimeout )
132+ defer cancel ()
133+
134+ return DownloadPublicationMarketDataWithRaw (reqCtx , apiURL , opts )
135+ }
136+
119137// buildPublicationMarketDataURL extracts the URL assignment logic for DownloadPublicationMarketData.
120138func buildPublicationMarketDataURL (securityToken string , urlFormat string , now time.Time ) string {
121139 start := time .Date (now .Year (), now .Month (), now .Day (), 0 , 0 , 0 , 0 , now .Location ())
0 commit comments