GithubHelp home page GithubHelp logo

alessandro-martin / go-lokalise-api Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lokalise/go-lokalise-api

0.0 0.0 0.0 1.86 MB

Lokalise API v2 Golang client library.

Home Page: https://developers.lokalise.com/reference/lokalise-rest-api

License: Other

Go 100.00%

go-lokalise-api's Introduction

Lokalise API v2 official Golang client library

GoDoc Build status Test Coverage

Index

Getting Started

Usage

import "github.com/lokalise/go-lokalise-api/v3"	// with go modules enabled (GO111MODULE=on or outside GOPATH)
import "github.com/lokalise/go-lokalise-api" // with go modules disabled

Initializing the client

token := os.Getenv("lokalise_token")
client, err := lokalise.New(token)

General options

You can set global API parameters with the ClientOption functions during the initialization. The following functions are available:

  • WithBaseURL
  • WithRetryCount
  • WithRetryTimeout
  • WithConnectionTimeout
  • WithDebug
  • WithPageLimit

Usage:

Api, err := lokalise.New(
    "token-string",
    lokalise.WithDebug(true),
    ...
)

Objects and models

Individual objects are represented as instances of according structs. Different objects are used for creating and updating in most cases.

Here are some object types:

  • Create/Update request objects, i.e. NewKey, NewContributor etc

  • Response objects: single/multiple, i.e. KeyResponse/KeysResponse and special , i.e. DeleteKeyResponse. There is no separate ErrorResponse - errors are encapsulated into concrete method's response.

  • List options that are used for sending certain options and pagination, i.e. KeyListOptions.

Request options and pagination

Some resources, such as Projects, Keys, Files, Tasks, Screenshots, Translations have optional parameters for List method (Keys also have an option for Retrieve). These parameters should be set before calling.

All request options could be set inline and separately:

// separately:
keys := client.Keys()
keys.SetListOptions(lokalise.KeyListOptions{
    IncludeTranslations: 1,
    IncludeComments: 1,
})

resp, err := keys.List("{PROJECT_ID}")

// inline:
client.Keys().WithListOptions(lokalise.KeyListOptions{Limit: 3}).List("{PROJECT_ID}")

There are two parameters, used for pagination: Limit and Page.

t := Api.Teams()
t.SetPageOptions(lokalise.PageOptions{
    Page:  2,
    Limit: 10,
})

resp, err := t.List()

Queued Processes

Some resource actions, such as Files.upload, are subject to intensive processing before request fulfills. These processes got optimised by becoming asynchronous. The initial request only queues the data for processing and retrieves to queued process identifier. Additional request to QueuedProcesses resource could be executed to obtain the current processing result.

Example with Files.upload:

projectId := "aaaabbbb.cccc"
uploadOpts := lokalise.FileUpload{
    Filename: "test.html",
    LangISO:  "en"
}

f := Api.Files()
resp, err = f.Upload(projectId, uploadOpts)

The successful response will contain process ID, which can be used to obtain the final result:

projectId := "aaaabbbb.cccc"
processId := "ddddeeeeeffff"

q := Api.QueuedProcesses()
resp, err := q.Retrieve(projectId, processId)

Rate limits

Access to all endpoints is limited to 6 requests per second from 14 September, 2021. This limit is applied per API token and per IP address. If you exceed the limit, a 429 HTTP status code will be returned and the corresponding exception will be raised that you should handle properly. To handle such errors, we recommend an exponential backoff mechanism with a limited number of retries.

Only one concurrent request per token is allowed.

Available resources

Comments

List project comments

projectId := "aaaabbbb.cccc"
c := Api.Comments()
c.SetPageOptions(lokalise.PageOptions{Page: 1, Limit: 20})
resp, err := c.ListProject(projectId)

List key comments

projectId := "aaaabbbb.cccc"
keyId := 26835183
c := Api.Comments()
c.SetPageOptions(lokalise.PageOptions{Page: 1, Limit: 20})
resp, err := c.ListByKey(projectId, keyId)

Create

c := lokalise.NewComment{Comment: "My new comment"}
resp, err := Api.Comments().Create(projectId, keyId, []lokalise.NewComment{c})

Retrieve

...
commentId := 26835183
resp, err := Api.Comments().Retrieve(projectId, keyId, commentId)

Delete

...
resp, err := Api.Comments().Delete(projectId, keyId, commentId)

Contributors

List all contributors

projectId := "aaaabbbb.cccc"
pageOpts := lokalise.PageOptions{Page: 1, Limit: 20}

c := Api.Contributors()
c.SetPageOptions(pageOpts)
c.List(projectId)

Create contributors

contributorCreate := lokalise.NewContributor{
    Email:    "[email protected]",
    Fullname: "New contributor",
    Permission: lokalise.Permission{
        IsAdmin:     true,
        IsReviewer:  true,
        Languages:   []lokalise.Language{{LangISO: "en", IsWritable: true}},
        AdminRights: []string{"upload", "download"},
    },
}
resp, err := Api.Contributors().Create(projectId, []lokalise.NewContributor{contributorCreate})

Retrieve contributor

userId := 47913 
resp, err := Api.Contributors().Retrieve(projectId, userId)

Update contributor

...
permissionUpdate := lokalise.Permission{
    IsReviewer: true,
    IsAdmin: false,
    AdminRights: []string{"keys", "upload", "download"},
}
resp, err := Api.Contributors().Update(projectId, userId, permissionUpdate)

go-lokalise-api's People

Contributors

yarlson avatar andrewlab-lokalise avatar crevil avatar dosexe avatar nickustinov avatar beinarovic avatar nemanjaaks avatar readper avatar nathanbak avatar sumanrana10048483 avatar

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.