Skip to content

Commit

Permalink
refactor utils DoGet DoPost,....
Browse files Browse the repository at this point in the history
  • Loading branch information
hisham waleed karam committed Apr 29, 2018
1 parent a171eaa commit 60fec99
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 152 deletions.
7 changes: 6 additions & 1 deletion about.go
Expand Up @@ -12,7 +12,12 @@ type AboutService interface {
//err is an error if error occurredÎ
func (g *GeoServer) IsRunning() (running bool, err error) {
targetURL := g.ParseURL("rest", "about", "version")
response, responseCode := g.DoGet(targetURL, jsonType, nil)
httpRequest := HTTPRequest{
URL: targetURL,
Method: getMethod,
Accept: jsonType,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
g.logger.Error(string(response))
err = g.GetError(responseCode, response)
Expand Down
35 changes: 30 additions & 5 deletions coverage_stores.go
Expand Up @@ -42,7 +42,13 @@ type CoverageStoreRequestBody struct {
// err is an error if error occurred else err is nil
func (g *GeoServer) GetCoverageStores(workspaceName string) (coverageStores []*Resource, err error) {
targetURL := g.ParseURL("rest", "workspaces", workspaceName, "coveragestores")
response, responseCode := g.DoGet(targetURL, jsonType, nil)
httpRequest := HTTPRequest{
Method: getMethod,
Accept: jsonType,
URL: targetURL,
Query: nil,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
g.logger.Error(string(response))
coverageStores = nil
Expand All @@ -66,8 +72,14 @@ func (g *GeoServer) CreateCoverageStore(workspaceName string, coverageStore Cove
CoverageStore: &coverageStore,
}
serializedData, _ := g.SerializeStruct(data)
g.logger.Warn(string(serializedData))
response, responseCode := g.DoPost(targetURL, bytes.NewBuffer(serializedData), jsonType+"; charset=utf-8", jsonType)
httpRequest := HTTPRequest{
Method: postMethod,
Data: bytes.NewBuffer(serializedData),
DataType: jsonType + "; charset=utf-8",
Accept: jsonType,
URL: targetURL,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusCreated {
g.logger.Error(string(response))
created = false
Expand All @@ -83,7 +95,14 @@ func (g *GeoServer) UpdateCoverageStore(workspaceName string, coverageStore Cove
targetURL := g.ParseURL("rest", "workspaces", workspaceName, "coveragestores", coverageStore.Name)
data := CoverageStoreRequestBody{CoverageStore: &coverageStore}
serializedData, _ := g.SerializeStruct(data)
response, responseCode := g.DoPut(targetURL, bytes.NewBuffer(serializedData), jsonType, jsonType)
httpRequest := HTTPRequest{
Method: putMethod,
Data: bytes.NewBuffer(serializedData),
DataType: jsonType,
Accept: jsonType,
URL: targetURL,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
g.logger.Error(string(response))
modified = false
Expand All @@ -97,7 +116,13 @@ func (g *GeoServer) UpdateCoverageStore(workspaceName string, coverageStore Cove
// DeleteCoverageStore delete coverage store from geoserver else return error
func (g *GeoServer) DeleteCoverageStore(workspaceName string, coverageStore string, recurse bool) (deleted bool, err error) {
targetURL := g.ParseURL("rest", "workspaces", workspaceName, "coveragestores", coverageStore)
response, responseCode := g.DoDelete(targetURL, jsonType, map[string]string{"recurse": strconv.FormatBool(recurse)})
httpRequest := HTTPRequest{
Method: deleteMethod,
Accept: jsonType,
URL: targetURL,
Query: map[string]string{"recurse": strconv.FormatBool(recurse)},
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
g.logger.Error(string(response))
deleted = false
Expand Down
42 changes: 37 additions & 5 deletions datastores.go
Expand Up @@ -56,7 +56,13 @@ type DatastoreConnectionParams struct {
// DatastoreExists checks if a datastore exists in a workspace else return error
func (g *GeoServer) DatastoreExists(workspaceName string, datastoreName string, quietOnNotFound bool) (exists bool, err error) {
targetURL := g.ParseURL("rest", "workspaces", workspaceName, "datastores", datastoreName)
response, responseCode := g.DoGet(targetURL, jsonType, map[string]string{"quietOnNotFound": strconv.FormatBool(quietOnNotFound)})
httpRequest := HTTPRequest{
Method: getMethod,
Accept: jsonType,
URL: targetURL,
Query: map[string]string{"quietOnNotFound": strconv.FormatBool(quietOnNotFound)},
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
exists = false
err = g.GetError(responseCode, response)
Expand All @@ -70,7 +76,13 @@ func (g *GeoServer) DatastoreExists(workspaceName string, datastoreName string,
func (g *GeoServer) GetDatastores(workspaceName string) (datastores []*Resource, err error) {
//TODO: check if workspace exist before creating it
targetURL := g.ParseURL("rest", "workspaces", workspaceName, "datastores")
response, responseCode := g.DoGet(targetURL, jsonType, nil)
httpRequest := HTTPRequest{
Method: getMethod,
Accept: jsonType,
URL: targetURL,
Query: nil,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
datastores = nil
err = g.GetError(responseCode, response)
Expand All @@ -90,7 +102,13 @@ func (g *GeoServer) GetDatastores(workspaceName string) (datastores []*Resource,
func (g *GeoServer) GetDatastoreDetails(workspaceName string, datastoreName string) (datastore *Datastore, err error) {
//TODO: check if workspace exist before creating it
targetURL := g.ParseURL("rest", "workspaces", workspaceName, "datastores", datastoreName)
response, responseCode := g.DoGet(targetURL, jsonType, nil)
httpRequest := HTTPRequest{
Method: getMethod,
Accept: jsonType,
URL: targetURL,
Query: nil,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
datastore = &Datastore{}
err = g.GetError(responseCode, response)
Expand Down Expand Up @@ -131,7 +149,15 @@ func (g *GeoServer) CreateDatastore(datastoreConnection DatastoreConnection, wor
datastoreConnection.Type)
targetURL := g.ParseURL("rest", "workspaces", workspaceName, "datastores")
data := bytes.NewReader([]byte(xml))
response, responseCode := g.DoPost(targetURL, data, xmlType, jsonType)
httpRequest := HTTPRequest{
Method: postMethod,
Accept: jsonType,
Data: data,
DataType: xmlType,
URL: targetURL,
Query: nil,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusCreated {
g.logger.Warn(string(response))
created = false
Expand All @@ -146,7 +172,13 @@ func (g *GeoServer) CreateDatastore(datastoreConnection DatastoreConnection, wor
// DeleteDatastore deletes a datastore from geoserver else return error
func (g *GeoServer) DeleteDatastore(workspaceName string, datastoreName string, recurse bool) (deleted bool, err error) {
targetURL := g.ParseURL("rest", "workspaces", workspaceName, "datastores", datastoreName)
response, responseCode := g.DoDelete(targetURL, jsonType, map[string]string{"recurse": strconv.FormatBool(recurse)})
httpRequest := HTTPRequest{
Method: deleteMethod,
Accept: jsonType,
URL: targetURL,
Query: map[string]string{"recurse": strconv.FormatBool(recurse)},
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
g.logger.Warn(string(response))
deleted = false
Expand Down
24 changes: 21 additions & 3 deletions feature_types.go
Expand Up @@ -121,7 +121,13 @@ func (g *GeoServer) GetFeatureTypes(workspaceName string, datastoreName string)
datastoreName = fmt.Sprintf("datastores/%s/featuretypes", datastoreName)
}
targetURL := g.ParseURL("rest", workspaceName, datastoreName)
response, responseCode := g.DoGet(targetURL, jsonType, nil)
httpRequest := HTTPRequest{
Method: getMethod,
Accept: jsonType,
URL: targetURL,
Query: nil,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
featureTypes = nil
err = g.GetError(responseCode, response)
Expand All @@ -144,7 +150,13 @@ func (g *GeoServer) DeleteFeatureType(workspaceName string, datastoreName string
datastoreName = fmt.Sprintf("datastores/%s/", datastoreName)
}
targetURL := g.ParseURL("rest", workspaceName, datastoreName, "featuretypes", featureTypeName)
response, responseCode := g.DoDelete(targetURL, jsonType, map[string]string{"recurse": strconv.FormatBool(recurse)})
httpRequest := HTTPRequest{
Method: deleteMethod,
Accept: jsonType,
URL: targetURL,
Query: map[string]string{"recurse": strconv.FormatBool(recurse)},
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
deleted = false
err = g.GetError(responseCode, response)
Expand All @@ -164,7 +176,13 @@ func (g *GeoServer) GetFeatureType(workspaceName string, datastoreName string, f
datastoreName = fmt.Sprintf("datastores/%s/featuretypes", datastoreName)
}
targetURL := g.ParseURL("rest", workspaceName, datastoreName, featureTypeName)
response, responseCode := g.DoGet(targetURL, jsonType, nil)
httpRequest := HTTPRequest{
Method: getMethod,
Accept: jsonType,
URL: targetURL,
Query: nil,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
featureType = nil
err = g.GetError(responseCode, response)
Expand Down
4 changes: 2 additions & 2 deletions geoserver.go
Expand Up @@ -49,8 +49,8 @@ func (g *GeoServer) GetGeoserverRequest(
method string,
accept string,
data io.Reader,
contentType string) (request *http.Request, err error) {
request, err = http.NewRequest(method, targetURL, data)
contentType string) (request *http.Request) {
request, _ = http.NewRequest(method, targetURL, data)
if data != nil {
request.Header.Set(contentTypeHeader, contentType)
}
Expand Down
3 changes: 1 addition & 2 deletions geoserver_test.go
Expand Up @@ -31,7 +31,6 @@ func TestSetLogger(t *testing.T) {
}
func TestGetGeoserverRequest(t *testing.T) {
gsCatalog := GetCatalog("", "", "")
request, err := gsCatalog.GetGeoserverRequest("", getMethod, jsonType, bytes.NewBuffer(make([]byte, 0, 0)), jsonType)
assert.Nil(t, err)
request := gsCatalog.GetGeoserverRequest("", getMethod, jsonType, bytes.NewBuffer(make([]byte, 0, 0)), jsonType)
assert.NotNil(t, request)
}
44 changes: 39 additions & 5 deletions layers.go
Expand Up @@ -93,7 +93,15 @@ func (g *GeoServer) UploadShapeFile(fileURI string, workspaceName string, datast
if !exists {
g.CreateWorkspace(workspaceName)
}
response, responseCode := g.DoPut(targetURL, bytes.NewBuffer(shapeFileBinary), zipType, "")
httpRequest := HTTPRequest{
Method: putMethod,
Accept: jsonType,
Data: bytes.NewBuffer(shapeFileBinary),
DataType: zipType,
URL: targetURL,
Query: nil,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusCreated {
g.logger.Error(string(response))
uploaded = false
Expand All @@ -112,7 +120,13 @@ func (g *GeoServer) GetLayers(workspaceName string) (layers []*Resource, err err
workspaceName = fmt.Sprintf("workspaces/%s/", workspaceName)
}
targetURL := g.ParseURL("rest", workspaceName, "layers")
response, responseCode := g.DoGet(targetURL, jsonType, nil)
httpRequest := HTTPRequest{
Method: getMethod,
Accept: jsonType,
URL: targetURL,
Query: nil,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
g.logger.Error(string(response))
layers = nil
Expand All @@ -136,7 +150,13 @@ func (g *GeoServer) GetLayer(workspaceName string, layerName string) (layer *Lay
workspaceName = fmt.Sprintf("workspaces/%s/", workspaceName)
}
targetURL := g.ParseURL("rest", workspaceName, "layers", layerName)
response, responseCode := g.DoGet(targetURL, jsonType, nil)
httpRequest := HTTPRequest{
Method: getMethod,
Accept: jsonType,
URL: targetURL,
Query: nil,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
g.logger.Error(string(response))
layer = &Layer{}
Expand All @@ -161,7 +181,15 @@ func (g *GeoServer) UpdateLayer(workspaceName string, layerName string, layer La
data := LayerRequestBody{Layer: layer}

serializedLayer, _ := g.SerializeStruct(data)
response, responseCode := g.DoPut(targetURL, bytes.NewBuffer(serializedLayer), jsonType, jsonType)
httpRequest := HTTPRequest{
Method: putMethod,
Accept: jsonType,
Data: bytes.NewBuffer(serializedLayer),
DataType: jsonType,
URL: targetURL,
Query: nil,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
g.logger.Error(string(response))
modified = false
Expand All @@ -179,7 +207,13 @@ func (g *GeoServer) DeleteLayer(workspaceName string, layerName string, recurse
workspaceName = fmt.Sprintf("workspaces/%s/", workspaceName)
}
targetURL := g.ParseURL("rest", workspaceName, "layers", layerName)
response, responseCode := g.DoDelete(targetURL, jsonType, map[string]string{"recurse": strconv.FormatBool(recurse)})
httpRequest := HTTPRequest{
Method: deleteMethod,
Accept: jsonType,
URL: targetURL,
Query: map[string]string{"recurse": strconv.FormatBool(recurse)},
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
g.logger.Error(string(response))
deleted = false
Expand Down
44 changes: 39 additions & 5 deletions styles.go
Expand Up @@ -55,7 +55,13 @@ func (g *GeoServer) GetStyles(workspaceName string) (styles []*Resource, err err
workspaceName = fmt.Sprintf("workspaces/%s/", workspaceName)
}
targetURL := fmt.Sprintf("%srest/%sstyles", g.ServerURL, workspaceName)
response, responseCode := g.DoGet(targetURL, jsonType, nil)
httpRequest := HTTPRequest{
Method: getMethod,
Accept: jsonType,
URL: targetURL,
Query: nil,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
g.logger.Error(string(response))
styles = nil
Expand All @@ -79,7 +85,13 @@ func (g *GeoServer) GetStyle(workspaceName string, styleName string) (style *Sty
workspaceName = fmt.Sprintf("workspaces/%s/", workspaceName)
}
targetURL := g.ParseURL("rest", workspaceName, "styles", styleName)
response, responseCode := g.DoGet(targetURL, jsonType, nil)
httpRequest := HTTPRequest{
Method: getMethod,
Accept: jsonType,
URL: targetURL,
Query: nil,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
g.logger.Error(string(response))
style = &Style{}
Expand All @@ -102,7 +114,15 @@ func (g *GeoServer) CreateStyle(workspaceName string, styleName string) (created
var style = Style{Name: styleName, Filename: fmt.Sprintf("%s.sld", styleName)}
serializedStyle, _ := g.SerializeStruct(StyleRequestBody{Style: &style})
data := bytes.NewBuffer(serializedStyle)
response, responseCode := g.DoPost(targetURL, data, jsonType, jsonType)
httpRequest := HTTPRequest{
Method: postMethod,
Accept: jsonType,
Data: data,
DataType: jsonType,
URL: targetURL,
Query: nil,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusCreated {
g.logger.Error(string(response))
created = false
Expand All @@ -120,7 +140,15 @@ func (g *GeoServer) UploadStyle(data io.Reader, workspaceName string, styleName
workspaceName = fmt.Sprintf("workspaces/%s/", workspaceName)
}
targetURL := g.ParseURL("rest", workspaceName, "styles", styleName)
response, responseCode := g.DoPut(targetURL, data, sldType, jsonType)
httpRequest := HTTPRequest{
Method: putMethod,
Accept: jsonType,
Data: data,
DataType: sldType,
URL: targetURL,
Query: nil,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
g.logger.Error(string(response))
success = false
Expand All @@ -138,7 +166,13 @@ func (g *GeoServer) DeleteStyle(workspaceName string, styleName string, purge bo
workspaceName = fmt.Sprintf("workspaces/%s/", workspaceName)
}
targetURL := g.ParseURL("rest", workspaceName, "styles", styleName)
response, responseCode := g.DoDelete(targetURL, jsonType, map[string]string{"purge": strconv.FormatBool(purge)})
httpRequest := HTTPRequest{
Method: deleteMethod,
Accept: jsonType,
URL: targetURL,
Query: map[string]string{"purge": strconv.FormatBool(purge)},
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
g.logger.Error(string(response))
deleted = false
Expand Down

0 comments on commit 60fec99

Please sign in to comment.