psref

package module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 24, 2022 License: MIT Imports: 13 Imported by: 0

README

psref

Go Reference

Unofficial client for Lenovo PSREF.

It supports listing all available (and discontinued) products, their models and specifications.

See examples on GoDev.

Documentation

Overview

Package psref implements an (unofficial) Lenovo PSREF API client.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound is returned when lookup leads to no results.
	ErrNotFound = errors.New("not found")
)

Functions

This section is empty.

Types

type Book

type Book struct {
	Title  string `json:"BookTitle"`
	URL    string `json:"BookLink"`
	Geo    string `json:"Geo"`
	Remark string `json:"Remark"`
}

Book is a reference to a resource for users to read.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client for PSREF API.

Example
package main

import (
	"context"
	"fmt"

	"github.com/dennwc/psref"
)

func main() {
	c := psref.NewClient()

	ctx := context.Background()

	// https://psref.lenovo.com/Detail/ThinkPad/ThinkPad_X1_Carbon_Gen_10?M=21CB000AUS
	const code = "21CB000AUS"

	m, err := c.ModelByCode(ctx, code)
	if err != nil {
		panic(err)
	}
	fmt.Printf("Product %d (%s):\n\tCPU: %s\n\tRAM: %s\n\tGPU: %s\n\tDisk: %s\n",
		m.ID, m.Key,
		m.DetailByName("Processor"), m.DetailByName("Memory"),
		m.DetailByName("Graphics"), m.DetailByName("Storage"),
	)
}
Output:

Product 1972 (ThinkPad_X1_Carbon_Gen_10):
	CPU: Intel Core i5-1240P, 12C (4P + 8E) / 16T, P-core 1.7 / 4.4GHz, E-core 1.2 / 3.3GHz, 12MB
	RAM: 16GB Soldered LPDDR5-5200
	GPU: Integrated Intel Iris Xe Graphics
	Disk: 256GB SSD M.2 2280 PCIe 4.0x4 NVMe Opal2

func NewClient

func NewClient(opts ...ClientOption) *Client

NewClient creates a client with specified options.

By default, the client will retry requests a few times and will use a conservative rate limit. See WithRetry and WithRate to adjust these settings.

func (*Client) Books

func (c *Client) Books(ctx context.Context) ([]Book, error)

Books returns a list of resources for users to read.

func (*Client) ModelByCode

func (c *Client) ModelByCode(ctx context.Context, code ModelCode) (*Model, error)

ModelByCode returns information about the given product model.

This method uses the search API, which might be considerably slower. Use ModelByID instead.

func (*Client) ModelByID

func (c *Client) ModelByID(ctx context.Context, id PID, code ModelCode) (*Model, error)

ModelByID returns information about the given product model.

func (*Client) ProductByID

func (c *Client) ProductByID(ctx context.Context, id PID) (*Product, error)

ProductByID returns an information about the product, given its numeric PSREF ID. Description of the product includes a list of all available product models.

func (*Client) ProductByModelCode

func (c *Client) ProductByModelCode(ctx context.Context, code ModelCode) (*Product, error)

ProductByModelCode returns an information about the product, given its alphanumeric code of one of the models.

This method uses the search API, which might be considerably slower. Use ProductByID instead.

func (*Client) Products

func (c *Client) Products(ctx context.Context) ([]ProductType, error)

Products lists all available and active products. See WithdrawnProducts for discontinued ones.

func (*Client) Search

func (c *Client) Search(ctx context.Context, qu string) ([]SearchResult, error)

Search PSREF data using keywords.

func (*Client) Updates

func (c *Client) Updates(ctx context.Context) (*Updates, error)

Updates returns an information about the current version of PSREF data and a list of added/updated/deleted entries.

func (*Client) WithdrawnProducts

func (c *Client) WithdrawnProducts(ctx context.Context) ([]ProductType, error)

WithdrawnProducts is similar to Products, but returns discontinued products instead of active ones.

type ClientOption added in v0.2.0

type ClientOption interface {
	// contains filtered or unexported methods
}

ClientOption controls different aspects of Client behavior.

func WithBaseURL added in v0.2.0

func WithBaseURL(url string) ClientOption

WithBaseURL changes the base URL for all API requests.

func WithDebug added in v0.2.0

func WithDebug(w io.Writer) ClientOption

WithDebug sets a debug log output.

func WithHTTPClient added in v0.2.0

func WithHTTPClient(cli *http.Client) ClientOption

WithHTTPClient sets an HTTP client for all requests.

func WithRate added in v0.2.0

