@@ -3,129 +3,120 @@ import axios from 'axios';
3
3
import Debug from 'debug' ;
4
4
import { formatDateTimeFiltersParameters } from './utilities.js' ;
5
5
const debug = Debug ( 'green-button-subscriber' ) ;
6
- let _configuration ;
7
- let _token ;
8
- export function setConfiguration ( configuration ) {
9
- _configuration = configuration ;
10
- }
11
- export function setUtilityApiConfiguration ( apiToken , baseUrl = 'https://utilityapi.com/' ) {
12
- setConfiguration ( {
13
- baseUrl,
14
- accessToken : apiToken
15
- } ) ;
16
- }
17
- async function getAccessToken ( ) {
18
- if ( _configuration . accessToken !== undefined ) {
19
- _token = {
20
- access_token : _configuration . accessToken ,
21
- expires_in : Number . POSITIVE_INFINITY
22
- } ;
23
- return ;
24
- }
25
- try {
26
- const authorizeUrl = `${ _configuration . baseUrl } oauth/authorize` ;
27
- debug ( `Authorize URL: ${ authorizeUrl } ` ) ;
28
- const response = await axios . post ( authorizeUrl , {
29
- response_type : 'code' ,
30
- grant_type : 'client_credentials' ,
31
- client_id : _configuration . clientId ,
32
- client_secret : _configuration . clientSecret
33
- } , {
34
- headers : {
35
- Referer : _configuration . baseUrl
36
- }
6
+ export class GreenButtonSubscriber {
7
+ _configuration ;
8
+ _token ;
9
+ constructor ( configuration ) {
10
+ if ( configuration !== undefined ) {
11
+ this . setConfiguration ( configuration ) ;
12
+ }
13
+ }
14
+ setConfiguration ( configuration ) {
15
+ this . _configuration = configuration ;
16
+ }
17
+ setUtilityApiConfiguration ( apiToken , baseUrl = 'https://utilityapi.com/' ) {
18
+ this . setConfiguration ( {
19
+ baseUrl,
20
+ accessToken : apiToken
37
21
} ) ;
38
- _token = response . data ;
39
- debug ( 'Access token obtained successfully.' ) ;
40
- debug ( 'Access Token:' , _token . access_token ) ;
41
22
}
42
- catch ( error ) {
43
- debug ( 'Error getting access token:' , error . response . data ) ;
23
+ async getAccessToken ( ) {
24
+ if ( this . _configuration . accessToken !== undefined ) {
25
+ this . _token = {
26
+ access_token : this . _configuration . accessToken ,
27
+ expires_in : Number . POSITIVE_INFINITY
28
+ } ;
29
+ return ;
30
+ }
31
+ try {
32
+ const authorizeUrl = `${ this . _configuration . baseUrl } oauth/authorize` ;
33
+ debug ( `Authorize URL: ${ authorizeUrl } ` ) ;
34
+ const response = await axios . post ( authorizeUrl , {
35
+ response_type : 'code' ,
36
+ grant_type : 'client_credentials' ,
37
+ client_id : this . _configuration . clientId ,
38
+ client_secret : this . _configuration . clientSecret
39
+ } , {
40
+ headers : {
41
+ Referer : this . _configuration . baseUrl
42
+ }
43
+ } ) ;
44
+ this . _token = response . data ;
45
+ debug ( 'Access token obtained successfully.' ) ;
46
+ debug ( 'Access Token:' , this . _token . access_token ) ;
47
+ }
48
+ catch ( error ) {
49
+ debug ( 'Error getting access token:' , error . response . data ) ;
50
+ }
44
51
}
45
- }
46
- export async function getEndpoint ( endpoint , getParameters = { } ) {
47
- if ( _token === undefined || Date . now ( ) >= _token . expires_in * 1000 ) {
48
- debug ( 'Token expired.' ) ;
49
- await getAccessToken ( ) ;
50
- }
51
- const headers = {
52
- Authorization : `Bearer ${ _token . access_token } `
53
- } ;
54
- const apiEndpoint = _configuration . baseUrl + endpoint ;
55
- debug ( `End Point: ${ apiEndpoint } ` ) ;
56
- const requestOptions = {
57
- headers
58
- } ;
59
- if ( getParameters !== undefined && Object . keys ( getParameters ) . length > 0 ) {
60
- requestOptions . params = getParameters ;
61
- }
62
- try {
63
- const response = await axios . get ( apiEndpoint , requestOptions ) ;
64
- return response . data ;
65
- }
66
- catch ( error ) {
67
- debug ( 'Error accessing API endpoint:' , error . response . data ) ;
68
- }
69
- return undefined ;
70
- }
71
- export async function getGreenButtonEndpoint ( greenButtonEndpoint , getParameters ) {
72
- const greenButtonXml = await getEndpoint ( `DataCustodian/espi/1_1/resource${ greenButtonEndpoint } ` , getParameters ) ;
73
- if ( greenButtonXml === undefined ) {
52
+ async getEndpoint ( endpoint , getParameters = { } ) {
53
+ if ( this . _token === undefined ||
54
+ Date . now ( ) >= this . _token . expires_in * 1000 ) {
55
+ debug ( 'Token expired.' ) ;
56
+ await this . getAccessToken ( ) ;
57
+ }
58
+ const headers = {
59
+ Authorization : `Bearer ${ this . _token . access_token } `
60
+ } ;
61
+ const apiEndpoint = this . _configuration . baseUrl + endpoint ;
62
+ debug ( `End Point: ${ apiEndpoint } ` ) ;
63
+ const requestOptions = {
64
+ headers
65
+ } ;
66
+ if ( getParameters !== undefined && Object . keys ( getParameters ) . length > 0 ) {
67
+ requestOptions . params = getParameters ;
68
+ }
69
+ try {
70
+ const response = await axios . get ( apiEndpoint , requestOptions ) ;
71
+ return response . data ;
72
+ }
73
+ catch ( error ) {
74
+ debug ( 'Error accessing API endpoint:' , error . response . data ) ;
75
+ }
74
76
return undefined ;
75
77
}
76
- return await atomToGreenButtonJson ( greenButtonXml ) ;
77
- }
78
- export async function getAuthorizations ( ) {
79
- return await getGreenButtonEndpoint ( '/Authorization' ) ;
80
- }
81
- export async function getAuthorization ( authorizationId ) {
82
- return await getGreenButtonEndpoint ( `/Authorization/${ authorizationId } ` ) ;
83
- }
84
- export async function getUsagePoints ( authorizationId ) {
85
- return await getGreenButtonEndpoint ( `/Subscription/${ authorizationId } /UsagePoint` ) ;
86
- }
87
- export async function getMeterReadings ( authorizationId , meterId ) {
88
- return await getGreenButtonEndpoint ( `/Subscription/${ authorizationId } /UsagePoint/${ meterId } /MeterReading` ) ;
89
- }
90
- export async function getIntervalBlocks ( authorizationId , meterId , readingId ) {
91
- return await getGreenButtonEndpoint ( `/Subscription/${ authorizationId } /UsagePoint/${ meterId } /MeterReading/${ readingId } /IntervalBlock` ) ;
92
- }
93
- export async function getUsageSummaries ( authorizationId , meterId ) {
94
- return await getGreenButtonEndpoint ( `/Subscription/${ authorizationId } /UsagePoint/${ meterId } /UsageSummary` ) ;
95
- }
96
- export async function getElectricPowerQualitySummaries ( authorizationId , meterId ) {
97
- return await getGreenButtonEndpoint ( `/Subscription/${ authorizationId } /UsagePoint/${ meterId } /ElectricPowerQualitySummary` ) ;
98
- }
99
- export async function getCustomers ( authorizationId ) {
100
- return await getGreenButtonEndpoint ( `/RetailCustomer/${ authorizationId } /Customer` ) ;
101
- }
102
- export async function getCustomerAccounts ( authorizationId , customerId ) {
103
- return await getGreenButtonEndpoint ( `/RetailCustomer/${ authorizationId } /Customer/${ customerId } /CustomerAccount` ) ;
104
- }
105
- export async function getCustomerAgreements ( authorizationId , customerId , customerAccountId ) {
106
- return await getGreenButtonEndpoint ( `/RetailCustomer/${ authorizationId } /Customer/${ customerId } /CustomerAccount/${ customerAccountId } /CustomerAgreement` ) ;
107
- }
108
- export async function getBatchSubscriptionsByAuthorization ( authorizationId , dateTimeFilters ) {
109
- return await getGreenButtonEndpoint ( `/Batch/Subscription/${ authorizationId } ` , formatDateTimeFiltersParameters ( dateTimeFilters ) ) ;
110
- }
111
- export async function getBatchSubscriptionsByMeter ( authorizationId , meterId , dateTimeFilters ) {
112
- return await getGreenButtonEndpoint ( `/Batch/Subscription/${ authorizationId } /UsagePoint/${ meterId } ` , formatDateTimeFiltersParameters ( dateTimeFilters ) ) ;
78
+ async getGreenButtonEndpoint ( greenButtonEndpoint , getParameters ) {
79
+ const greenButtonXml = await this . getEndpoint ( `DataCustodian/espi/1_1/resource${ greenButtonEndpoint } ` , getParameters ) ;
80
+ if ( greenButtonXml === undefined ) {
81
+ return undefined ;
82
+ }
83
+ return await atomToGreenButtonJson ( greenButtonXml ) ;
84
+ }
85
+ async getAuthorizations ( ) {
86
+ return await this . getGreenButtonEndpoint ( '/Authorization' ) ;
87
+ }
88
+ async getAuthorization ( authorizationId ) {
89
+ return await this . getGreenButtonEndpoint ( `/Authorization/${ authorizationId } ` ) ;
90
+ }
91
+ async getUsagePoints ( authorizationId ) {
92
+ return await this . getGreenButtonEndpoint ( `/Subscription/${ authorizationId } /UsagePoint` ) ;
93
+ }
94
+ async getMeterReadings ( authorizationId , meterId ) {
95
+ return await this . getGreenButtonEndpoint ( `/Subscription/${ authorizationId } /UsagePoint/${ meterId } /MeterReading` ) ;
96
+ }
97
+ async getIntervalBlocks ( authorizationId , meterId , readingId ) {
98
+ return await this . getGreenButtonEndpoint ( `/Subscription/${ authorizationId } /UsagePoint/${ meterId } /MeterReading/${ readingId } /IntervalBlock` ) ;
99
+ }
100
+ async getUsageSummaries ( authorizationId , meterId ) {
101
+ return await this . getGreenButtonEndpoint ( `/Subscription/${ authorizationId } /UsagePoint/${ meterId } /UsageSummary` ) ;
102
+ }
103
+ async getElectricPowerQualitySummaries ( authorizationId , meterId ) {
104
+ return await this . getGreenButtonEndpoint ( `/Subscription/${ authorizationId } /UsagePoint/${ meterId } /ElectricPowerQualitySummary` ) ;
105
+ }
106
+ async getCustomers ( authorizationId ) {
107
+ return await this . getGreenButtonEndpoint ( `/RetailCustomer/${ authorizationId } /Customer` ) ;
108
+ }
109
+ async getCustomerAccounts ( authorizationId , customerId ) {
110
+ return await this . getGreenButtonEndpoint ( `/RetailCustomer/${ authorizationId } /Customer/${ customerId } /CustomerAccount` ) ;
111
+ }
112
+ async getCustomerAgreements ( authorizationId , customerId , customerAccountId ) {
113
+ return await this . getGreenButtonEndpoint ( `/RetailCustomer/${ authorizationId } /Customer/${ customerId } /CustomerAccount/${ customerAccountId } /CustomerAgreement` ) ;
114
+ }
115
+ async getBatchSubscriptionsByAuthorization ( authorizationId , dateTimeFilters ) {
116
+ return await this . getGreenButtonEndpoint ( `/Batch/Subscription/${ authorizationId } ` , formatDateTimeFiltersParameters ( dateTimeFilters ) ) ;
117
+ }
118
+ async getBatchSubscriptionsByMeter ( authorizationId , meterId , dateTimeFilters ) {
119
+ return await this . getGreenButtonEndpoint ( `/Batch/Subscription/${ authorizationId } /UsagePoint/${ meterId } ` , formatDateTimeFiltersParameters ( dateTimeFilters ) ) ;
120
+ }
113
121
}
114
- export default {
115
- setConfiguration,
116
- setUtilityApiConfiguration,
117
- getEndpoint,
118
- getGreenButtonEndpoint,
119
- getAuthorizations,
120
- getAuthorization,
121
- getUsagePoints,
122
- getMeterReadings,
123
- getIntervalBlocks,
124
- getUsageSummaries,
125
- getElectricPowerQualitySummaries,
126
- getCustomers,
127
- getCustomerAccounts,
128
- getCustomerAgreements,
129
- getBatchSubscriptionsByAuthorization,
130
- getBatchSubscriptionsByMeter
131
- } ;
122
+ export { helpers } from '@cityssm/green-button-parser' ;
0 commit comments