This is an unofficial WeTransfer Go SDK.
Status: v1.0.0-rc.1
If you find bugs, please file an issue here. Thank you!
Get the SDK with:
go get -v github.com/tors/wt-go-sdk
To get started, you'll need an API key. You can get one from WeTransfer's Developers Portal.
It's a good practice not to hardcode any private keys. In the event that you think your API key might be compromised, you can revoke it from within the developer portal.
You'll need to create an authorized client which automatically requests for a JWT token. This token is used in subsequent requests to the server.
apiKey := "<your-api-key>"
ctx := context.Background()
client, err := wt.NewAuthorizedClient(ctx, apiKey, nil)
For subsequent authorized requests, you'll need to pass a context.
A transfer is a collection of files that can be created once and downloaded until it expires. The expiry is set to 7 days and the maximum size of data per transfer is 2GB. Files are immutable once successfully uploaded.
You can use *Buffer
, and *LocalFile
types as file objects to create a
transfer.
// Buffer
ponya := wt.NewBuffer("pony.txt", []byte("yeehaaa"))
// LocalFile
ponyb := wt.NewLocalFile("disk/pony.txt")
client.Transfers.Create(ctx, &message, ponya, ponyb)
Transfers.Create
does the necessary steps to actually transfer the file.
Internally it does the whole ritual - create new transfer, request for upload
URLs, actual file upload to S3, complete the upload, and finalize it.
Transfers.Create
is a variadic function that accepts structs that implement
the Uploadable
interface.
var pets []Uploadable
pony := wt.NewBuffer("pony.txt", []byte("yeehaaa"))
kitten, _ := wt.NewLocalFile("disk/kitten.txt")
pets = append(pets, pony, kitten)
client.Transfers.Create(ctx, &message, pets...)
transfer, _ := client.Transfers.Find(ctx, "transfer-id")
fmt.Println(transfer.Files)
A board is collection of items that can be links or traditional files. Unlike
transfers
, boards' items do not have explicit expiry time as long as they
receive activity. If untouched, they expire after 3 months.
To create a board, a name is required. You can also pass a description which is optional. New boards have 0 items.
board, err := client.Boards.Create(ctx, "My kittens", nil)
fmt.Println(board.GetID())
In WeTransfer context, a link has two fields - a url which is required and a title which is optional. Links must added to existing boards.
// Add single link
desc := "Pony wiki"
pony, _ := wt.NewLink("https://en.wikipedia.org/wiki/Pony", &desc)
board, _ := client.Boards.AddLinks(ctx, board, pony)
// Add multiple
links := []*wt.Link{
pony,
&Link{
URL: "https://en.wikipedia.org/wiki/Kitten"
},
}
board, _ := client.Boards.AddLinks(ctx, board, links...)
fmt.Println(board.Items)
Files can be added to existing boards too. The way files are uploaded in boards is the same as how files are uploaded in trasfers. The only difference is that you can group files using the board and add more files in the future if needed.
pony := wt.NewBuffer("pony.txt", []byte("yehaaa"))
kitten := wt.NewBuffer("kitten.txt", []byte("meowww"))
narwhal, _ := wt.NewLocalFile("disk/narwhal.txt")
unicorn, _ := wt.NewLocalFile("disk/unicorn.txt")
client.Boards.AddFiles(ctx, board, pony, kitten, narwhal, unicorn)
// Pass a slice like Transfers
withHornsOnly := append(pets[:0], narwhal, unicorn)
client.Boards.AddFiles(ctx, board, withHornsOnly...)
board, _ := client.Boards.Find(ctx, "board-id")
fmt.Println(board.Items)
There are 2 types of test suites in this library - unit and integration. The unit tests do not make network calls so it should run pretty fast. Integration tests on the other hand verify that this SDK is coded in such a way that it follows the actual behavior of the live API. Actual data is sent over to the live API and if there are incompatibilities, integration tests should hopefully be able to catch those.
Run the unit tests...
make test
To run the integration tests, you'll need to get an API key and create an .env file like so:
echo WETRANSFER_API_TOKEN=key > integration/.env
Once you've done that, you can now run the integration test suite.
make integration