Skip to content

Commit

Permalink
refactor: clients, preparing the ground for deduplication (#212)
Browse files Browse the repository at this point in the history
* refactor: client, preparing for deduplication

* chore(api): format code
  • Loading branch information
bouassaba authored Jul 23, 2024
1 parent 465d178 commit 123a7f5
Show file tree
Hide file tree
Showing 37 changed files with 774 additions and 652 deletions.
213 changes: 0 additions & 213 deletions conversion/client/api_client.go

This file was deleted.

52 changes: 52 additions & 0 deletions conversion/client/api_client/health_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2023 Anass Bouassaba.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the GNU Affero General Public License v3.0 only, included in the file
// licenses/AGPL.txt.

package api_client

import (
"fmt"
"io"
"net/http"

"github.com/kouprlabs/voltaserve/conversion/config"
"github.com/kouprlabs/voltaserve/conversion/infra"
)

type HealthClient struct {
config *config.Config
}

func NewHealthClient() *HealthClient {
return &HealthClient{
config: config.GetConfig(),
}
}

func (cl *HealthClient) Get() (string, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/v2/health", cl.config.APIURL), nil)
if err != nil {
return "", err
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer func(Body io.ReadCloser) {
if err := Body.Close(); err != nil {
infra.GetLogger().Error(err)
}
}(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}
110 changes: 110 additions & 0 deletions conversion/client/api_client/snapshot_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright 2023 Anass Bouassaba.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the GNU Affero General Public License v3.0 only, included in the file
// licenses/AGPL.txt.

package api_client

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"

"github.com/kouprlabs/voltaserve/conversion/config"
"github.com/kouprlabs/voltaserve/conversion/infra"
)

type SnapshotClient struct {
config *config.Config
}

func NewSnapshotClient() *SnapshotClient {
return &SnapshotClient{
config: config.GetConfig(),
}
}

type SnapshotPatchOptions struct {
Options PipelineRunOptions `json:"options"`
Fields []string `json:"fields"`
Original *S3Object `json:"original"`
Preview *S3Object `json:"preview"`
Text *S3Object `json:"text"`
OCR *S3Object `json:"ocr"`
Entities *S3Object `json:"entities"`
Mosaic *S3Object `json:"mosaic"`
Thumbnail *S3Object `json:"thumbnail"`
Status *string `json:"status"`
TaskID *string `json:"taskId"`
}

const (
SnapshotStatusWaiting = "waiting"
SnapshotStatusProcessing = "processing"
SnapshotStatusReady = "ready"
SnapshotStatusError = "error"
)

const (
SnapshotFieldOriginal = "original"
SnapshotFieldPreview = "preview"
SnapshotFieldText = "text"
SnapshotFieldOCR = "ocr"
SnapshotFieldEntities = "entities"
SnapshotFieldMosaic = "mosaic"
SnapshotFieldThumbnail = "thumbnail"
SnapshotFieldStatus = "status"
SnapshotFieldLanguage = "language"
SnapshotFieldTaskID = "taskId"
)

type PipelineRunOptions struct {
PipelineID *string `json:"pipelineId,omitempty"`
TaskID string `json:"taskId"`
SnapshotID string `json:"snapshotId"`
Bucket string `json:"bucket"`
Key string `json:"key"`
Payload map[string]string `json:"payload,omitempty"`
}

type S3Object struct {
Bucket string `json:"bucket"`
Key string `json:"key"`
Size *int64 `json:"size,omitempty"`
Image *ImageProps `json:"image,omitempty"`
}

type ImageProps struct {
Width int `json:"width"`
Height int `json:"height"`
}

func (cl *SnapshotClient) Patch(opts SnapshotPatchOptions) error {
body, err := json.Marshal(opts)
if err != nil {
return err
}
req, err := http.NewRequest("PATCH", fmt.Sprintf("%s/v2/snapshots/%s?api_key=%s", cl.config.APIURL, opts.Options.SnapshotID, cl.config.Security.APIKey), bytes.NewBuffer(body))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer func(Body io.ReadCloser) {
if err := Body.Close(); err != nil {
infra.GetLogger().Error(err)
}
}(resp.Body)
return nil
}
Loading

0 comments on commit 123a7f5

Please sign in to comment.