Skip to content

OsoianMarcel/bnm-go

Repository files navigation

bnm-go v2

A modern Go client for retrieving official exchange rates from the National Bank of Moldova (BNM).

Build Status GoDev license

This version is a complete redesign of v1, built with improved Go practices, full test coverage, and extensibility for real-world usage.

Features

  • 100% unit test coverage – fully tested and reliable.
  • Pluggable caching – support for custom cache adapters.
  • Context support – cancel or timeout ongoing requests with context.Context.
  • Concurrent safe – designed for multi-goroutine usage.
  • Flexible configuration – functional options to customize cache, HTTP client, unmarshaler, logging, etc.

Quick Start

package main

import (
    "context"
    "fmt"
    "log"
    "time"

    "github.com/OsoianMarcel/bnm-go/v2"
)

func main() {
    client := bnm.NewClient(
        bnm.WithCache(bnm.NewMemoryCache()),
        bnm.WithWarnError(func(err error) {
            log.Printf("warn: %v", err)
        }),
    )

    ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
    defer cancel()

    resp, err := client.Fetch(ctx, bnm.NewQuery(time.Now(), bnm.LANG_EN))
    if err != nil {
        log.Fatalf("failed to fetch rates: %v", err)
    }

    fmt.Printf("Rates: %+v\n", resp)
}

Configuration Options

  • WithCache(cache Cache) – provide a cache implementation.
  • WithWarnError(fn WarnFunc) – handle non-critical errors gracefully.
  • WithGetRequest(fn GetRequestFunc) – override HTTP request logic.
  • WithUnmarshaler(fn UnmarshalerFunc) – customize response unmarshaling.

Testing

This project follows Go testing best practices.

Run all tests with:

go test ./...

Generate a detailed coverage report:

go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out

✅ v2 is fully covered by unit tests.

Contribute

Contributions to the package are always welcome!

License

All contents of this package are licensed under the MIT license.

About

Go library used to get official exchange rates of National bank of Moldova

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages