Skip to content

Commit

Permalink
Add new webhook types
Browse files Browse the repository at this point in the history
  • Loading branch information
suda committed Aug 23, 2023
1 parent 2faf5e5 commit e1c9a76
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
19 changes: 19 additions & 0 deletions fixtures/CdrCreated.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"specversion": "1.0",
"id": "319fcd4c-df4f-43a2-accf-7d025dd94aa8",
"type": "CdrCreated",
"subject": "FRGRN4A7BA75EC4AC418596F4E1124AE30C9",
"time": "2023-08-23T12:37:53.2104009Z",
"source": "https://beta.api.longship.io/v1/cdrs",
"datacontenttype": "application/json",
"data": {
"chargepointid": "ESTG_00669",
"locationid": "ES*ESTG_00669",
"evseid": "FR*ESTG_00669*1",
"connectornumber": 1,
"totalenergyinkwh": 1.2,
"totalduration": "03:59:00.0000069",
"totalcosts": 0.3,
"transactionid": "1605381954"
}
}
8 changes: 8 additions & 0 deletions fixtures/LocationCreated.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"specversion": "1.0",
"id": "3ecc2a8e-c90a-4a2f-9900-8a2c4bfbbef5",
"type": "LocationCreated",
"subject": "ES*ESTG_00669",
"time": "2023-08-23T12:36:01.7311121Z",
"source": "https://beta.api.longship.io/v1/locations"
}
8 changes: 8 additions & 0 deletions fixtures/LocationUpdated.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"specversion": "1.0",
"id": "e9846adf-e553-41d4-a972-249a0d567e2f",
"type": "LocationUpdated",
"subject": "ES*ESTG_00669",
"time": "2023-08-23T12:30:15.654638Z",
"source": "https://beta.api.longship.io/v1/locations"
}
49 changes: 48 additions & 1 deletion longship/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class WebhookPayloadType(str, Enum):
SessionStart = "SessionStart"
SessionUpdate = "SessionUpdate"
SessionStop = "SessionStop"
CDRCreated = "CdrCreated"
LocationCreated = "LocationCreated"
LocationUpdated = "LocationUpdated"
MSPInvoiceProposalStatus = "MspInvoiceProposalStatus"
Ping = "Ping"

def __str__(self) -> str:
return str(self.value)
Expand Down Expand Up @@ -72,6 +77,33 @@ class SessionStopData(SessionUpdateData):
pass


@attr.s(auto_attribs=True)
class CDRCreatedData:
chargepointid: str
locationid: Optional[str] = attr.ib(default=None)
evseid: Optional[str] = attr.ib(default=None)
connectornumber: int
totalenergyinkwh: float
totalduration: str
totalcosts: float
transactionid: str

@attr.s(auto_attribs=True)
class LocationCreatedData:
pass

@attr.s(auto_attribs=True)
class LocationUpdatedData:
pass

@attr.s(auto_attribs=True)
class MSPInvoiceProposalStatusData:
pass

@attr.s(auto_attribs=True)
class PingData:
pass

@attr.s(auto_attribs=True)
class WebhookPayload:
specversion: str
Expand All @@ -88,6 +120,11 @@ class WebhookPayload:
SessionStartData,
SessionUpdateData,
SessionStopData,
CDRCreatedData,
LocationCreatedData,
LocationUpdatedData,
MSPInvoiceProposalStatusData,
PingData,
]

def __attrs_post_init__(self):
Expand All @@ -96,11 +133,21 @@ def __attrs_post_init__(self):
elif self.type == WebhookPayloadType.OperationalStatusChanged:
self.data = OperationalStatusChangedData(**self.data)
elif self.type == WebhookPayloadType.ConnectivityStatusChanged:
self.data['status'] = self.data['status'].upper()
self.data["status"] = self.data["status"].upper()
self.data = ConnectivityStatusChangedData(**self.data)
elif self.type == WebhookPayloadType.SessionStart:
self.data = SessionStartData(**self.data)
elif self.type == WebhookPayloadType.SessionUpdate:
self.data = SessionUpdateData(**self.data)
elif self.type == WebhookPayloadType.SessionStop:
self.data = SessionStopData(**self.data)
elif self.type == WebhookPayloadType.CDRCreated:
self.data = CDRCreatedData(**self.data)
elif self.type == WebhookPayloadType.LocationCreated:
self.data = LocationCreatedData(**self.data)
elif self.type == WebhookPayloadType.LocationUpdated:
self.data = LocationUpdatedData(**self.data)
elif self.type == WebhookPayloadType.MSPInvoiceProposalStatus:
self.data = MSPInvoiceProposalStatusData(**self.data)
elif self.type == WebhookPayloadType.Ping:
self.data = PingData(**self.data)

0 comments on commit e1c9a76

Please sign in to comment.