Skip to content
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
12 changes: 6 additions & 6 deletions api/batches/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ const jobBasePath = "/job-batches-api/v2/projects/"

// Batch defines the batch behaviour
type Batch interface {
Create(ctx context.Context, projectID string, payload CreateBatchPayload) (CreateBatchResponse, error)
CreateJob(ctx context.Context, projectID string, payload CreateJobPayload) (CreateJobResponse, error)
Create(projectID string, payload CreateBatchPayload) (CreateBatchResponse, error)
CreateJob(projectID string, payload CreateJobPayload) (CreateJobResponse, error)
UploadFile(ctx context.Context, projectID, batchUID string, payload UploadFilePayload) (UploadFileResponse, error)
GetStatus(ctx context.Context, projectID, batchUID string) (GetStatusResponse, error)
GetStatus(projectID, batchUID string) (GetStatusResponse, error)
}

// NewBatch returns new Batch implementation
Expand All @@ -39,7 +39,7 @@ func newHttpBatch(client *smclient.Client) httpBatch {
}

// Create creates a new batch in the specified project
func (h httpBatch) Create(ctx context.Context, projectID string, payload CreateBatchPayload) (CreateBatchResponse, error) {
func (h httpBatch) Create(projectID string, payload CreateBatchPayload) (CreateBatchResponse, error) {
url := jobBasePath + projectID + "/batches"
payloadB, err := json.Marshal(payload)
if err != nil {
Expand Down Expand Up @@ -70,7 +70,7 @@ func (h httpBatch) Create(ctx context.Context, projectID string, payload CreateB
}

// CreateJob creates a new job in the specified project
func (h httpBatch) CreateJob(ctx context.Context, projectID string, payload CreateJobPayload) (CreateJobResponse, error) {
func (h httpBatch) CreateJob(projectID string, payload CreateJobPayload) (CreateJobResponse, error) {
url := jobBasePath + projectID + "/jobs"
payloadB, err := json.Marshal(payload)
if err != nil {
Expand Down Expand Up @@ -196,7 +196,7 @@ func (h httpBatch) UploadFile(ctx context.Context, projectID, batchUID string, p
}

// GetStatus retrieves the status of a batch in the specified project
func (h httpBatch) GetStatus(ctx context.Context, projectID, batchUID string) (GetStatusResponse, error) {
func (h httpBatch) GetStatus(projectID, batchUID string) (GetStatusResponse, error) {
url := jobBasePath + projectID + "/batches/" + batchUID
var response getStatusResponse
rawMessage, code, err := h.client.Get(url, nil)
Expand Down
55 changes: 55 additions & 0 deletions api/job/job.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package job

import (
"encoding/json"
"fmt"
"io"

smclient "github.com/Smartling/api-sdk-go/helpers/sm_client"
)

const jobBasePath = "/job-batches-api/v2/projects/"

// Job defines the job behaviour
type Job interface {
GetJob(projectID string, translationJobUID string) (GetJobResponse, error)
}

// NewJob returns new Job implementation
func NewJob(client *smclient.Client) Job {
return newHttpJob(client)
}

// httpJob implements Job interface using HTTP client
type httpJob struct {
client *smclient.Client
}

func newHttpJob(client *smclient.Client) httpJob {
return httpJob{client: client}
}

// GetJob gets a job related info
func (h httpJob) GetJob(projectID string, translationJobUID string) (GetJobResponse, error) {
url := jobBasePath + projectID + "/jobs/" + translationJobUID
var response getJobResponse
rawMessage, code, err := h.client.Get(url, nil)
if err != nil {
return GetJobResponse{}, err
}
if code != 200 {
body, _ := io.ReadAll(rawMessage)
h.client.Logger.Debugf("response body: %s\n", body)
return GetJobResponse{}, fmt.Errorf("unexpected response code: %d", code)
}
body, err := io.ReadAll(rawMessage)
if err != nil {
body, _ := io.ReadAll(rawMessage)
h.client.Logger.Debugf("response body: %s\n", body)
return GetJobResponse{}, err
}
if err := json.Unmarshal(body, &response); err != nil {
return GetJobResponse{}, fmt.Errorf("failed to unmarshal response: %w", err)
}
return toGetJobResponse(response), nil
}
23 changes: 23 additions & 0 deletions api/job/job_ent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package job

// GetJobResponse defines get job response
type GetJobResponse struct {
TranslationJobUID string
JobName string
}
type getJobResponse struct {
Response struct {
Code string `json:"code"`
Data struct {
JobName string `json:"jobName"`
TranslationJobUID string `json:"translationJobUid"`
} `json:"data"`
} `json:"response"`
}

func toGetJobResponse(r getJobResponse) GetJobResponse {
return GetJobResponse{
TranslationJobUID: r.Response.Data.TranslationJobUID,
JobName: r.Response.Data.JobName,
}
}
5 changes: 1 addition & 4 deletions api/mt/uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,7 @@ func (u httpUploader) UploadFile(accountUID AccountUID, filename string, req Upl
return UploadFileResponse{}, fmt.Errorf("failed to unmarshal: %v", err)
}

return UploadFileResponse{
Code: response.Response.Code,
FileUID: FileUID(response.Response.Data.FileUID),
}, nil
return toUploadFileResponse(response), nil
}

func buildUploadFilePath(accountUID AccountUID) string {
Expand Down