Skip to content

bugscatcher/go-yamusic

 
 

Repository files navigation

yamusic

GoDoc Go Report Card Build Status Coverage Status Contributions Welcome

Description

Unofficial Go client library for Yandex.Music API.

Golang fork of Node.js library. Client style based on google/go-github.

Usage

import "github.com/ndrewnee/go-yamusic/yamusic"

Construct a new Yandex.Music client, then use the various services on the client to access different parts of the Yandex.Music API.

Using functional options for friendly APIs.

package main

import (
    "github.com/ndrewnee/go-yamusic/yamusic"
    "github.com/rubyist/circuitbreaker"
    "context"
    "log"
    "net/http"
    "time"
)

func main() {
    // constructing http client with circuit breaker
    // it implements yamusic.Doer interface
    circuitClient := circuit.NewHTTPClient(time.Second * 5, 10, nil)
    client := yamusic.NewClient(
        // if you want http client with circuit breaker
        yamusic.HTTPClient(circuitClient),
        // provide user_id and access_token (needed by some methods)
        yamusic.AccessToken(100, "some_access_token"),
    )
    // list all genres
    genres, resp, err := client.Genres().List(context.Background())
    if err != nil {
        log.Fatal(err)
    }
    // resp is general type *http.Response
    if resp.StatusCode != http.StatusOK {
        log.Fatal("http status is not 200")
    }
    log.Println("Genres: ", genres)
    // create new public playlist. Need access token
    createdPlaylist, _, err := client.Playlists().Create(context.Background(), "New Playlist", true)
    if err != nil {
        log.Fatal(err)
    }
    log.Println("Created playlist: ", createdPlaylist)
}

Tests

Running unit tests:

go test ./yamusic/

Running integration tests:

go test -tags=integration ./test/integration/

Note that you should set YANDEX_USER_ID and YANDEX_ACCESS_TOKEN environment variables.