Mercurial > gemma
comparison pkg/imports/erdms.go @ 2988:e1ccc8438529
DMV: separated fetching list of countries from database from requesting the RIS index. Also added some logging and better error handling.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Wed, 10 Apr 2019 11:00:23 +0200 |
parents | eb1d119f253f |
children | 6a44a89ffb51 |
comparison
equal
deleted
inserted
replaced
2987:524f1382558a | 2988:e1ccc8438529 |
---|---|
22 | 22 |
23 "gemma.intevation.de/gemma/pkg/soap" | 23 "gemma.intevation.de/gemma/pkg/soap" |
24 "gemma.intevation.de/gemma/pkg/soap/erdms" | 24 "gemma.intevation.de/gemma/pkg/soap/erdms" |
25 ) | 25 ) |
26 | 26 |
27 const selectUserCountriesSQL = `SELECT DISTINCT country FROM users.list_users` | 27 const ( |
28 selectUserCountriesSQL = ` | |
29 SELECT DISTINCT country FROM users.list_users | |
30 WHERE country <> '--' | |
31 ` | |
32 ) | |
33 | |
34 func userCountries(ctx context.Context, tx *sql.Tx) ([]string, error) { | |
35 rows, err := tx.QueryContext(ctx, selectUserCountriesSQL) | |
36 if err != nil { | |
37 return nil, err | |
38 } | |
39 defer rows.Close() | |
40 | |
41 var countries []string | |
42 | |
43 for rows.Next() { | |
44 var country string | |
45 if err = rows.Scan(&country); err != nil { | |
46 return nil, err | |
47 } | |
48 countries = append(countries, country) | |
49 } | |
50 | |
51 if err := rows.Err(); err != nil { | |
52 return nil, err | |
53 } | |
54 | |
55 return countries, nil | |
56 } | |
28 | 57 |
29 func getRisData( | 58 func getRisData( |
30 tx *sql.Tx, | 59 tx *sql.Tx, |
31 ctx context.Context, | 60 ctx context.Context, |
32 feedback Feedback, | 61 feedback Feedback, |
35 URL string, | 64 URL string, |
36 insecure bool, | 65 insecure bool, |
37 funcode string, | 66 funcode string, |
38 ) ([]*erdms.GetRisDataXMLResponse, error) { | 67 ) ([]*erdms.GetRisDataXMLResponse, error) { |
39 | 68 |
69 countries, err := userCountries(ctx, tx) | |
70 if err != nil { | |
71 return nil, err | |
72 } | |
73 | |
40 var auth *soap.BasicAuth | 74 var auth *soap.BasicAuth |
41 if username != "" { | 75 if username != "" { |
42 auth = &soap.BasicAuth{ | 76 auth = &soap.BasicAuth{ |
43 Login: username, | 77 Login: username, |
44 Password: password, | 78 Password: password, |
45 } | 79 } |
46 } | 80 } |
47 | 81 |
48 client := erdms.NewRefService(URL, insecure, auth) | 82 client := erdms.NewRefService(URL, insecure, auth) |
49 | 83 |
50 rows, err := tx.QueryContext(ctx, selectUserCountriesSQL) | 84 var responseData []*erdms.GetRisDataXMLResponse |
51 if err != nil { | 85 for _, country := range countries { |
52 return nil, err | |
53 } | |
54 defer rows.Close() | |
55 | 86 |
56 var country string | 87 feedback.Info("Request RIS index for country %s", country) |
57 var countries []string | |
58 var responseData []*erdms.GetRisDataXMLResponse | |
59 for rows.Next() { | |
60 err = rows.Scan(&country) | |
61 if err != nil { | |
62 return nil, err | |
63 } | |
64 countries = append(countries, country) | |
65 | 88 |
66 request := &erdms.GetRisDataXML{ | 89 request := &erdms.GetRisDataXML{ |
67 GetRisDataXMLType: &erdms.GetRisDataXMLType{ | 90 GetRisDataXMLType: &erdms.GetRisDataXMLType{ |
68 Subcode: erdms.NoNS{Text: country + "%"}, | 91 Subcode: erdms.NoNS{Text: country + "%"}, |
69 Funcode: erdms.NoNS{Text: funcode}, | 92 Funcode: erdms.NoNS{Text: funcode}, |
74 if err != nil { | 97 if err != nil { |
75 return nil, fmt.Errorf("Error requesting ERDMS service: %v", err) | 98 return nil, fmt.Errorf("Error requesting ERDMS service: %v", err) |
76 } | 99 } |
77 responseData = append(responseData, data) | 100 responseData = append(responseData, data) |
78 } | 101 } |
102 | |
79 feedback.Info("Import data for countries: %s.", | 103 feedback.Info("Import data for countries: %s.", |
80 strings.Join(countries, ", ")) | 104 strings.Join(countries, ", ")) |
105 | |
81 return responseData, nil | 106 return responseData, nil |
82 } | 107 } |