Documentation
¶
Overview ¶
Package app implements the use cases shared by the CLI and the MCP server: session signing with automatic re-login, the auth commands, and the domain operations built on the Repsol API.
Index ¶
- Constants
- func AliasFor(materialID string) string
- type AddressView
- type AgencyView
- type App
- func (a *App) Catalog(ctx context.Context) ([]CatalogItem, error)
- func (a *App) Doctor(ctx context.Context) DoctorReport
- func (a *App) ImportSession(ctx context.Context, session gigya.Session) (AuthStatus, error)
- func (a *App) LastOrder(ctx context.Context) (OrderSummary, error)
- func (a *App) ListOrders(ctx context.Context, limit int) ([]OrderSummary, error)
- func (a *App) Logout() (LogoutResult, error)
- func (a *App) PasswordLogin(ctx context.Context, email, password string) (AuthStatus, error)
- func (a *App) PlaceOrder(ctx context.Context, in PlaceOrderInput) (OrderResult, error)
- func (a *App) Schedule(ctx context.Context, product string) (ScheduleView, error)
- func (a *App) SetAddress(ctx context.Context, in SetAddressInput) (AddressView, error)
- func (a *App) ShowAddress() (AddressView, error)
- func (a *App) ShowOrder(ctx context.Context, reference string) (OrderSummary, error)
- func (a *App) Status(ctx context.Context, check bool) (AuthStatus, error)
- type AuthStatus
- type CatalogItem
- type Deps
- type DoctorCheck
- type DoctorReport
- type Env
- type LogoutResult
- type OrderResult
- type OrderSummary
- type PlaceOrderInput
- type ProductInfo
- type ScheduleView
- type Session
- type SetAddressInput
Constants ¶
const PendingLookback = 20
PendingLookback is how many history entries the pending guard reads before a write.
const ShowLookback = 100
ShowLookback is how many history entries order show searches.
const SignatureMaxAge = 50 * time.Second
SignatureMaxAge is how long an obtained signature is reused before it is refreshed. The web application refreshes it every 50 seconds.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type AddressView ¶
type AddressView struct {
// Profile is the profile the address belongs to.
Profile string `json:"profile"`
// Summary is the full address followed by the floor and door.
Summary string `json:"summary"`
// FullAddress is the address the service returned.
FullAddress string `json:"full_address"`
// PostalCode is the five-digit postal code of the full address.
PostalCode string `json:"postal_code"`
// Locality is the town or city.
Locality string `json:"locality"`
// Province is the name of the province.
Province string `json:"province"`
// Floor is the floor of the delivery address.
Floor string `json:"floor,omitempty"`
// Door is the door of the delivery address.
Door string `json:"door,omitempty"`
// Details are free-form delivery notes.
Details string `json:"details,omitempty"`
// AgencyCode is the agency the Repsol API resolved for the address.
AgencyCode string `json:"agency_code"`
// Origin is "repsol" or "google", which candidate the address came from.
Origin string `json:"origin"`
}
AddressView is the stored address as the CLI and the MCP server print it.
func (AddressView) PrettyPrint ¶
func (v AddressView) PrettyPrint(w io.Writer) error
PrettyPrint writes the stored address as human-readable text. It uses no ANSI colours, so NO_COLOR holds without any code.
type AgencyView ¶
type AgencyView struct {
// Name is the agency name.
Name string `json:"name,omitempty"`
// Phone is the agency phone.
Phone string `json:"phone,omitempty"`
// Code is the numeric agency code.
Code string `json:"code,omitempty"`
}
AgencyView is the agency of an order, as the CLI and the MCP server print it.
type App ¶
type App struct {
// contains filtered or unexported fields
}
App is the entry point of every use case. Create it with New.
func (*App) Catalog ¶
func (a *App) Catalog(ctx context.Context) ([]CatalogItem, error)
Catalog returns the products the agency of the stored address sells. The catalog route lists packaging rows without a material id; they are dropped because they are not orderable.
func (*App) Doctor ¶
func (a *App) Doctor(ctx context.Context) DoctorReport
Doctor probes the local configuration and the Repsol backends without writing anything: it loads both TOML files, signs the stored session, asks the API for its health, reads the stored address and resolves its agency. A check that depends on a failed one is skipped and says so.
func (*App) ImportSession ¶
ImportSession validates an imported session with one signature refresh and stores it. A stored password survives only when it belongs to the imported account, because a password from another account would log the wrong user in.
func (*App) LastOrder ¶
func (a *App) LastOrder(ctx context.Context) (OrderSummary, error)
LastOrder returns the newest history entry. It is a not-found error when the profile has no orders.
func (*App) ListOrders ¶
ListOrders returns the newest history entries, at most limit of them.
func (*App) Logout ¶
func (a *App) Logout() (LogoutResult, error)
Logout deletes the stored credentials of the profile and forgets the cached signature. Other profiles are left untouched.
func (*App) PasswordLogin ¶
PasswordLogin bootstraps a device, logs in with the email and password, and stores the resulting session together with the password, so the CLI can log in again on its own.
func (*App) PlaceOrder ¶
func (a *App) PlaceOrder(ctx context.Context, in PlaceOrderInput) (OrderResult, error)
PlaceOrder validates the input, plans the order and, unless it is a dry run, places it. Every read happens before the write; the first failure stops the call, and a failed write is never retried.
func (*App) Schedule ¶
Schedule returns the delivery dates the agency of the stored address offers for a product. An alias, a numeric material id or an empty product selects the product; empty means butano.
func (*App) SetAddress ¶
func (a *App) SetAddress(ctx context.Context, in SetAddressInput) (AddressView, error)
SetAddress resolves free text the way the web application does, stores the chosen address on the profile and returns its view.
func (*App) ShowAddress ¶
func (a *App) ShowAddress() (AddressView, error)
ShowAddress returns the stored address of the profile. It is a usage error when the profile has none.
type AuthStatus ¶
type AuthStatus struct {
// Profile is the profile the status describes.
Profile string `json:"profile"`
// LoggedIn reports whether a session (stored or from the environment) is
// available.
LoggedIn bool `json:"logged_in"`
// Source is "stored", "env", or empty when there is no session.
Source string `json:"source"`
// Email is the account email the CLI knows.
Email string `json:"email,omitempty"`
// FirstName is the account first name the CLI knows.
FirstName string `json:"first_name,omitempty"`
// PasswordStored reports whether automatic re-login is possible.
PasswordStored bool `json:"password_stored"`
// AddressSet reports whether the profile has a stored delivery address.
AddressSet bool `json:"address_set"`
// Checked reports whether the session was validated with Gigya.
Checked bool `json:"checked"`
// SessionValid is the result of the check, or nil when none was done.
SessionValid *bool `json:"session_valid,omitempty"`
}
AuthStatus is the state of the stored session of one profile.
func (AuthStatus) PrettyPrint ¶
func (s AuthStatus) PrettyPrint(w io.Writer) error
PrettyPrint writes the session status as human-readable text. It uses no ANSI colours, so NO_COLOR holds without any code.
type CatalogItem ¶
type CatalogItem struct {
// Product is the canonical alias, or empty for a material without one.
Product string `json:"product"`
// MaterialID is the numeric material id as the API names it.
MaterialID string `json:"material_id"`
// Name is the product name the agency uses.
Name string `json:"name"`
// Price is the price of one bottle.
Price float64 `json:"price"`
// PricePerKilo is the price per kilogram when the agency publishes it.
PricePerKilo float64 `json:"price_per_kilo,omitempty"`
// MaxQuantity is the maximum number of bottles in one order.
MaxQuantity int `json:"max_quantity"`
}
CatalogItem is one orderable product of the profile's agency.
type Deps ¶
type Deps struct {
// API talks to the Repsol API. New installs the app's signer on it.
API *repsolapi.Client
// Places talks to the address service.
Places *repsolapi.PlacesClient
// Gigya validates sessions and performs password logins.
Gigya *gigya.Client
// Store reads and writes config.toml and credentials.toml.
Store config.Store
// Profile is the profile the app works on.
Profile string
// Env carries the REPSOL_* environment values.
Env Env
// Now returns the current time. It is the clock the signature cache ages
// against.
Now func() time.Time
}
Deps are the dependencies of an App. API, Gigya, Store and Profile are required; Places is required by the address use cases; Now defaults to time.Now.
type DoctorCheck ¶
type DoctorCheck struct {
// Name is the check identifier: config, credentials, session, api, address
// or agency.
Name string `json:"name"`
// OK reports whether the probe succeeded.
OK bool `json:"ok"`
// Detail explains a failure, or carries a short success value such as the
// agency code.
Detail string `json:"detail,omitempty"`
}
DoctorCheck is the outcome of one doctor probe.
type DoctorReport ¶
type DoctorReport struct {
// OK reports whether every check succeeded.
OK bool `json:"ok"`
// Checks are the probes in their documented order.
Checks []DoctorCheck `json:"checks"`
}
DoctorReport is the outcome of every doctor probe.
type Env ¶
type Env struct {
// LoginToken is REPSOL_LOGIN_TOKEN.
LoginToken string
// GMID is REPSOL_GMID, the device cookie the login token is bound to.
GMID string
// Email is REPSOL_EMAIL, used for automatic re-login.
Email string
// Password is REPSOL_PASSWORD, used for automatic re-login.
Password string
}
Env holds the session and login values read from the REPSOL_* environment variables. They are never persisted.
type LogoutResult ¶
type LogoutResult struct {
// Profile is the profile whose credentials were removed.
Profile string `json:"profile"`
// Removed reports whether there were credentials to remove.
Removed bool `json:"removed"`
}
LogoutResult reports the effect of Logout.
type OrderResult ¶
type OrderResult struct {
// ID is the external id the write returned. It is empty on a dry run.
ID string `json:"id,omitempty"`
// DryRun reports whether the order was only planned.
DryRun bool `json:"dry_run"`
// OrderType is the write the cart check selected: SOLICITUDE or ORDER.
OrderType string `json:"order_type"`
// Product is the canonical alias, or empty for a material without one.
Product string `json:"product"`
// MaterialID is the numeric material id as the API names it.
MaterialID string `json:"material_id"`
// Quantity is the number of bottles.
Quantity int `json:"quantity"`
// UnitPrice is the price of one bottle.
UnitPrice float64 `json:"unit_price"`
// Total is the price of the whole order.
Total float64 `json:"total"`
// DeliveryDate is the promised delivery date as YYYY-MM-DD.
DeliveryDate string `json:"delivery_date"`
// Address is the delivery address summary.
Address string `json:"address"`
// Agency is the agency that will deliver the order.
Agency AgencyView `json:"agency"`
// Payment is always "on_delivery".
Payment string `json:"payment"`
// Note is a caveat for the owner, set when OrderType is SOLICITUDE.
Note string `json:"note,omitempty"`
}
OrderResult is the outcome of an order placement, or of a dry run of one.
func (OrderResult) PrettyPrint ¶
func (r OrderResult) PrettyPrint(w io.Writer) error
PrettyPrint writes the order result as human-readable text. It uses no ANSI colours, so NO_COLOR holds without any code.
type OrderSummary ¶
type OrderSummary struct {
// Reference is the eight-digit referenceSAC the history reports.
Reference string `json:"reference"`
// CreatedAt is the creation time as RFC 3339, or the raw value when it does
// not parse.
CreatedAt string `json:"created_at"`
// DeliveryDate is the promised delivery date as YYYY-MM-DD.
DeliveryDate string `json:"delivery_date"`
// Product is the canonical alias, or empty for a material without one.
Product string `json:"product,omitempty"`
// MaterialID is the numeric material id as the API names it.
MaterialID string `json:"material_id"`
// Quantity is the number of bottles.
Quantity int `json:"quantity"`
// UnitPrice is the price of one bottle.
UnitPrice float64 `json:"unit_price"`
// Total is the price of the whole order.
Total float64 `json:"total"`
// Address is the delivery address summary the history carries.
Address string `json:"address"`
// Agency is the agency of the order.
Agency AgencyView `json:"agency"`
// Pending reports whether the delivery is today or later in Madrid for the
// stored address. The history has no status field.
Pending bool `json:"pending"`
}
OrderSummary is one entry of the order history.
type PlaceOrderInput ¶
type PlaceOrderInput struct {
// Product is a product alias or a numeric material id.
Product string
// Quantity is the number of bottles.
Quantity int
// Date is the requested delivery date as YYYY-MM-DD, or empty.
Date string
// Pay is the payment method: empty or "delivery".
Pay string
// DryRun plans the order without writing it.
DryRun bool
// Force skips the pending-order guard.
Force bool
}
PlaceOrderInput is the user input of an order placement. Empty Product and zero Quantity are filled with the defaults by the caller; Date empty means the earliest offered date, and Pay empty means delivery.
type ProductInfo ¶
type ProductInfo struct {
// Alias is the canonical alias, or empty for a material id without one.
Alias string
// MaterialID is the numeric material id as the API names it.
MaterialID string
// MaxQty is the maximum number of bottles in one order.
MaxQty int
}
ProductInfo is a product resolved to its material id and per-order maximum.
func ResolveProduct ¶
func ResolveProduct(arg, provinceCode string) (ProductInfo, error)
ResolveProduct turns a product alias or a numeric material id into a ProductInfo. An empty argument means butano, which resolves to the Canarias material when the province is one of the two Canary Islands provinces.
type ScheduleView ¶
type ScheduleView struct {
// Product is the canonical alias, or empty for a material without one.
Product string `json:"product"`
// MaterialID is the numeric material id as the API names it.
MaterialID string `json:"material_id"`
// Mode is the schedule mode, empty when the API omits it.
Mode string `json:"mode,omitempty"`
// Warning reports whether the agency flags the schedule.
Warning bool `json:"warning"`
// Dates are the offered delivery dates, sorted ascending.
Dates []string `json:"dates"`
}
ScheduleView is the delivery schedule of one product for the stored address.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session signs the authenticated Repsol calls. It caches the signature Gigya issued and re-logs in once when the stored session is rejected and a password is available. A mutex serialises the cache and the re-login, because the MCP server runs tool calls concurrently.
func (*Session) Sign ¶
Sign implements repsolapi.Signer. The rules, in order:
- a cached signature younger than SignatureMaxAge is returned as is;
- the session comes from the environment when it carries a login token, otherwise from the stored credentials of the profile;
- no token and no password is not_logged_in;
- a rejected token is retried with exactly one password login when a password is available, and is session_expired otherwise;
- the login reuses the session gmid, bootstraps a new one when it has none, and saves the new token and gmid unless the session came from the environment.
type SetAddressInput ¶
type SetAddressInput struct {
// Text is the free-text address, such as "Calle Falsa 1, Pueblo".
Text string
// Floor is the floor of the delivery address.
Floor string
// Door is the door of the delivery address.
Door string
// Details are free-form delivery notes.
Details string
// Pick chooses the candidate when several match; zero means none chosen.
Pick int
}
SetAddressInput is the user input of an address resolution. Pick is the 1-based candidate to choose when the autocomplete returns several.