Skip to content

Commit

Permalink
use post method instead of get
Browse files Browse the repository at this point in the history
  • Loading branch information
antonzaharov committed Nov 27, 2024
1 parent 457b225 commit c780607
Showing 1 changed file with 50 additions and 36 deletions.
86 changes: 50 additions & 36 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package main

import (
"errors"
"fmt"
"io"
"net/http"
url2 "net/url"
"net/url"
)

const (
defaultBaseURL = "https://api.reg.ru/api/regru2/"
defaultBaseURL = "https://api.reg.ru/api/regru2"
)

type RegruClient struct {
Expand All @@ -27,73 +26,88 @@ func NewRegruClient(username string, password string, zone string) *RegruClient
}

func (c *RegruClient) getRecords() error {
s := fmt.Sprintf("{\"username\":\"%s\",\"password\":\"%s\",\"domains\":[{\"dname\":\"%s\"}],\"output_content_type\":\"plain\"}", c.username, c.password, c.zone)
url := fmt.Sprintf("%szone/get_resource_records?input_data=%s&input_format=json", defaultBaseURL, url2.QueryEscape(s))
inputData := fmt.Sprintf(`{"username":"%s","password":"%s","domains":[{"dname":"%s"}],"output_content_type":"plain"}`, c.username, c.password, c.zone)

fmt.Println("Get TXT Query:", url)
res, err := http.Get(url)
requestData := url.Values{}
requestData.Set("input_data", inputData)
requestData.Set("input_format", "json")

requestURL := fmt.Sprintf("%s/zone/get_resource_records", defaultBaseURL)

fmt.Printf("[%s] Get resource records\n", c.zone)
res, err := http.PostForm(requestURL, requestData)
if err != nil {
return fmt.Errorf("failed to make GET request: %v", err)
return fmt.Errorf("[%s] failed to make get resource records request POST %s: %v", c.zone, requestURL, err)
}
defer res.Body.Close()

body, err := io.ReadAll(res.Body)
res.Body.Close()
if res.StatusCode > 299 {
return errors.New(fmt.Sprintf("response failed with status code: %d and body: %s", res.StatusCode, body))
}
if err != nil {
return fmt.Errorf("failed to ready response")
return fmt.Errorf("[%s] failed to read get resource records response %d body: %v", c.zone, res.StatusCode, err)
}
if res.StatusCode > 299 {
return fmt.Errorf("[%s] get resource records response failed with status code %d and body: %s", c.zone, res.StatusCode, body)
}

fmt.Printf("Get TXT success. Response body: %s", body)
fmt.Printf("[%s] Get TXT resource record success. Response body: %s\n", c.zone, body)

return nil
}

func (c *RegruClient) createTXT(domain string, value string) error {
s := fmt.Sprintf("{\"username\":\"%s\",\"password\":\"%s\",\"domains\":[{\"dname\":\"%s\"}],\"subdomain\":\"%s\",\"text\":\"%s\",\"output_content_type\":\"plain\"}", c.username, c.password, c.zone, domain, value)
url := fmt.Sprintf("%szone/add_txt?input_data=%s&input_format=json", defaultBaseURL, url2.QueryEscape(s))
inputData := fmt.Sprintf(`{"username":"%s","password":"%s","domains":[{"dname":"%s"}],"subdomain":"%s","text":"%s","output_content_type":"plain"}`, c.username, c.password, c.zone, domain, value)

requestData := url.Values{}
requestData.Set("input_data", inputData)
requestData.Set("input_format", "json")

fmt.Println("Create TXT Query:", url)
res, err := http.Get(url)
requestURL := fmt.Sprintf("%s/zone/add_txt", defaultBaseURL)

fmt.Printf("[%s] Add TXT record\n", c.zone)
res, err := http.PostForm(requestURL, requestData)
if err != nil {
return fmt.Errorf("failed to make GET request: %v", err)
return fmt.Errorf("[%s] failed to make add TXT record request POST %s: %v", c.zone, requestURL, err)
}
defer res.Body.Close()

body, err := io.ReadAll(res.Body)
res.Body.Close()
if res.StatusCode > 299 {
return errors.New(fmt.Sprintf("response failed with status code: %d and body: %s", res.StatusCode, body))
}
if err != nil {
return fmt.Errorf("failed to ready response")
return fmt.Errorf("[%s] failed to read add TXT record response %d body: %v", c.zone, res.StatusCode, err)
}
if res.StatusCode > 299 {
return fmt.Errorf("[%s] add TXT record response failed with status code %d and body: %s", c.zone, res.StatusCode, body)
}

fmt.Printf("Create TXT success. Response body: %s", body)
fmt.Printf("[%s] Add TXT record success. Response body: %s\n", c.zone, body)

return nil
}

func (c *RegruClient) deleteTXT(domain string, value string) error {
s := fmt.Sprintf("{\"username\":\"%s\",\"password\":\"%s\",\"domains\":[{\"dname\":\"%s\"}],\"subdomain\":\"%s\",\"content\":\"%s\",\"record_type\":\"TXT\",\"output_content_type\":\"plain\"}", c.username, c.password, c.zone, domain, value)
url := fmt.Sprintf("%szone/remove_record?input_data=%s&input_format=json", defaultBaseURL, url2.QueryEscape(s))
inputData := fmt.Sprintf(`{"username":"%s","password":"%s","domains":[{"dname":"%s"}],"subdomain":"%s","content":"%s","record_type":"TXT","output_content_type":"plain"}`, c.username, c.password, c.zone, domain, value)

fmt.Println("Delete TXT Query:", url)
res, err := http.Get(url)
requestData := url.Values{}
requestData.Set("input_data", inputData)
requestData.Set("input_format", "json")

requestURL := fmt.Sprintf("%s/zone/remove_record", defaultBaseURL)

fmt.Printf("[%s] Remove TXT record\n", c.zone)
res, err := http.PostForm(requestURL, requestData)
if err != nil {
return fmt.Errorf("failed to make GET request: %v", err)
return fmt.Errorf("[%s] failed to make remove TXT record request POST %s: %v", c.zone, requestURL, err)
}
defer res.Body.Close()

body, err := io.ReadAll(res.Body)
res.Body.Close()
if res.StatusCode > 299 {
return errors.New(fmt.Sprintf("response failed with status code: %d and body: %s", res.StatusCode, body))
}
if err != nil {
return fmt.Errorf("failed to ready response")
return fmt.Errorf("[%s] failed to read remove TXT record response %d body: %v", c.zone, res.StatusCode, err)
}
if res.StatusCode > 299 {
return fmt.Errorf("[%s] remove TXT record response failed with status code %d and body: %s", c.zone, res.StatusCode, body)
}

fmt.Printf("Delete TXT success. Response body: %s", body)
fmt.Printf("[%s] Remove TXT record success. Response body: %s\n", c.zone, body)

return nil
}

0 comments on commit c780607

Please sign in to comment.