Skip to content

Commit

Permalink
osm-zoning sane logging of the error
Browse files Browse the repository at this point in the history
  • Loading branch information
baditaflorin committed Jun 14, 2024
1 parent b22cfa7 commit f04a1a6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
12 changes: 4 additions & 8 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 27 additions & 6 deletions osm-zoning/oauth/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func Init(cfg *config.Config) {
}

func createWayRequest(changesetID int, nodes []int64, tags map[string]string) (*http.Request, error) {
log.Printf("Creating way request for changeset %d with nodes %v and tags %v", changesetID, nodes, tags)
var tagsXML string
for key, value := range tags {
tagsXML += fmt.Sprintf(`<tag k="%s" v="%s"/>`, key, value)
Expand All @@ -60,6 +61,7 @@ func createWayRequest(changesetID int, nodes []int64, tags map[string]string) (*
}

func updateWayRequest(changesetID int, wayID int64, tags map[string]string) (*http.Request, error) {
log.Printf("Creating update request for way %d in changeset %d with tags %v", wayID, changesetID, tags)
var tagsXML string
for key, value := range tags {
tagsXML += fmt.Sprintf(`<tag k="%s" v="%s"/>`, key, value)
Expand All @@ -72,10 +74,13 @@ func updateWayRequest(changesetID int, wayID int64, tags map[string]string) (*ht
</way>
</osm>`, wayID, changesetID, tagsXML)

log.Printf("XmlData:\n %v", xmlData)

return utils.CreateRequest("PUT", fmt.Sprintf("https://api.openstreetmap.org/api/0.6/way/%d", wayID), "text/xml", []byte(xmlData))
}

func UpdateWayTags(cfg *config.Config, token *oauth2.Token, wayID int64, tags map[string]string) {
log.Printf("Updating way tags for way %d with tags %v", wayID, tags)
changesetID, err := CreateChangeset(cfg, token)
if err != nil {
log.Fatalf("Failed to create changeset: %v", err)
Expand All @@ -88,12 +93,13 @@ func UpdateWayTags(cfg *config.Config, token *oauth2.Token, wayID int64, tags ma
}
req.Header.Set("Authorization", "Bearer "+token.AccessToken)

log.Printf("Sending update request to URL %s with method %s", req.URL.String(), req.Method)
_, err = utils.DoRequest(client, req)
if err != nil {
log.Fatalf("Failed to update way: %v", err)
}

fmt.Printf("Way updated successfully\n")
log.Printf("Way %d updated successfully", wayID)

if err := CloseChangeset(token, changesetID); err != nil {
log.Fatalf("Failed to close changeset: %v", err)
Expand All @@ -112,11 +118,14 @@ func createWayUpdateRequest(changesetID int, wayID int64, tags map[string]string
%s
</way>
</osm>`, wayID, changesetID, tagsXML)
log.Printf("Creating way tags for way %d with tags %v", wayID, tags)
log.Printf("XmlData:\n %v", xmlData)

return utils.CreateRequest("PUT", fmt.Sprintf("https://api.openstreetmap.org/api/0.6/way/%d", wayID), "text/xml", []byte(xmlData))
}

func createChangesetRequest(cfg *config.Config, token *oauth2.Token) (*http.Request, error) {
log.Printf("Creating changeset request with comment: %s", cfg.ChangesetComment)
xmlData := fmt.Sprintf(`
<osm>
<changeset>
Expand All @@ -132,18 +141,19 @@ func CreateChangeset(cfg *config.Config, token *oauth2.Token) (int, error) {
client := Oauth2Config.Client(oauth2.NoContext, token)
req, err := createChangesetRequest(cfg, token)
if err != nil {
return 0, err
return 0, fmt.Errorf("failed to create request: %v", err)
}
req.Header.Set("Authorization", "Bearer "+token.AccessToken)

body, err := utils.DoRequest(client, req)
if err != nil {
return 0, err
return 0, fmt.Errorf("failed to execute request: %v", err)
}

var changesetID int
fmt.Sscanf(string(body), "%d", &changesetID)

log.Printf("Created changeset with ID %d", changesetID)
return changesetID, nil
}

Expand All @@ -158,10 +168,16 @@ func CloseChangeset(token *oauth2.Token, changesetID int) error {
req.Header.Set("Authorization", "Bearer "+token.AccessToken)

_, err = utils.DoRequest(client, req)
return err
if err != nil {
return fmt.Errorf("failed to execute request: %v", err)
}

log.Printf("Closed changeset with ID %d", changesetID)
return nil
}

func CreateMapWay(cfg *config.Config, token *oauth2.Token, nodes []int64, tags map[string]string) {
log.Printf("Creating map way with nodes %v and tags %v", nodes, tags)
changesetID, err := CreateChangeset(cfg, token)
if err != nil {
log.Fatalf("Failed to create changeset: %v", err)
Expand All @@ -174,19 +190,21 @@ func CreateMapWay(cfg *config.Config, token *oauth2.Token, nodes []int64, tags m
}
req.Header.Set("Authorization", "Bearer "+token.AccessToken)

log.Printf("Sending create request to URL %s with method %s", req.URL.String(), req.Method)
_, err = utils.DoRequest(client, req)
if err != nil {
log.Fatalf("Failed to create way: %v", err)
}

fmt.Printf("Way created successfully\n")
log.Printf("Way created successfully")

if err := CloseChangeset(token, changesetID); err != nil {
log.Fatalf("Failed to close changeset: %v", err)
}
}

func SaveChanges(cfg *config.Config, token *oauth2.Token) {
log.Println("Saving all pending changes")
changesetID, err := CreateChangeset(cfg, token)
if err != nil {
log.Fatalf("Failed to create changeset: %v", err)
Expand All @@ -196,23 +214,26 @@ func SaveChanges(cfg *config.Config, token *oauth2.Token) {
pendingChanges := osm.GetPendingChanges()

for wayID, tags := range pendingChanges {
log.Printf("Saving changes for way %d with tags %v", wayID, tags)
req, err := createWayUpdateRequest(changesetID, wayID, tags)
if err != nil {
log.Fatalf("Failed to create request: %v", err)
}
req.Header.Set("Authorization", "Bearer "+token.AccessToken)

log.Printf("Sending update request to URL %s with method %s", req.URL.String(), req.Method)
_, err = utils.DoRequest(client, req)
if err != nil {
log.Fatalf("Failed to update way: %v", err)
}

fmt.Printf("Way %d updated successfully\n", wayID)
log.Printf("Way %d updated successfully", wayID)
}

if err := CloseChangeset(token, changesetID); err != nil {
log.Fatalf("Failed to close changeset: %v", err)
}

osm.ClearPendingChanges()
log.Println("All changes saved successfully")
}
14 changes: 8 additions & 6 deletions osm-zoning/utils/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,25 @@ func CreateRequest(method, url, contentType string, body []byte) (*http.Request,
return nil, fmt.Errorf("failed to create request: %v", err)
}
req.Header.Set("Content-Type", contentType)
req.Header.Set("User-Agent", "OSM-ZoningApp/1.0")
req.Header.Set("Content-Length", fmt.Sprintf("%d", len(body)))
return req, nil
}

func DoRequest(client *http.Client, req *http.Request) ([]byte, error) {
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("request failed: %v", err)
return nil, fmt.Errorf("request failed for URL %s with method %s: %v", req.URL.String(), req.Method, err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("request failed: status code %d", resp.StatusCode)
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %v", err)
return nil, fmt.Errorf("failed to read response body for URL %s with method %s: %v", req.URL.String(), req.Method, err)
}

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("request failed for URL %s with method %s: status code %d and Body:%s", req.URL.String(), req.Method, resp.StatusCode, string(body))
}

return body, nil
Expand Down

0 comments on commit f04a1a6

Please sign in to comment.