Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OGC API - Features - Part 4: Create #127

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/pg_featureserv.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ WriteTimeoutSec = 30
# Publish functions from these schemas (default is publish postgisftw)
# FunctionIncludes = [ "postgisftw", "schema2" ]

# Allow write changes to database. Default is to read only.
# AllowWrite = false

[Paging]
# The default number of features in a response
LimitDefault = 20
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/gorilla/mux v1.7.3
github.com/jackc/pgtype v1.0.2
github.com/jackc/pgx/v4 v4.1.2
github.com/paulmach/orb v0.7.1
github.com/pborman/getopt v0.0.0-20190409184431-ee0cd42419d3
github.com/sirupsen/logrus v1.7.0
github.com/spf13/viper v1.6.1
Expand Down
53 changes: 42 additions & 11 deletions go.sum

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions hugo/content/installation/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ WriteTimeoutSec = 30
# Publish functions from these schemas (default is publish postgisftw)
# FunctionIncludes = [ "postgisftw", "schema2" ]

# Allow write changes to database. Default is to read only.
# AllowWrite = false

[Paging]
# The default number of features in a response
LimitDefault = 20
Expand Down
38 changes: 24 additions & 14 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
ParamProperties = "properties"
ParamSortBy = "sortby"
ParamTransform = "transform"
ParamType = "type"

OrderByDirSep = ":"
OrderByDirD = "d"
Expand All @@ -70,20 +71,22 @@ const (
)

const (
ErrMsgEncoding = "Error encoding response"
ErrMsgLoadCollections = "Unable to access Collections"
ErrMsgCollectionNotFound = "Collection not found: %v"
ErrMsgCollectionAccess = "Unable to access Collection: %v"
ErrMsgFeatureNotFound = "Feature not found: %v"
ErrMsgLoadFunctions = "Unable to access Functions"
ErrMsgFunctionNotFound = "Function not found: %v"
ErrMsgFunctionAccess = "Unable to access Function: %v"
ErrMsgInvalidParameterValue = "Invalid value for parameter %v: %v"
ErrMsgInvalidQuery = "Invalid query parameters"
ErrMsgDataReadError = "Unable to read data from: %v"
ErrMsgDataWriteError = "Unable to write data to: %v"
ErrMsgNoDataRead = "No data read from: %v"
ErrMsgRequestTimeout = "Maximum time exceeded. Request cancelled."
ErrMsgEncoding = "Error encoding response"
ErrMsgLoadCollections = "Unable to access Collections"
ErrMsgCollectionNotFound = "Collection not found: %v"
ErrMsgCollectionAccess = "Unable to access Collection: %v"
ErrMsgFeatureNotFound = "Feature not found: %v"
ErrMsgCreateFeatureNotConform = "Unable to create new feature in Collection - data does not respect schema: %v"
ErrMsgCreateFeatureInCatalog = "Unable to create new feature in Collection - catalog error: %v"
ErrMsgLoadFunctions = "Unable to access Functions"
ErrMsgFunctionNotFound = "Function not found: %v"
ErrMsgFunctionAccess = "Unable to access Function: %v"
ErrMsgInvalidParameterValue = "Invalid value for parameter %v: %v"
ErrMsgInvalidQuery = "Invalid query parameters"
ErrMsgDataReadError = "Unable to read data from: %v"
ErrMsgDataWriteError = "Unable to write data to: %v"
ErrMsgNoDataRead = "No data read from: %v"
ErrMsgRequestTimeout = "Maximum time exceeded. Request cancelled."
)