func WithRate(rate *rate.Limiter) ClientOption

WithRate sets a rate limit for all requests. Passing nil will disable rate limiting.

func WithRetry added in v0.2.0

func WithRetry(retries int) ClientOption

WithRetry sets the number of retries per request. Setting 0 or 1 means send request only once, setting -1 means retry until completion.

type Date

type Date time.Time

Date is wrapper around time.Time which uses custom JSON encoding.

func (Date) MarshalJSON

func (d Date) MarshalJSON() ([]byte, error)

func (*Date) UnmarshalJSON

func (d *Date) UnmarshalJSON(data []byte) error

type Documentation

type Documentation struct {
	ProductID PID    `json:"ProductId"`
	Title     string `json:"DocTitle"`
	URL       string `json:"DocLink"`
}

Documentation is a reference to documentation resource.

type KeyValue

type KeyValue struct {
	Name  string `json:"Name"`
	Value string `json:"Value"`
}

KeyValue pair used in the model specifications.

type Model

type Model struct {
	Product
	WithdrawnStatus int        `json:"M_WdStatus"`
	RefURL          string     `json:"ModelURL"`
	Detail          []KeyValue `json:"Detail"`
	Code            ModelCode  `json:"ModelCode"`
}

Model is a full model information, including exact specifications. Not all Product fields will be set.

func (*Model) DetailByName

func (m *Model) DetailByName(name string) string

DetailByName searches a specification value by the key name.

type ModelCode

type ModelCode string

ModelCode is an alphanumeric product code.

type ModelInfo

type ModelInfo struct {
	Code    ModelCode `json:"ModelCode"`
	Summary string    `json:"Summary"`
	Updated Date      `json:"Updated"`
}

ModelInfo is a basic model info used in the model list.

type PID

type PID uint64

PID is a numeric PSREF product ID.

type Product

type Product struct {
	ID              PID             `json:"ProductId"`
	Key             string          `json:"ProductKey"`
	Name            string          `json:"Name"`
	RefURL          string          `json:"ProductURL"`
	WithdrawnStatus int64           `json:"P_WdStatus"`
	SpecURL         string          `json:"Spec"`
	US_Pdf          string          `json:"US_Pdf"`
	EMEA_Pdf        string          `json:"EMEA_Pdf"`
	WW_Pdf          string          `json:"WW_Pdf"`
	Image           string          `json:"ImageForShare"`
	Images          []string        `json:"Images"`
	Models          []ModelInfo     `json:"Models"`
	Docs            []Documentation `json:"Documentations"`
}

Product is a full product information. It includes multiple models, which in turn list exact specifications.

type ProductLine

type ProductLine struct {
	Name   string   `json:"ProductLineName"`
	Image  string   `json:"ImageUrl"`
	Series []Series `json:"Series"`
}

ProductLine includes multiple product series.

type ProductShort

type ProductShort struct {
	ID              PID    `json:"ProductId"`
	Key             string `json:"ProductKey"`
	Name            string `json:"ProductName"`
	WithdrawnStatus int64  `json:"P_WdStatus"`
	Updated         Date   `json:"LastUpdated"`
	ModelModified   Date   `json:"ModelModifyDateTime"`
	ConfigModified  Date   `json:"ConfigModifyDateTime"`
}

ProductShort is a short product description.

type ProductType

type ProductType struct {
	Name    string        `json:"ClassificationName"`
	BgColor string        `json:"BackgroundColor"`
	Lineup  []ProductLine `json:"ProductLine"`
}

ProductType is a top-level product type which includes multiple product lineups.

type SearchResult

type SearchResult struct {
	ID     PID    `json:"ProductId"`
	Name   string `json:"ProductName"`
	Models int    `json:"ModelCount"`
}

SearchResult as returned by the Search API.

type Series

type Series struct {
	Name     string         `json:"SeriesName"`
	Products []ProductShort `json:"Products"`
}

type UpdatedProduct

type UpdatedProduct struct {
	ID     PID    `json:"productId"`
	Title  string `json:"title"`
	Reason string `json:"reason,omitempty"`
}

UpdatedProduct is an information about product update used in the PSREF Updates info.

type Updates

type Updates struct {
	Version      uint64    `json:"x_Version"`   // TODO: upstream
	VersionTS    time.Time `json:"x_VersionTS"` // TODO: upstream
	VersionTitle string    `json:"LatestUpdateVersion"`

	New       []UpdatedProduct `json:"New"`
	Updated   []UpdatedProduct `json:"Updated"`
	Withdrawn []UpdatedProduct `json:"Withdrawn"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL