Skip to content

Commit

Permalink
Merge pull request #15 from gowizzard/development
Browse files Browse the repository at this point in the history
feat: Add new functions to requests package.
  • Loading branch information
gowizzard authored Apr 9, 2024
2 parents 0ae6c6b + b42fdaa commit 25b17e5
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 0 deletions.
81 changes: 81 additions & 0 deletions internal/requests/containers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package request

import (
"context"
"encoding/json"
"net/http"
"path"
"time"
)

// ContainersResponse is to decode the response from the containers endpoint.
type ContainersResponse struct {
Id string `json:"Id"`
}

// ContainerResponse is to decode the response from the container endpoint.
type ContainerResponse struct {
Created time.Time `json:"Created"`
Id string `json:"Id"`
Image string `json:"Image"`
Name string `json:"Name"`
RestartCount int `json:"RestartCount"`
State struct {
StartedAt time.Time `json:"StartedAt"`
Status string `json:"Status"`
} `json:"State"`
}

// Containers are to get the containers information from the docker unix socket.
func Containers() (decode []ContainersResponse, err error) {

ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

c := config{
Path: path.Join("containers", "json"),
Method: http.MethodGet,
Context: ctx,
}

response, err := c.send()
if err != nil {
return nil, err
}
defer response.Body.Close()

err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return nil, err
}

return decode, nil

}

// Container is to get the container information from the docker unix socket.
func Container(id string) (decode ContainerResponse, err error) {

ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

c := config{
Path: path.Join("containers", id, "json"),
Method: http.MethodGet,
Context: ctx,
}

response, err := c.send()
if err != nil {
return ContainerResponse{}, err
}
defer response.Body.Close()

err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return ContainerResponse{}, err
}

return decode, nil

}
40 changes: 40 additions & 0 deletions internal/requests/images.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package request

import (
"context"
"encoding/json"
"net/http"
"path"
)

// ImageResponse is to decode the response from the image endpoint.
type ImageResponse struct {
RepoTags []string `json:"RepoTags"`
}

// Image is to get the image from the docker unix socket.
func Image(id string) (decode ImageResponse, err error) {

ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

c := config{
Path: path.Join("images", id, "json"),
Method: http.MethodGet,
Context: ctx,
}

response, err := c.send()
if err != nil {
return ImageResponse{}, err
}
defer response.Body.Close()

err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return ImageResponse{}, err
}

return decode, nil

}
42 changes: 42 additions & 0 deletions internal/requests/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package request

import (
"context"
"encoding/json"
"net/http"
)

// VersionResponse is to decode the response from the version endpoint.
type VersionResponse struct {
Version string `json:"Version"`
ApiVersion string `json:"ApiVersion"`
Os string `json:"Os"`
Arch string `json:"Arch"`
}

// Version is to get the version information from the docker unix socket.
func Version() (decode VersionResponse, err error) {

ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

c := config{
Path: "version",
Method: http.MethodGet,
Context: ctx,
}

response, err := c.send()
if err != nil {
return VersionResponse{}, err
}
defer response.Body.Close()

err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return VersionResponse{}, err
}

return decode, nil

}

0 comments on commit 25b17e5

Please sign in to comment.