-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from gowizzard/development
feat: Add new functions to requests package.
- Loading branch information
Showing
3 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |