Skip to content
Merged
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
2 changes: 2 additions & 0 deletions cpp/include/darabonba/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ class Client {

static bool is5xx(const shared_ptr<int> &code);

static string getHostName();

Client(){};
~Client(){};
};
Expand Down
9 changes: 9 additions & 0 deletions cpp/src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <darabonba/util.hpp>
#include <iostream>
#include <map>
#include <unistd.h>

using namespace std;

Expand Down Expand Up @@ -70,3 +71,11 @@ void Darabonba_Util::Client::sleep(const shared_ptr<int> &millisecond) {
int m = !millisecond ? 0 : *millisecond;
boost::this_thread::sleep_for(boost::chrono::milliseconds(m));
}

string Darabonba_Util::Client::getHostName() {
char hostname[256];
if (gethostname(hostname, sizeof(hostname)) == 0) {
return string(hostname);
}
return "";
}
6 changes: 6 additions & 0 deletions cpp/tests/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ using namespace Darabonba_Util;
TEST(tests_util, test_getDateUTCString) {
ASSERT_EQ(29, Client::getDateUTCString().size());
}

TEST(tests_util, test_getHostName) {
string hostname = Client::getHostName();
// hostname should not be empty in most cases
ASSERT_GE(hostname.length(), 0);
}
18 changes: 17 additions & 1 deletion csharp/core/Common.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -535,6 +535,22 @@ public static Stream AssertAsReadable(object a)
throw new ArgumentException("The value is not Stream");
}

/// <summary>
/// Get hostname of current machine
/// </summary>
/// <returns>the string value</returns>
public static string GetHostName()
{
try
{
return System.Net.Dns.GetHostName();
}
catch
{
return "";
}
}

internal static string GetDefaultUserAgent()
{
string OSVersion = Environment.OSVersion.ToString();
Expand Down
15 changes: 15 additions & 0 deletions csharp/tests/CommonTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -716,4 +716,19 @@ public class TestRegSubModel : TeaModel

public int? testInt { get; set; }
}

public class GetHostNameTest
{
[Fact]
public void Test_GetHostName()
{
string hostname = Common.GetHostName();
Assert.NotNull(hostname);
// hostname should not be empty in most cases
if (!string.IsNullOrEmpty(hostname))
{
Assert.True(hostname.Length > 0);
}
}
}
}
9 changes: 9 additions & 0 deletions golang/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"math/rand"
"net/http"
"net/url"
"os"
"reflect"
"runtime"
"strconv"
Expand Down Expand Up @@ -633,3 +634,11 @@ func ReadAsSSE(body io.ReadCloser) (<-chan SSEEvent, <-chan error) {
}()
return eventChannel, errorChannel
}

func GetHostName() *string {
hostname, err := os.Hostname()
if err != nil {
return tea.String("")
}
return tea.String(hostname)
}
9 changes: 9 additions & 0 deletions golang/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,3 +500,12 @@ func TestToJSONString(t *testing.T) {
str = ToJSONString(testMap)
utils.AssertEqual(t, "{\"key\":\"value\",\"map\":{\"str\":\"test&<>://中文\"},\"num\":1}", tea.StringValue(str))
}

func Test_GetHostName(t *testing.T) {
hostname := GetHostName()
utils.AssertNotNil(t, hostname)
// hostname should not be empty in most cases
if tea.StringValue(hostname) != "" {
utils.AssertEqual(t, true, len(tea.StringValue(hostname)) > 0)
}
}
13 changes: 13 additions & 0 deletions java/src/main/java/com/aliyun/teautil/Common.java
Original file line number Diff line number Diff line change
Expand Up @@ -597,5 +597,18 @@ public static List<Map<String, Object>> toArray(Object input) {
return null;
}
}

/**
* Get hostname of current machine
*
* @return the string value
*/
public static String getHostName() {
try {
return java.net.InetAddress.getLocalHost().getHostName();
} catch (Exception e) {
return "";
}
}
}

10 changes: 10 additions & 0 deletions java/src/test/java/com/aliyun/teautil/CommonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -643,4 +643,14 @@ public Map<String, List<Integer>> getIntegerListMap() {
return this.integerListMap;
}
}

@Test
public void getHostNameTest() {
String hostname = Common.getHostName();
Assert.assertNotNull(hostname);
// hostname should not be empty in most cases
if (!hostname.isEmpty()) {
Assert.assertTrue(hostname.length() > 0);
}
}
}
6 changes: 6 additions & 0 deletions main.tea
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ static function assertAsArray(value: any)throws : [ any ];
*/
static function getUserAgent(userAgent: string): string;

/**
* Get hostname of current machine
* @return the string value
*/
static function getHostName(): string;

/**
* If the code between 200 and 300, return true, or return false
* @return boolean
Expand Down
10 changes: 10 additions & 0 deletions php/src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -597,4 +597,14 @@ public static function assertAsReadable($value)

throw new \InvalidArgumentException('It is not a stream value.');
}

/**
* Get hostname of current machine.
*
* @return string the string value
*/
public static function getHostName()
{
return gethostname() ?: '';
}
}
10 changes: 10 additions & 0 deletions php/tests/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -547,4 +547,14 @@ public function toMap()

return $res;
}

public function testGetHostName()
{
$hostname = Utils::getHostName();
$this->assertNotNull($hostname);
// hostname should not be empty in most cases
if ($hostname !== '') {
$this->assertTrue(strlen($hostname) > 0);
}
}
}
12 changes: 12 additions & 0 deletions python/alibabacloud_tea_util/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,3 +516,15 @@ def assert_as_array(
if not isinstance(value, list):
raise ValueError('The value is not a list')
return value

@staticmethod
def get_host_name() -> str:
"""
Get hostname of current machine
@return: the string value
"""
import socket
try:
return socket.gethostname()
except Exception:
return ''
7 changes: 7 additions & 0 deletions python/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,10 @@ def test_assert_as_array(self):
assert False
except Exception as e:
self.assertEqual('The value is not a list', str(e))

def test_get_host_name(self):
hostname = Client.get_host_name()
self.assertIsNotNone(hostname)
# hostname should not be empty in most cases
if hostname:
self.assertTrue(len(hostname) > 0)
4 changes: 4 additions & 0 deletions swift/Sources/TeaUtils/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,8 @@ public class Client {
}
throw TeaError("value is not a InputStream")
}

public static func getHostName() -> String {
return ProcessInfo.processInfo.hostName
}
}
9 changes: 9 additions & 0 deletions ts/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,15 @@ export default class Client {
}
throw new Error(`The value is not a readable`);
}

/**
* Get hostname of current machine
* @return the string value
*/
static getHostName(): string {
const os = require('os');
return os.hostname();
}
}


9 changes: 9 additions & 0 deletions ts/test/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,4 +393,13 @@ describe('Tea Util', function () {
const result = Client.assertAsReadable(readable);
assert.deepStrictEqual(result, readable);
});

it('getHostName', function () {
const hostname = Client.getHostName();
assert.ok(hostname);
// hostname should not be empty in most cases
if (hostname) {
assert.ok(hostname.length > 0);
}
});
});
Loading