const (
Expand Down Expand Up @@ -639,3 +642,10 @@ func PathFunctionItems(name string) string {
func PathItem(name string, fid string) string {
return fmt.Sprintf("%v/%v/%v/%v", TagCollections, name, TagItems, fid)
}

var Db2OpenapiFormatMap = map[string]string{
"int": "integer",
"int4": "integer",
"long": "int64",
"text": "string",
}
28 changes: 28 additions & 0 deletions internal/api/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ const (
// ContentTypeGeoJSON
ContentTypeGeoJSON = "application/geo+json"

// ContentTypeGML
ContentTypeGML = "application/gml+xml"

// ContentTypeSchemaJSON
ContentTypeSchemaJSON = "application/schema+json"

// ContentTypeHTML
ContentTypeHTML = "text/html"

Expand All @@ -49,6 +55,12 @@ const (

// FormatText code and extension for Text
FormatSVG = "svg"

// FormatJSON code and extension for JSON
FormatSchemaJSON = "schema+json"

// FormatXML code and extension for XML/GML
FormatXML = "xml"
)

// RequestedFormat gets the format for a request from extension or headers
Expand All @@ -70,12 +82,28 @@ func RequestedFormat(r *http.Request) string {
// Use Accept header if present
hdrAccept := r.Header.Get("Accept")
//fmt.Println("Accept:" + hdrAccept)
if strings.Contains(hdrAccept, ContentTypeSchemaJSON) {
return FormatSchemaJSON
}
if strings.Contains(hdrAccept, ContentTypeHTML) {
return FormatHTML
}
return FormatJSON
}

// RequestedFormat gets the format for a request from extension or headers
func SentDataFormat(r *http.Request) string {
// Use ContentType header if present
hdrContentType := r.Header.Get("Content-Type")
if strings.Contains(hdrContentType, ContentTypeGeoJSON) {
return FormatJSON
}
if strings.Contains(hdrContentType, ContentTypeGML) {
return FormatXML
}
return FormatJSON
}

// PathStripFormat removes a format extension from a path
func PathStripFormat(path string) string {
if strings.HasSuffix(path, ".html") || strings.HasSuffix(path, ".json") {
Expand Down
152 changes: 141 additions & 11 deletions internal/api/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,76 @@ package api
*/

import (
"encoding/json"
"net/url"

"github.com/CrunchyData/pg_featureserv/internal/conf"
"github.com/getkin/kin-openapi/openapi3"
log "github.com/sirupsen/logrus"
)

func getFeatureExample() map[string]interface{} {
var result map[string]interface{}
var jsonStr = `{"type":"Feature","geometry":{"type":"Point","coordinates":[-70.88461956597838,47.807897059236495]},"properties":{"prop_a":"propA","prop_b":1,"prop_c":"propC","prop_d":1}}`
err := json.Unmarshal([]byte(jsonStr), &result)
if err != nil {
return nil
}
return result
}

var FeatureSchema openapi3.Schema = openapi3.Schema{
Type: "object",
Required: []string{},
Properties: map[string]*openapi3.SchemaRef{
"id": {
Value: &openapi3.Schema{
OneOf: []*openapi3.SchemaRef{
openapi3.NewSchemaRef("", &openapi3.Schema{
Type: "number", Format: "long",
}),
openapi3.NewSchemaRef("", &openapi3.Schema{
Type: "string",
}),
},
},
},
"type": {
Value: &openapi3.Schema{
Type: "string",
Default: "Feature",
},
},
"geometry": {
Value: &openapi3.Schema{
Type: "string", // mandatory to validate the schema
OneOf: []*openapi3.SchemaRef{
openapi3.NewSchemaRef("https://geojson.org/schema/Point.json", &openapi3.Schema{Type: "string"}),
openapi3.NewSchemaRef("https://geojson.org/schema/LineString.json", &openapi3.Schema{Type: "string"}),
openapi3.NewSchemaRef("https://geojson.org/schema/Polygon.json", &openapi3.Schema{Type: "string"}),
openapi3.NewSchemaRef("https://geojson.org/schema/MultiPoint.json", &openapi3.Schema{Type: "string"}),
openapi3.NewSchemaRef("https://geojson.org/schema/MultiLineString.json", &openapi3.Schema{Type: "string"}),
openapi3.NewSchemaRef("https://geojson.org/schema/MultiPolygon.json", &openapi3.Schema{Type: "string"}),
},
},
},
"properties": {
Value: &openapi3.Schema{
Type: "object",
},
},
"bbox": {
Value: &openapi3.Schema{
Type: "array",
MinItems: 4,
MaxItems: openapi3.Uint64Ptr(4),
Items: openapi3.NewSchemaRef("", openapi3.NewFloat64Schema().WithMin(-180).WithMax(180)),
},
},
},
Example: getFeatureExample(),
}

// GetOpenAPIContent returns a Swagger OpenAPI structure
func GetOpenAPIContent(urlBase string) *openapi3.Swagger {

Expand Down Expand Up @@ -53,6 +116,16 @@ func GetOpenAPIContent(urlBase string) *openapi3.Swagger {
AllowEmptyValue: false,
},
}
paramFeatureID := openapi3.ParameterRef{
Value: &openapi3.Parameter{
Name: "featureId",
Description: "Id of feature in collection to retrieve data for.",
In: "path",
Required: true,
Schema: &openapi3.SchemaRef{Value: openapi3.NewStringSchema()},
AllowEmptyValue: false,
},
}
paramBbox := openapi3.ParameterRef{
Value: &openapi3.Parameter{
Name: "bbox",
Expand Down Expand Up @@ -211,6 +284,16 @@ func GetOpenAPIContent(urlBase string) *openapi3.Swagger {
AllowEmptyValue: false,
},
}
paramType := openapi3.ParameterRef{
Value: &openapi3.Parameter{
Name: "type",
Description: "Data schema type (create, update, etc.).",
In: "query",
Required: false,
Schema: &openapi3.SchemaRef{Value: openapi3.NewStringSchema()},
AllowEmptyValue: false,
},
}
return &openapi3.Swagger{
OpenAPI: "3.0.0",
Info: openapi3.Info{
Expand Down Expand Up @@ -342,7 +425,7 @@ func GetOpenAPIContent(urlBase string) *openapi3.Swagger {
Responses: openapi3.Responses{
"200": &openapi3.ResponseRef{
Value: &openapi3.Response{
Description: "GeoJSON Featuree Collection document containing data for features",
Description: "GeoJSON Feature Collection document containing data for features",
/*
// TODO: create schema for result?
Content: openapi3.NewContentWithJSONSchemaRef(
Expand All @@ -355,6 +438,62 @@ func GetOpenAPIContent(urlBase string) *openapi3.Swagger {
},
},
},
Post: &openapi3.Operation{
OperationID: "createCollectionFeature",
Parameters: openapi3.Parameters{
&paramCollectionID,
&paramFeatureID,
// TODO keep it for the next evolution
// &paramCrs,
},
RequestBody: &openapi3.RequestBodyRef{
Value: &openapi3.RequestBody{
Description: "Add a new feature",
Required: true,
Content: openapi3.NewContentWithJSONSchema(&FeatureSchema),
},
},
Responses: openapi3.Responses{
"201": &openapi3.ResponseRef{
Value: &openapi3.Response{
Description: "Empty body with location header",
Headers: map[string]*openapi3.HeaderRef{
"location": {
Value: &openapi3.Header{
Description: "Contains a link to access to the new feature data",
},
},
},
},
},
},
},
},
apiBase + "collections/{collectionId}/schema": &openapi3.PathItem{
Summary: "Feature schema for collection",
Description: "Provides access to data representation (schema) for any features in specified collection",
Get: &openapi3.Operation{
OperationID: "getCollectionSchema",
Parameters: openapi3.Parameters{
&paramCollectionID,
&paramType,
},
Responses: openapi3.Responses{
"200": &openapi3.ResponseRef{
Value: &openapi3.Response{
Description: "GeoJSON Feature Collection document containing data schema for specific type",
/*
// TODO: create schema for result?
Content: openapi3.NewContentWithJSONSchemaRef(
&openapi3.SchemaRef{
Ref: "http://geojson.org/schema/FeatureSchema.json",
},
),
*/
},
},
},
},
},
apiBase + "collections/{collectionId}/items/{featureId}": &openapi3.PathItem{
Summary: "Single feature data from collection",
Expand All @@ -363,16 +502,7 @@ func GetOpenAPIContent(urlBase string) *openapi3.Swagger {
OperationID: "getCollectionFeature",
Parameters: openapi3.Parameters{
&paramCollectionID,
&openapi3.ParameterRef{
Value: &openapi3.Parameter{
Name: "featureId",
Description: "Id of feature in collection to retrieve data for.",
In: "path",
Required: true,
Schema: &openapi3.SchemaRef{Value: openapi3.NewStringSchema()},
AllowEmptyValue: false,
},
},
&paramFeatureID,
&paramProperties,
&paramTransform,
&paramCrs,
Expand Down
2 changes: 2 additions & 0 deletions internal/conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func setDefaultConfig() {
viper.SetDefault("Database.TableIncludes", []string{})
viper.SetDefault("Database.TableExcludes", []string{})
viper.SetDefault("Database.FunctionIncludes", []string{"postgisftw"})
viper.SetDefault("Database.AllowWrite", false)

viper.SetDefault("Paging.LimitDefault", 10)
viper.SetDefault("Paging.LimitMax", 1000)
Expand Down Expand Up @@ -94,6 +95,7 @@ type Database struct {
TableIncludes []string
TableExcludes []string
FunctionIncludes []string
AllowWrite bool
}

// Metadata config
Expand Down
Loading
Loading