Giter Club home page Giter Club logo

crowdin-api-client-go's Introduction

Crowdin Go client

The Crowdin Go client is a lightweight interface to the Crowdin API. It provides common services for making API requests.

Our API is a full-featured RESTful API that helps you to integrate localization into your development process. The endpoints that we use allow you to easily make calls to retrieve information and to execute actions needed.

Installation

go get github.com/crowdin/crowdin-api-client-go

Quick Start

Create a new Crowdin client, then use the exposed services to access different parts of the Crowdin API.
You can generate Personal Access Token in your Crowdin Account Settings.

import "github.com/crowdin/crowdin-api-client-go/crowdin"

client, err := crowdin.NewClient(
    os.Getenv("CROWDIN_ACCESS_TOKEN"),
    crowdin.WithOrganization("organization-name"), // optional for Crowdin Enterprise
)

For example, to create a new project:

package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/crowdin/crowdin-api-client-go/crowdin"
	"github.com/crowdin/crowdin-api-client-go/crowdin/model"
)

func main() {
    client, err := crowdin.NewClient(os.Getenv("CROWDIN_ACCESS_TOKEN"))
    if err != nil {
        log.Fatalf("Error creating client: %s", err)
    }

    ctx := context.Background()
    request := &model.ProjectsAddRequest{
        Name: "My Project",
		SourceLanguageID: "en",
		TargetLanguageIDs: []string{"uk", "de"},

    }
    project, _, err := client.Projects.Add(ctx, request)
    if err != nil {
        log.Fatalf("Error creating project: %s", err)
    }

    fmt.Printf("Project: %+v\n", project)
}

Some API methods have optional parameters that can be passed.
For example, to list all projects for a specific user:

package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/crowdin/crowdin-api-client-go/crowdin"
	"github.com/crowdin/crowdin-api-client-go/crowdin/model"
)

func main() {
    client, err := crowdin.NewClient(os.Getenv("CROWDIN_ACCESS_TOKEN"))
    if err != nil {
        log.Fatalf("Error creating client: %s", err)
    }

    // list all projects for a specific user
    opts := &model.ProjectsListOptions{UserID: 1}
    projects, _, err := client.Projects.List(context.Background(), opts)
    if err != nil {
        log.Fatalf("Error getting projects: %s", err)
    }

    fmt.Printf("Projects: %+v\n", projects)
}

Error Handling

In case of an error, the client returns an error object. This can either be a generic error with an error message and a code, or a validation error that additionally contains validation error codes.

To detect this condition of error, you can use a type assertion:

res, _, err := client.SourceStrings.Add(ctx, 1, nil)
if err != nil {
    if validationErr, ok := err.(*model.ValidationErrorResponse); ok {
        fmt.Printf("Validation error: %v\n", validationErr)
    } else 
        fmt.Printf("Error: %v\n", err)
    }
}

HTTP Request Timeout

To set a timeout for HTTP requests, you can pass a custom HTTP client with a timeout to the client.
You can also use the context package, you can pass cancellation signals and deadlines to various services of the client to handle a request.

client, err := crowdin.NewClient(
    os.Getenv("CROWDIN_ACCESS_TOKEN"),
    crowdin.WithHTTPClient(&http.Client{Timeout: 10 * time.Second}),
)

Seeking Assistance

If you find any problems or would like to suggest a feature, please read the How can I contribute section in our contributing guidelines.

Contributing

If you want to contribute please read the Contributing guidelines.

License

The Crowdin Go client is licensed under the MIT License.
See the LICENSE file distributed with this work for additional
information regarding copyright ownership.

Except as contained in the LICENSE file, the name(s) of the above copyright
holders shall not be used in advertising or otherwise to promote the sale,
use or other dealings in this Software without prior written authorization.

crowdin-api-client-go's People

Contributors

andrii-bodnar avatar vorobeyme avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

crowdin-api-client-go's Issues

report add storage method bug

Behavior

upload file by add storage method would earse exited file content(exited file content was removed).

Replay

Env:

  • crowdin-api-client-go: v0.6.0
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/crowdin/crowdin-api-client-go/crowdin"
	"github.com/crowdin/crowdin-api-client-go/crowdin/model"
	"github.com/pkg/errors"
)

func main(){
  	client, err := crowdin.NewClient(
		os.Getenv("CROWDIN_ACCESS_TOKEN"),
	)
	if err != nil {
		log.Fatal("NewClientErr: ", err)
	}

      // add storage for update file.
	filePath := "./test.json"
	file, err := os.Open(filePath)
	if err != nil {
		log.Fatal("Open file err: ", err)
	}
	addStorage, resp, err := client.Storages.Add(context.Background(), file)
	fmt.Println("AddStorages: ", res.Status)
	if err != nil {
		log.Fatal("NewClientErr: ", err)
	}
	// Output:
	// AddStorages:  201 Created
	// {
	//    "id": 21***837,
	//    "fileName": "test.json"
	// }

	// update file content, I confirm there are some entry from test.json file  before update by browser dashboard. and file was empty after execute below update method.
	projectID := 4****3
	updateFile, resp, err := client.SourceFiles.UpdateOrRestoreFile(context.Background(), projectID, 24384, &model.FileUpdateRestoreRequest{
		StorageID:    addStorage.ID,
		UpdateOption: "clear_translations_and_approvals",
	})
}

bug trace:

image
image
image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.