From 6f308e5e23be0d75069fea22f577580cc74c7e78 Mon Sep 17 00:00:00 2001 From: Preston Baxter Date: Thu, 16 Nov 2023 22:05:42 -0600 Subject: [PATCH] B: Working on jsonapi things --- .gitmodules | 3 ++ libs/jsonapi | 1 + service/vendors/pco/webhooks.go | 47 ++++++++++++++++++++ service/vendors/pco/webhooks/subscription.go | 27 +++++++++++ 4 files changed, 78 insertions(+) create mode 160000 libs/jsonapi create mode 100644 service/vendors/pco/webhooks.go create mode 100644 service/vendors/pco/webhooks/subscription.go diff --git a/.gitmodules b/.gitmodules index 5f26f0d..c46b286 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "libs/oauth2"] path = libs/oauth2 url = https://git.preston-baxter.com/Preston_PLB/oauth2.git +[submodule "libs/jsonapi"] + path = libs/jsonapi + url = https://git.preston-baxter.com/Preston_PLB/jsonapi.git diff --git a/libs/jsonapi b/libs/jsonapi new file mode 160000 index 0000000..1e07b10 --- /dev/null +++ b/libs/jsonapi @@ -0,0 +1 @@ +Subproject commit 1e07b10d47d02b07ccaa2dfc5ceec143bdd81c14 diff --git a/service/vendors/pco/webhooks.go b/service/vendors/pco/webhooks.go new file mode 100644 index 0000000..75dbf2e --- /dev/null +++ b/service/vendors/pco/webhooks.go @@ -0,0 +1,47 @@ +package pco + +import ( + "fmt" + "net/http" + "reflect" + + "git.preston-baxter.com/Preston_PLB/capstone/webhook-service/vendors/pco/webhooks" + "github.com/google/jsonapi" +) + + +func (api *PcoApiClient) GetSubscriptions() ([]services.Subscription, error) { + api.Url().Path = "/webhook/v2/subscriptions" + + req, err := http.NewRequest(http.MethodGet, api.Url().String(), nil) + if err != nil { + return nil, err + } + + resp, err := api.Do(req) + if err != nil { + return nil, err + } + + if resp.StatusCode > 299 || resp.StatusCode < 200 { + return nil, fmt.Errorf("Failed to retrieve plan with status code: %d", resp.StatusCode) + } + + raw, err := jsonapi.UnmarshalManyPayload(resp.Body, reflect.TypeOf(webhooks.Subscription{})) + if err != nil { + return nil, err + } + + webhooks := make([]webhooks.Subscription, len(raw)) + for index, hook := range raw { + var ok bool + webhooks[index], ok = hook.(reflect.TypeOf(webhooks.Subscription)) + if !ok { + return fmt.Errorf("Failed to extract webhook payload") + } + } + + + + return plan, nil +} diff --git a/service/vendors/pco/webhooks/subscription.go b/service/vendors/pco/webhooks/subscription.go new file mode 100644 index 0000000..5cc9d31 --- /dev/null +++ b/service/vendors/pco/webhooks/subscription.go @@ -0,0 +1,27 @@ +package webhooks + +import "time" + +type Subscription struct { + Id string `jsonapi:"primary,Subscription"` + //attrs + Active bool `jsonapi:"attr,active,omitempty"` + ApplicationId string `jsonapi:"attr,application_id,omitempty"` + AuthenticitySecret bool `jsonapi:"attr,authenticity_secret,omitempty"` + CreatedAt time.Time `jsonapi:"attr,created_at,omitempty"` + UpdatedAt time.Time `jsonapi:"attr,updated_at,omitempty"` + Name string `jsonapi:"attr,name,omitempty"` + Url string `jsonapi:"attr,url,omitempty"` +} + +type WebhookSubscription struct { + Id string `jsonapi:"primary,WebhookSubscription"` + //attrs + Active bool `jsonapi:"attr,active,omitempty"` + ApplicationId string `jsonapi:"attr,application_id,omitempty"` + AuthenticitySecret bool `jsonapi:"attr,authenticity_secret,omitempty"` + CreatedAt time.Time `jsonapi:"attr,created_at,omitempty"` + UpdatedAt time.Time `jsonapi:"attr,updated_at,omitempty"` + Name string `jsonapi:"attr,name,omitempty"` + Url string `jsonapi:"attr,url,omitempty"` +}