Code Monkey home page Code Monkey logo

check-status's Introduction

check-status

check-status is a Go library designed to periodically fetch and store order statuses from various providers. This library helps in managing the status of orders in memory and provides an interface to retrieve these statuses.

Features

  • Fetch order statuses from multiple providers at specified intervals.
  • Store order statuses in memory.
  • Retrieve order statuses by order ID.

Installation

To install the library, run:

go get github.com/Elvilius/check-status

Usage

Importing the library

import (
	"github.com/Elvilius/check-status"
)

Initializing CheckStatus

To initialize CheckStatus, you need to provide a slice of config.ProviderConfig which contains the configuration for each provider

package main

import (
	"github.com/Elvilius/check-status"
	"github.com/Elvilius/check-status/pkg/config"
)

func main() {
	providerConfigs := []config.ProviderConfig{
		{
			URL: "http://example.com/api/order-status",
			Method: "GET",
			Interval: 10,
			AuthHeaders: map[string]string{
				"Authorization": "Bearer your_token",
			},
			Adapter: &YourProviderAdapter{},
		},
		// Add more provider configurations as needed
	}

	cs := check_status.NewCheckStatus(providerConfigs)

	time.Sleep(10 * time.Second)
	// Use cs to get order status
	orderID := 12345
	status, err := cs.GetOrderStatus(orderID)
	if err != nil {
		// Handle error
	}
	// Use status
}

By default, the library uses the following adapter:

type DefaultProviderAdapter struct{}

func (a *DefaultProviderAdapter) AdaptResponse(data []byte) ([]models.OrderStatus, error) {
	var response []struct {
		OrderID int    `json:"order_id"`
		Status  string `json:"status"`
	}
	if err := json.Unmarshal(data, &response); err != nil {
		return nil, err
	}

	length := len(response)
	orders := make([]models.OrderStatus, length)

	for i, order := range response {
		orders[i] = models.OrderStatus{
			OrderID: order.OrderID,
			Status:  order.Status,
		}
	}
	return orders, nil
}

Configuring Providers

Each ProviderConfig contains the following fields:

  • URL: The URL of the provider's API endpoint.
  • Method: The HTTP method to use (e.g., "GET", "POST").
  • Interval: The interval at which the fetcher should request the provider's API.
  • AuthHeaders: A map of authentication headers required by the provider's API.
  • Adapter: An implementation of the Adapter interface to adapt the provider's response to models.OrderStatus.
  • Implementing an Adapter

Implementing an Adapter

You need to implement an adapter that converts the provider's response into the models.OrderStatus format. Here's an example:

type OrderStatus struct {
	OrderID int
	Status  string
}
type YourProviderAdapter struct{}

func (a *YourProviderAdapter) AdaptResponse(data []byte) ([]models.OrderStatus, error) {
	// Implement the logic to adapt the provider's response
}

Fetching Order Status

Once CheckStatus is initialized and running, you can fetch the status of an order using its ID

Monitoring

CheckStatus provides methods to retrieve metrics about the fetching process:

// GetMetrics returns the number of successful fetches, the number of failed fetches, and the total duration of all fetches.
func (cs *CheckStatus) GetMetrics() (int, int, time.Duration) {
	return cs.monitor.GetMetrics()
}

// GetMessageMetric returns a summary message of the fetch metrics.
func (cs *CheckStatus) GetMessageMetric() string {
	return cs.monitor.GetMessageMetric()
}

check-status's People

Watchers

Lucian avatar Victor Sergienko 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.