Skip to content

Commit

Permalink
add client
Browse files Browse the repository at this point in the history
  • Loading branch information
franzmueller committed Aug 16, 2023
1 parent 7b711d1 commit 2fdb0c7
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
53 changes: 53 additions & 0 deletions lib/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2023 InfAI (CC SES)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package client

import (
"encoding/json"
"fmt"
"github.com/SENERGY-Platform/import-repository/lib/api"
"io"
"net/http"
)

type Interface = api.Controller

type Client struct {
baseUrl string
}

func NewClient(baseUrl string) Interface {
return &Client{baseUrl: baseUrl}
}

func do[T any](req *http.Request) (result T, err error, code int) {
resp, err := http.DefaultClient.Do(req)
if err != nil {
return result, err, http.StatusInternalServerError
}
defer resp.Body.Close()
if resp.StatusCode > 299 {
temp, _ := io.ReadAll(resp.Body) //read error response end ensure that resp.Body is read to EOF
return result, fmt.Errorf("unexpected statuscode %v: %v", resp.StatusCode, string(temp)), resp.StatusCode
}
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
_, _ = io.ReadAll(resp.Body) //ensure resp.Body is read to EOF
return result, err, http.StatusInternalServerError
}
return
}
71 changes: 71 additions & 0 deletions lib/client/import-types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2023 InfAI (CC SES)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package client

import (
"bytes"
"encoding/json"
"errors"
"github.com/SENERGY-Platform/import-repository/lib/auth"
"github.com/SENERGY-Platform/import-repository/lib/model"
"net/http"
)

func (c Client) ReadImportType(id string, jwt auth.Token) (result model.ImportType, err error, errCode int) {
req, err := http.NewRequest(http.MethodGet, c.baseUrl+"/import-types/"+id, nil)
req.Header.Set("Authorization", "Bearer "+jwt.Jwt())
return do[model.ImportType](req)
}

// CheckAccessToImportType NOT IMPLEMENTED!
func (c Client) CheckAccessToImportType(jwt auth.Token, id string, action model.AuthAction) (err error, code int) {
return errors.New("not implemented"), http.StatusInternalServerError
}

func (c Client) CreateImportType(importType model.ImportType, jwt auth.Token) (result model.ImportType, err error, code int) {
b, err := json.Marshal(importType)
if err != nil {
return result, err, http.StatusBadRequest
}
req, err := http.NewRequest(http.MethodPost, c.baseUrl+"/import-types", bytes.NewBuffer(b))
req.Header.Set("Authorization", "Bearer "+jwt.Jwt())
return do[model.ImportType](req)
}

func (c Client) SetImportType(importType model.ImportType, jwt auth.Token) (err error, code int) {
b, err := json.Marshal(importType)
if err != nil {
return err, http.StatusBadRequest
}
req, err := http.NewRequest(http.MethodPost, c.baseUrl+"/import-types", bytes.NewBuffer(b))
req.Header.Set("Authorization", "Bearer "+jwt.Jwt())
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err, http.StatusInternalServerError
}
return nil, resp.StatusCode
}

func (c Client) DeleteImportType(id string, jwt auth.Token) (err error, errCode int) {
req, err := http.NewRequest(http.MethodDelete, c.baseUrl+"/import-types/"+id, nil)
req.Header.Set("Authorization", "Bearer "+jwt.Jwt())
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err, http.StatusInternalServerError
}
return nil, resp.StatusCode
}

0 comments on commit 2fdb0c7

Please sign in to comment.