version 1
Build and Deploy / test-backend (push) Successful in 1m11s
Build and Deploy / lint-frontend (push) Successful in 21s
Build and Deploy / build-backend (push) Successful in 58s
Build and Deploy / deploy (push) Canceled after 0s
Build and Deploy / build-frontend (push) Canceled after 5m38s

This commit is contained in:
Ivan Rezchikov
2026-09-18 00:45:51 +03:00
commit 628dd6384b
149 changed files with 11478 additions and 0 deletions
@@ -0,0 +1,124 @@
package dictionary
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
"github.com/dusiburg/fancy-api/internal/model"
)
const freeDictionaryAPIURL = "https://freedictionaryapi.com/api/v1/entries"
type FreeDictionaryProvider struct {
client *http.Client
}
func NewFreeDictionaryProvider() *FreeDictionaryProvider {
return &FreeDictionaryProvider{client: &http.Client{Timeout: 5 * time.Second}}
}
type pronunciation struct {
Type string `json:"type"`
Text string `json:"text"`
Tags []string `json:"tags,omitempty"`
}
type apiEntry struct {
Language json.RawMessage `json:"language"`
PartOfSpeech string `json:"partOfSpeech"`
Pronunciations []pronunciation `json:"pronunciations,omitempty"`
Forms []model.Form `json:"forms,omitempty"`
Senses []model.Sense `json:"senses,omitempty"`
Synonyms []string `json:"synonyms,omitempty"`
Antonyms []string `json:"antonyms,omitempty"`
}
func (e apiEntry) toModel() model.Entry {
lang := model.Language(model.LangEN)
if len(e.Language) > 0 {
if e.Language[0] == '{' {
var langInfo struct{ Code model.Language `json:"code"` }
if json.Unmarshal(e.Language, &langInfo) == nil {
lang = langInfo.Code
}
} else {
lang = model.Language(e.Language)
}
}
return model.Entry{
Language: lang,
PartOfSpeech: model.PartOfSpeech(e.PartOfSpeech),
IPA: firstIPA(e.Pronunciations),
Forms: e.Forms,
Senses: e.Senses,
Synonyms: e.Synonyms,
Antonyms: e.Antonyms,
}
}
func firstIPA(pronunciations []pronunciation) string {
for _, p := range pronunciations {
if p.Type == "ipa" && p.Text != "" {
return p.Text
}
}
return ""
}
type apiResponse struct {
Word string `json:"word"`
Entries []apiEntry `json:"entries"`
}
func (r apiResponse) toModel() *model.DictionaryResult {
entries := make([]model.Entry, 0, len(r.Entries))
for _, e := range r.Entries {
entries = append(entries, e.toModel())
}
return &model.DictionaryResult{Word: r.Word, Entries: entries}
}
func (p *FreeDictionaryProvider) Lookup(ctx context.Context, word string, lang model.Language) (*model.DictionaryResult, error) {
if _, ok := model.ParseLanguage(string(lang)); !ok {
return nil, fmt.Errorf("%w: %s", ErrUnsupportedLanguage, lang)
}
reqURL := fmt.Sprintf("%s/%s/%s", freeDictionaryAPIURL, lang, url.PathEscape(word))
req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqURL, nil)
if err != nil {
return nil, fmt.Errorf("build request: %w", err)
}
resp, err := p.client.Do(req)
if err != nil {
return nil, fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusOK:
case http.StatusNotFound:
return nil, ErrWordNotFound
default:
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
var payload apiResponse
if err := json.NewDecoder(resp.Body).Decode(&payload); err != nil {
return nil, fmt.Errorf("decode response: %w", err)
}
if len(payload.Entries) == 0 {
return nil, ErrWordNotFound
}
result := payload.toModel()
result.Provider = "Free Dictionary"
return result, nil
}