Skip to content
Snippets Groups Projects
Select Git revision
  • c02dc41938e832d51f65c907008029a3ae4c37c3
  • main default protected
2 results

services.go

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    services.go 2.81 KiB
    package services
    
    import (
        "encoding/json"
        "net/http"
        "time"
        "assignment1/models"
    )
    
    // FetchBooksByLanguage retrieves books from the Gutendex API by language.
    func FetchBooksByLanguage(language string) (*models.GutenbergResponse, error) {
        url := "http://129.241.150.113:8000/books?languages=" + language
        resp, err := http.Get(url)
        if err != nil {
            return nil, err
        }
        defer resp.Body.Close()
    
        var booksResponse models.GutenbergResponse
        if err := json.NewDecoder(resp.Body).Decode(&booksResponse); err != nil {
            return nil, err
        }
    
        return &booksResponse, nil
    }
    
    
    // FetchCountriesByLanguage retrieves countries for a given language code.
    func FetchCountriesByLanguage(language string) ([]models.CountryInfo, error) {
        url := "http://129.241.150.113:3000/language2countries/" + language
        resp, err := http.Get(url)
        if err != nil {
            return nil, err
        }
        defer resp.Body.Close()
    
        var countriesResponse []models.CountryInfo // Adjusted to expect an array
        if err := json.NewDecoder(resp.Body).Decode(&countriesResponse); err != nil {
            return nil, err
        }
    
        return countriesResponse, nil
    }
    
    
    // FetchPopulationByCountryCode retrieves population for a given country code.
    func FetchPopulationByCountryCode(isoCode string) (int64, error) {
        url := "http://129.241.150.113:8080/v3.1/alpha/" + isoCode
        resp, err := http.Get(url)
        if err != nil {
            return 0, err
        }
        defer resp.Body.Close()
    
        var countryResponse []models.CountryResponse // Assuming CountryResponse is an array
        if err := json.NewDecoder(resp.Body).Decode(&countryResponse); err != nil {
            return 0, err
        }
    
        if len(countryResponse) > 0 {
            return countryResponse[0].Population, nil
        }
    
        return 0, nil // Or return an error if no country is found
    }
    
    // CalculateUniqueAuthors calculates the number of unique authors from a list of books.
    func CalculateUniqueAuthors(books []models.GutenbergBook) int {
        authorsSet := make(map[string]struct{})
        for _, book := range books {
            for _, author := range book.Authors {
                authorsSet[author.Name] = struct{}{}
            }
        }
        return len(authorsSet)
    }
    
    func CheckServiceAvailability(serviceURL string) int {
        resp, err := http.Get(serviceURL)
        if err != nil {
            // If there's an error, might want to return a status code indicating the service is unavailable
            return http.StatusServiceUnavailable
        }
        defer resp.Body.Close()
    
        // Return the HTTP status code of the response
        return resp.StatusCode
    }
    
    
    // Assuming you have a global variable to track the start time of your service.
    var serviceStartTime = time.Now()
    
    // GetUptime calculates the uptime of the service in seconds.
    func GetUptime() int64 {
        return int64(time.Since(serviceStartTime).Seconds())
    }