edsm-uploader

A platform independent uploader of Elite Dangerous journal files to EDSM
git clone git://archive.git.mtrnord.blog/MTRNord/edsm-uploader.git
Log | Files | Refs | LICENSE

edsm.go (2484B)


      1 package edsm
      2 
      3 import (
      4 	"bytes"
      5 	"encoding/json"
      6 	"io"
      7 	"log"
      8 	"os"
      9 
     10 	"github.com/MTRNord/edsm_uploader/datatypes"
     11 	"github.com/hashicorp/go-retryablehttp"
     12 	"github.com/pkg/errors"
     13 )
     14 
     15 type EDSM struct {
     16 	commanderName string
     17 	apiKey        string
     18 	client        *retryablehttp.Client
     19 	logger        *log.Logger
     20 }
     21 
     22 func NewEDSM(commanderName string, apiKey string, logger *log.Logger) *EDSM {
     23 	retryClient := retryablehttp.NewClient()
     24 	retryClient.RetryMax = 10
     25 	retryClient.Logger = log.New(os.Stderr, "retryablehttp: ", log.LstdFlags)
     26 	return &EDSM{
     27 		commanderName: commanderName,
     28 		apiKey:        apiKey,
     29 		logger:        logger,
     30 		client:        retryClient,
     31 	}
     32 }
     33 
     34 type RequestJSON struct {
     35 	CommanderName   string `json:"commanderName"`
     36 	ApiKey          string `json:"apiKey"`
     37 	Software        string `json:"fromSoftware"`
     38 	SoftwareVersion string `json:"fromSoftwareVersion"`
     39 	GameVersion     string `json:"fromGameVersion"`
     40 	GameBuild       string `json:"fromGameBuild"`
     41 	Message         string `json:"message"`
     42 }
     43 
     44 func (e *EDSM) SendJournalLine(fileHeader *datatypes.FileHeader, journalLine string) error {
     45 	// Send HTTP Post request to https://www.edsm.net/api-journal-v1
     46 	// If the response is less then 200 then return an error.
     47 
     48 	// Build the JSON request.
     49 	requestJSON := RequestJSON{
     50 		CommanderName:   e.commanderName,
     51 		ApiKey:          e.apiKey,
     52 		Software:        "github.com/MTRNord/edsm_uploader",
     53 		SoftwareVersion: "0.1.0",
     54 		GameVersion:     fileHeader.GameVersion,
     55 		GameBuild:       fileHeader.Build,
     56 		Message:         journalLine,
     57 	}
     58 	// Convert struct to json
     59 	b, err := json.Marshal(requestJSON)
     60 	if err != nil {
     61 		return errors.WithStack(err)
     62 	}
     63 
     64 	resp, err := e.client.Post("https://www.edsm.net/api-journal-v1", "application/json", bytes.NewBuffer(b))
     65 	if err != nil {
     66 		return errors.WithStack(err)
     67 	}
     68 	defer resp.Body.Close()
     69 
     70 	if resp.StatusCode < 200 {
     71 		// Create error from response
     72 		bodyBytes, err := io.ReadAll(resp.Body)
     73 		if err != nil {
     74 			return errors.WithStack(err)
     75 		}
     76 		bodyString := string(bodyBytes)
     77 		return &EDSMError{
     78 			Status:  resp.StatusCode,
     79 			Message: bodyString,
     80 		}
     81 	} /* else {
     82 		// Log the response
     83 		bodyBytes, err := io.ReadAll(resp.Body)
     84 		if err != nil {
     85 			return errors.WithStack(err)
     86 		}
     87 		bodyString := string(bodyBytes)
     88 		e.logger.Printf("Response from EDSM: %s", bodyString)
     89 	} */
     90 
     91 	return nil
     92 }
     93 
     94 type EDSMError struct {
     95 	Status  int
     96 	Message string
     97 }
     98 
     99 func (e *EDSMError) Error() string {
    100 	return e.Message
    101 }