Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Helper method for handling api requests #9

Merged
merged 3 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/deployments/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
}

resource "artie_deployment" "example" {
name = "MongoDB ➡️ BigQueryyy"
name = "MongoDB ➡️ BigQueryy"
source = {
name = "MongoDB"
config = {
Expand All @@ -35,12 +35,12 @@ resource "artie_deployment" "example" {
}
},
{
name = "stock"
name = "new_table"
schema = ""
advanced_settings = {}
},
{
name = "new_table"
name = "stock"
schema = ""
advanced_settings = {}
}
Expand Down
45 changes: 17 additions & 28 deletions internal/provider/deployment_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,20 +192,7 @@ func (r *DeploymentResource) Read(ctx context.Context, req resource.ReadRequest,
return
}

apiReq.Header.Set("Authorization", fmt.Sprintf("Bearer %s", r.apiKey))
apiResp, err := http.DefaultClient.Do(apiReq)
if err != nil {
resp.Diagnostics.AddError("Unable to Read Deployment", err.Error())
return
}

if apiResp.StatusCode != http.StatusOK {
resp.Diagnostics.AddError("Unable to Read Deployment", fmt.Sprintf("Received status code %d", apiResp.StatusCode))
return
}

defer apiResp.Body.Close()
bodyBytes, err := io.ReadAll(apiResp.Body)
bodyBytes, err := r.handleAPIRequest(apiReq)
if err != nil {
resp.Diagnostics.AddError("Unable to Read Deployment", err.Error())
return
Expand Down Expand Up @@ -255,20 +242,7 @@ func (r *DeploymentResource) Update(ctx context.Context, req resource.UpdateRequ
return
}

apiReq.Header.Set("Authorization", fmt.Sprintf("Bearer %s", r.apiKey))
apiResp, err := http.DefaultClient.Do(apiReq)
if err != nil {
resp.Diagnostics.AddError("Unable to Update Deployment", err.Error())
return
}

if apiResp.StatusCode != http.StatusOK {
resp.Diagnostics.AddError("Unable to Update Deployment", fmt.Sprintf("Received status code %d", apiResp.StatusCode))
return
}

defer apiResp.Body.Close()
bodyBytes, err := io.ReadAll(apiResp.Body)
bodyBytes, err := r.handleAPIRequest(apiReq)
if err != nil {
resp.Diagnostics.AddError("Unable to Update Deployment", err.Error())
return
Expand Down Expand Up @@ -303,3 +277,18 @@ func (r *DeploymentResource) Delete(ctx context.Context, req resource.DeleteRequ
func (r *DeploymentResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("uuid"), req, resp)
}

func (r *DeploymentResource) handleAPIRequest(apiReq *http.Request) (bodyBytes []byte, err error) {
apiReq.Header.Set("Authorization", fmt.Sprintf("Bearer %s", r.apiKey))
apiResp, err := http.DefaultClient.Do(apiReq)
if err != nil {
return []byte{}, err
}

if apiResp.StatusCode != http.StatusOK {
return []byte{}, fmt.Errorf("received status code %d", apiResp.StatusCode)
}

defer apiResp.Body.Close()
return io.ReadAll(apiResp.Body)
}