22package entsoe
33
44import (
5+ "bytes"
56 "context"
67 "fmt"
8+ "io"
79 "net/http"
810 "time"
911
@@ -51,6 +53,9 @@ func DownloadPublicationMarketData(ctx context.Context, securityToken string, ur
5153
5254 now := time .Now ().In (location )
5355 client := NewAPIClient ()
56+ opts := & DownloadOptions {
57+ UserAgent : client .userAgent ,
58+ }
5459 ctx , cancel := context .WithTimeout (ctx , 30 * time .Second )
5560 defer cancel ()
5661
@@ -68,40 +73,42 @@ func DownloadPublicationMarketData(ctx context.Context, securityToken string, ur
6873 url := buildPublicationMarketDataURL (securityToken , urlFormat , now )
6974 fmt .Println (url )
7075 var err error
71- marketDocument , err = client .DownloadPublicationMarketData (ctx , url )
76+ var rawXML []byte
77+ marketDocument , rawXML , err = DownloadPublicationMarketDataWithRaw (ctx , url , opts )
7278 if err != nil {
7379 return nil , err
7480 }
7581 if cache != nil {
76- cache .StoreDocument (todayKey , marketDocument , CacheSourceDownload )
82+ cache .StoreDocumentWithRaw (todayKey , marketDocument , rawXML , CacheSourceDownload )
7783 }
7884 }
7985
80- // If instructed, also retrieve data for the next day.
81- if fetchNextDay {
82- tomorrow := now .AddDate (0 , 0 , 1 )
83- tomorrowKey := tomorrow .Format ("2006-01-02" )
86+ // Retrieve data for the next day – use cache when available, or fetch if fetchNextDay is true.
87+ tomorrow := now .AddDate (0 , 0 , 1 )
88+ tomorrowKey := tomorrow .Format ("2006-01-02" )
8489
85- var marketDocumentNextDay * PublicationMarketData
86- if cache != nil {
87- if cached , ok := cache .Get (tomorrowKey ); ok {
88- fmt .Printf ("Using cached market data for %s\n " , tomorrowKey )
89- marketDocumentNextDay = cached
90- }
90+ var marketDocumentNextDay * PublicationMarketData
91+ if cache != nil {
92+ if cached , ok := cache .Get (tomorrowKey ); ok {
93+ fmt .Printf ("Using cached market data for %s\n " , tomorrowKey )
94+ marketDocumentNextDay = cached
9195 }
96+ }
9297
93- if marketDocumentNextDay == nil {
94- urlNextDay := buildPublicationMarketDataURL (securityToken , urlFormat , tomorrow )
95- var err error
96- marketDocumentNextDay , err = client .DownloadPublicationMarketData (ctx , urlNextDay )
97- if err != nil {
98- return nil , err
99- }
100- if cache != nil {
101- cache .StoreDocument (tomorrowKey , marketDocumentNextDay , CacheSourceDownload )
102- }
98+ if marketDocumentNextDay == nil && fetchNextDay {
99+ urlNextDay := buildPublicationMarketDataURL (securityToken , urlFormat , tomorrow )
100+ var err error
101+ var rawXMLNextDay []byte
102+ marketDocumentNextDay , rawXMLNextDay , err = DownloadPublicationMarketDataWithRaw (ctx , urlNextDay , opts )
103+ if err != nil {
104+ return nil , err
103105 }
106+ if cache != nil {
107+ cache .StoreDocumentWithRaw (tomorrowKey , marketDocumentNextDay , rawXMLNextDay , CacheSourceDownload )
108+ }
109+ }
104110
111+ if marketDocumentNextDay != nil {
105112 // Merge the data from both days
106113 marketDocument = mergePublicationMarketData (marketDocument , marketDocumentNextDay )
107114 }
@@ -144,50 +151,63 @@ func mergePublicationMarketData(first *PublicationMarketData, second *Publicatio
144151
145152// DownloadPublicationMarketDataWithOptions downloads and decodes a PublicationMarketData with custom options
146153func DownloadPublicationMarketDataWithOptions (ctx context.Context , apiURL string , opts * DownloadOptions ) (* PublicationMarketData , error ) {
154+ doc , _ , err := DownloadPublicationMarketDataWithRaw (ctx , apiURL , opts )
155+ return doc , err
156+ }
157+
158+ // DownloadPublicationMarketDataWithRaw downloads and decodes a PublicationMarketData along with its raw XML bytes
159+ func DownloadPublicationMarketDataWithRaw (ctx context.Context , apiURL string , opts * DownloadOptions ) (* PublicationMarketData , []byte , error ) {
147160 if apiURL == "" {
148- return nil , fmt .Errorf ("API URL cannot be empty" )
161+ return nil , nil , fmt .Errorf ("API URL cannot be empty" )
149162 }
150163
151164 client := & http.Client {}
152165
153166 // Create HTTP request with context
154167 req , err := http .NewRequestWithContext (ctx , http .MethodGet , apiURL , nil )
155168 if err != nil {
156- return nil , fmt .Errorf ("failed to create HTTP request: %w" , err )
169+ return nil , nil , fmt .Errorf ("failed to create HTTP request: %w" , err )
157170 }
158171
159172 // Set default headers
160173 userAgent := "entsoe-go-client/1.0"
161- if opts .UserAgent != "" {
174+ if opts != nil && opts .UserAgent != "" {
162175 userAgent = opts .UserAgent
163176 }
164177 req .Header .Set ("User-Agent" , userAgent )
165178 req .Header .Set ("Accept" , "application/xml, text/xml" )
166179
167180 // Set custom headers
168- for key , value := range opts .Headers {
169- req .Header .Set (key , value )
181+ if opts != nil {
182+ for key , value := range opts .Headers {
183+ req .Header .Set (key , value )
184+ }
170185 }
171186
172187 // Execute the request
173188 resp , err := client .Do (req )
174189 if err != nil {
175- return nil , fmt .Errorf ("failed to execute HTTP request: %w" , err )
190+ return nil , nil , fmt .Errorf ("failed to execute HTTP request: %w" , err )
176191 }
177192 defer resp .Body .Close ()
178193
179194 // Check HTTP status code
180195 if resp .StatusCode != http .StatusOK {
181- return nil , fmt .Errorf ("HTTP request failed with status %d: %s" , resp .StatusCode , resp .Status )
196+ return nil , nil , fmt .Errorf ("HTTP request failed with status %d: %s" , resp .StatusCode , resp .Status )
197+ }
198+
199+ rawBytes , err := io .ReadAll (resp .Body )
200+ if err != nil {
201+ return nil , nil , fmt .Errorf ("failed to read response body: %w" , err )
182202 }
183203
184204 // Decode the XML response using the existing decoder
185- doc , err := DecodeEnergyPricesXML (resp . Body )
205+ doc , err := DecodeEnergyPricesXML (bytes . NewReader ( rawBytes ) )
186206 if err != nil {
187- return nil , fmt .Errorf ("failed to decode XML response: %w" , err )
207+ return nil , nil , fmt .Errorf ("failed to decode XML response: %w" , err )
188208 }
189209
190- return doc , nil
210+ return doc , rawBytes , nil
191211}
192212
193213// ValidateAPIURL performs basic validation on the API URL
0 commit comments