nic-6443 commented on code in PR #13201:
URL: https://github.com/apache/apisix/pull/13201#discussion_r3067584355


##########
apisix/discovery/nacos/client.lua:
##########
@@ -0,0 +1,363 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You 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.
+--
+
+--- Reusable HTTP client primitives for Nacos service discovery.
+--- Extracted from init.lua so that both static-config mode and
+--- dynamic-config mode can share the same core logic.
+
+local require         = require
+local http            = require('resty.http')
+local core            = require('apisix.core')
+local ipairs          = ipairs
+local pairs           = pairs
+local type            = type
+local ngx             = ngx
+local ngx_re          = require('ngx.re')
+local string          = string
+local string_sub      = string.sub
+local str_byte        = string.byte
+local str_find        = core.string.find
+local log             = core.log
+
+local auth_path = 'auth/login'
+local instance_list_path = 'ns/instance/list?healthyOnly=true&serviceName='
+local default_namespace_id = "public"
+local default_group_name = "DEFAULT_GROUP"
+
+
+local _M = {}
+
+
+-- ─── HTTP primitives ──────────────────────────────────────────────────
+
+function _M.request(request_uri, path, body, method, basic_auth, timeout)
+    local url = request_uri .. path
+    log.info('request url:', url)
+    local headers = {}
+    headers['Accept'] = 'application/json'
+
+    if basic_auth then
+        headers['Authorization'] = basic_auth
+    end
+
+    if body and 'table' == type(body) then
+        local err
+        body, err = core.json.encode(body)
+        if not body then
+            return nil, 'invalid body : ' .. err
+        end
+        headers['Content-Type'] = 'application/json'
+    end
+
+    local httpc = http.new()
+    timeout = timeout or {}
+    local connect_timeout = timeout.connect or 2000
+    local send_timeout = timeout.send or 5000
+    local read_timeout = timeout.read or 5000
+    httpc:set_timeouts(connect_timeout, send_timeout, read_timeout)
+    local res, err = httpc:request_uri(url, {
+        method = method,
+        headers = headers,
+        body = body,
+        ssl_verify = true,
+    })
+    if not res then
+        return nil, err
+    end
+
+    if not res.body or res.status ~= 200 then
+        return nil, 'status = ' .. res.status
+    end
+
+    local json_str = res.body
+    local data, decode_err = core.json.decode(json_str)
+    if not data then
+        return nil, decode_err
+    end
+    return data
+end
+
+
+-- ─── authentication ───────────────────────────────────────────────────
+
+function _M.get_token_param(base_uri, username, password, timeout)
+    if not username or not password then
+        return ''
+    end
+
+    local args = { username = username, password = password }
+    local data, err = _M.request(base_uri, auth_path .. '?' .. 
ngx.encode_args(args),
+                                 nil, 'POST', nil, timeout)
+    if err then
+        log.error('nacos login fail:', username, ' ', password, ' desc:', err)

Review Comment:
   Fixed in latest commit.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to