-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: clients, preparing the ground for deduplication (#212)
* refactor: client, preparing for deduplication * chore(api): format code
- Loading branch information
Showing
37 changed files
with
774 additions
and
652 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.