Skip to content

Commit

Permalink
marked suggestions
Browse files Browse the repository at this point in the history
Signed-off-by: NitishKumar06 <[email protected]>
  • Loading branch information
nitishfy committed Sep 24, 2023
1 parent a571532 commit 497bdfb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
22 changes: 22 additions & 0 deletions obs/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package obs

import (
"encoding/xml"
"fmt"
)

type Status struct {
XMLName xml.Name `json:"status" xml:"status"`
Code string `json:"code" xml:"code,attr"`
Summary string `json:"summary" xml:"summary"`
}

type APIError struct {
HTTPStatusCode int
XMLStatusCode string
Message string
}

func (e *APIError) Error() string {
return fmt.Sprintf("HTTP status %d: %s (%s)", e.HTTPStatusCode, e.XMLStatusCode, e.Message)
}
39 changes: 16 additions & 23 deletions obs/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import (
"bytes"
"encoding/xml"
"fmt"
"log"
"net/http"
"path"
"net/url"
)

type Project struct {
Expand Down Expand Up @@ -97,33 +98,21 @@ type RepositoryPath struct {
Repository string `json:"repository" xml:"repository,attr"`
}

type Status struct {
XMLName xml.Name `json:"status" xml:"status"`
Code string `json:"code" xml:"code,attr"`
Summary string `json:"summary" xml:"summary"`
}

type APIError struct {
HTTPStatusCode int
XMLStatusCode string
Message string
}

func (e *APIError) Error() string {
return fmt.Sprintf("HTTP status %d: %s (%s)", e.HTTPStatusCode, e.XMLStatusCode, e.Message)
}

func (c Client) CreateProject(project *Project) error {
xmlData, err := xml.MarshalIndent(project, "", " ")
if err != nil {
return err
}

urlPath := path.Join(c.APIURL, "source", project.Name, "_meta")
urlPath, err := url.JoinPath(c.APIURL, "source", project.Name, "_meta")
if err != nil {
log.Fatal(err)
}

req, err := http.NewRequest("PUT", urlPath, bytes.NewBuffer(xmlData))
if err != nil {
return &APIError{
HTTPStatusCode: http.StatusInternalServerError,
HTTPStatusCode: 0,
XMLStatusCode: "",
Message: fmt.Sprintf("failed to generate a new request: %v", err),
}
Expand All @@ -135,7 +124,7 @@ func (c Client) CreateProject(project *Project) error {
resp, err := c.Client.Do(req)
if err != nil {
return &APIError{
HTTPStatusCode: resp.StatusCode,
HTTPStatusCode: 0,
XMLStatusCode: "",
Message: fmt.Sprintf("failed to make a new request: %v", err),
}
Expand All @@ -162,11 +151,15 @@ func (c Client) CreateProject(project *Project) error {
}

func (c Client) DeleteProject(project *Project) error {
urlPath := path.Join(c.APIURL, "source", project.Name)
urlPath, err := url.JoinPath(c.APIURL, "source", project.Name)
if err != nil {
log.Fatal(err)
}

req, err := http.NewRequest("DELETE", urlPath, nil)
if err != nil {
return &APIError{
HTTPStatusCode: http.StatusInternalServerError,
HTTPStatusCode: 0,
XMLStatusCode: "",
Message: fmt.Sprintf("failed to generate a new request: %v", err),
}
Expand All @@ -178,7 +171,7 @@ func (c Client) DeleteProject(project *Project) error {
resp, err := c.Client.Do(req)
if err != nil {
return &APIError{
HTTPStatusCode: resp.StatusCode,
HTTPStatusCode: 0,
XMLStatusCode: "",
Message: fmt.Sprintf("failed to make a new request: %v", err),
}
Expand Down

0 comments on commit 497bdfb

Please sign in to comment.