Skip to content
Permalink
Browse files

Mark/python new (#4)

* api test instance.

* detail error description

* diff test between data and advanced api

* new python samples

* modify test;
  • Loading branch information...
vaecole authored and octopus-dev committed Apr 4, 2018
1 parent eebf95b commit 282cdd59dd8f262f74ba0d1e4ae0304f5c859b61
Showing with 766 additions and 23 deletions.
  1. +6 −0 .gitignore
  2. +0 −22 Code/ApiSamples.sln
  3. +36 −0 Code/CSharp/ApiSamples.sln
  4. +9 −0 Code/CSharp/Octopus.ApiSamples.Test/App.config
  5. +68 −0 Code/CSharp/Octopus.ApiSamples.Test/Octopus.ApiSamples.Test.csproj
  6. +144 −0 Code/CSharp/Octopus.ApiSamples.Test/Program.cs
  7. +36 −0 Code/CSharp/Octopus.ApiSamples.Test/Properties/AssemblyInfo.cs
  8. 0 Code/{ → CSharp}/Octopus.ApiSamples/ApiCallException.cs
  9. 0 Code/{ → CSharp}/Octopus.ApiSamples/App.config
  10. 0 Code/{ → CSharp}/Octopus.ApiSamples/Models/CallResult.cs
  11. 0 Code/{ → CSharp}/Octopus.ApiSamples/Models/StartTaskResult.cs
  12. 0 Code/{ → CSharp}/Octopus.ApiSamples/Models/Task.cs
  13. 0 Code/{ → CSharp}/Octopus.ApiSamples/Models/TaskDataExportResult.cs
  14. 0 Code/{ → CSharp}/Octopus.ApiSamples/Models/TaskDataOffsetResult.cs
  15. 0 Code/{ → CSharp}/Octopus.ApiSamples/Models/TaskGroup.cs
  16. 0 Code/{ → CSharp}/Octopus.ApiSamples/Models/TaskStatusModel.cs
  17. 0 Code/{ → CSharp}/Octopus.ApiSamples/Models/Token.cs
  18. 0 Code/{ → CSharp}/Octopus.ApiSamples/Octopus.ApiSamples.csproj
  19. 0 Code/{ → CSharp}/Octopus.ApiSamples/Program.cs
  20. 0 Code/{ → CSharp}/Octopus.ApiSamples/Properties/AssemblyInfo.cs
  21. +1 −1 Code/{ → CSharp}/Octopus.ApiSamples/Samples/OctoparseSample.cs
  22. 0 Code/{ → CSharp}/Octopus.ApiSamples/Samples/OctopusSample.cs
  23. 0 Code/{ → CSharp}/Octopus.ApiSamples/Utils/HttpStatusErrors.cs
  24. 0 Code/{ → CSharp}/Octopus.ApiSamples/Utils/Requires.cs
  25. 0 Code/{ → CSharp}/Octopus.ApiSamples/packages.config
  26. +100 −0 Code/Python/main.py
  27. +347 −0 Code/Python/samples.py
  28. +19 −0 Code/Python/util.py
@@ -104,3 +104,9 @@ _UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
*.ide

# Python
__pycache__/
*.pyc
/Code/Python/.vscode
/Code/.vs

This file was deleted.

Oops, something went wrong.
@@ -0,0 +1,36 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26730.3
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Octopus.ApiSamples", "Octopus.ApiSamples\Octopus.ApiSamples.csproj", "{4388AAED-CFDA-4178-A1BA-EFB138EB8710}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Octopus.ApiSamples.Test", "Octopus.ApiSamples.Test\Octopus.ApiSamples.Test.csproj", "{815ADACD-46AA-47B3-BD46-FDE520DEB6C8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Inter|Any CPU = Inter|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4388AAED-CFDA-4178-A1BA-EFB138EB8710}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4388AAED-CFDA-4178-A1BA-EFB138EB8710}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4388AAED-CFDA-4178-A1BA-EFB138EB8710}.Inter|Any CPU.ActiveCfg = Release|Any CPU
{4388AAED-CFDA-4178-A1BA-EFB138EB8710}.Inter|Any CPU.Build.0 = Release|Any CPU
{4388AAED-CFDA-4178-A1BA-EFB138EB8710}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4388AAED-CFDA-4178-A1BA-EFB138EB8710}.Release|Any CPU.Build.0 = Release|Any CPU
{815ADACD-46AA-47B3-BD46-FDE520DEB6C8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{815ADACD-46AA-47B3-BD46-FDE520DEB6C8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{815ADACD-46AA-47B3-BD46-FDE520DEB6C8}.Inter|Any CPU.ActiveCfg = Inter|Any CPU
{815ADACD-46AA-47B3-BD46-FDE520DEB6C8}.Inter|Any CPU.Build.0 = Inter|Any CPU
{815ADACD-46AA-47B3-BD46-FDE520DEB6C8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{815ADACD-46AA-47B3-BD46-FDE520DEB6C8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {81B64536-B64A-4F6C-A034-90879C1ADA71}
EndGlobalSection
EndGlobal
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<appSettings>

</appSettings>
</configuration>
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{815ADACD-46AA-47B3-BD46-FDE520DEB6C8}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>Octopus.ApiSamples.Test</RootNamespace>
<AssemblyName>Octopus.ApiSamples.Test</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Inter|AnyCPU'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\Inter\</OutputPath>
<DefineConstants>TRACE;DEBUG;INTER</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>AnyCPU</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Octopus.ApiSamples\Octopus.ApiSamples.csproj">
<Project>{4388aaed-cfda-4178-a1ba-efb138eb8710}</Project>
<Name>Octopus.ApiSamples</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
@@ -0,0 +1,144 @@
using Octopus.ApiSamples.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Security.Cryptography;

namespace Octopus.ApiSamples.Test
{
class Program
{
public static Token Token;
public static string TaskId = "b14da7c6-d7e7-4f07-b0bc-175f59e86bc9";
public static string TaskIdOperation = "fdd0875e-545b-4c62-9bc1-e14e8bbb0c73";

static void Main(string[] args)
{
try
{
string address = "http://{0}.bazhuayu.com";
bool isAdvanced = true;
#if INTER
address = "http://{0}.octoparse.com";
TaskId = "dacd06a1-216d-4553-b73f-3b53416cc7b5";
TaskIdOperation = "82e80964-e104-48fa-b3cc-ed49fe68ed44";
#endif
string apiName = "dataapi";
if (isAdvanced)
{
apiName = "advancedapi";
}
address = string.Format(address, apiName);
if (args.Length > 0)
{
address = args[0];
}
if (args.Length > 1)
{
isAdvanced = bool.Parse(args[1]);
}
OctoparseSample octoparseSample = new OctoparseSample(address);
Token = octoparseSample.GetToken("username", "password");
Debug.Assert(Token?.AccessToken != null);
Console.WriteLine(Token.AccessToken + Token.ExpiresIn);

try
{
Token = octoparseSample.RefreshToken(Token.RefreshToken);
Debug.Assert(Token?.AccessToken != null);
Console.WriteLine("Token refreshed!");
Console.WriteLine(Token.AccessToken + Token.ExpiresIn);
}
catch (ApiCallException ex)
{
Console.WriteLine(ex.Error + ex.ErrorDescription);
}

Console.WriteLine("GetTaskDataByOffset:");
var wrappedData = octoparseSample.GetTaskDataByOffset(Token.AccessToken, TaskId, 0, 10);
Debug.Assert(wrappedData != null);
Console.WriteLine(wrappedData.DataList.Rows.Count + "/" + wrappedData.Total);
Console.WriteLine(wrappedData.DataList.Rows[0][0].ToString());
Console.WriteLine("OK.");

Console.WriteLine("RemoveTaskData:");
octoparseSample.RemoveTaskData(Token.AccessToken, TaskIdOperation);
Console.WriteLine("OK.");

Console.WriteLine("GetTaskGroups:");
var groups = octoparseSample.GetTaskGroups(Token.AccessToken);
Debug.Assert(groups != null);
Console.WriteLine(groups.Count);
Console.WriteLine(groups.First().Name);
Console.WriteLine("OK.");

Console.WriteLine("GetTasks:");
var tasks = octoparseSample.GetTasksByGroup(Token.AccessToken, groups.FirstOrDefault().Id);
Debug.Assert(tasks != null);
Console.WriteLine(tasks.Count);
Console.WriteLine(tasks.First().Name);
Console.WriteLine("OK.");

Console.WriteLine("ExportNotExportedData:");
var exportData1 = octoparseSample.ExportNotExportedData(Token.AccessToken, TaskId, 10);
Debug.Assert(exportData1 != null);
if (exportData1.DataList != null)
{
Console.WriteLine(exportData1.DataList.Rows.Count + "/" + exportData1.Total);
Console.WriteLine(exportData1.DataList.Rows[0][0].ToString());
}
Console.WriteLine("OK.");

Console.WriteLine("MarkDataExported:");
octoparseSample.MarkDataExported(Token.AccessToken, TaskId);
Console.WriteLine("OK.");

#region Advanced
if (isAdvanced)
{
Console.WriteLine("StartTask:");
var startRes = octoparseSample.StartTask(Token.AccessToken, TaskIdOperation);
Console.WriteLine(startRes);

System.Threading.Thread.Sleep(2000);

Console.WriteLine("Task Status:");
var statuses = octoparseSample.GetTaskStatusList(Token.AccessToken, new string[] { TaskIdOperation });
Console.WriteLine(statuses.FirstOrDefault().Status);

Console.WriteLine("StopTask:");
octoparseSample.StopTask(Token.AccessToken, TaskIdOperation);
Console.WriteLine("OK.");
statuses = octoparseSample.GetTaskStatusList(Token.AccessToken, new string[] { TaskIdOperation });
Console.WriteLine(statuses.FirstOrDefault().Status);

Console.WriteLine("UpdateTaskRuleProperty: navigateAction1.Url");
octoparseSample.UpdateTaskRuleProperty<string>(Token.AccessToken, TaskIdOperation, "navigateAction1.Url", "http://acm.sjtu.edu.cn/OnlineJudge/problems?page=" + new Random().Next(1, 100));
Console.WriteLine("OK.");

Console.WriteLine("GetTaskRulePropertyByName: navigateAction1.Url");
var properties = octoparseSample.GetTaskRulePropertyByName<string[]>(Token.AccessToken, TaskIdOperation, "navigateAction1.Url");
Console.WriteLine(properties.FirstOrDefault());

Console.WriteLine("UpdateTaskRuleProperty: loopAction2.TextList");
octoparseSample.UpdateTaskRuleProperty<string[]>(Token.AccessToken, TaskIdOperation, "loopAction2.TextList", new string[] { "testText1", "testText2" });
Console.WriteLine("OK.");

Console.WriteLine("GetTaskRulePropertyByName: loopAction2.TextList");
properties = octoparseSample.GetTaskRulePropertyByName<string[]>(Token.AccessToken, TaskIdOperation, "loopAction2.TextList");
Console.WriteLine(properties.FirstOrDefault());
}
}
catch (ApiCallException ex)
{
Console.WriteLine(ex.Error + ex.ErrorDescription);
}
Console.WriteLine("Test finished!");
Console.ReadKey();
#endregion
}
}
}
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// 有关程序集的一般信息由以下
// 控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("Octopus.ApiSamples.Test")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Octopus.ApiSamples.Test")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// 将 ComVisible 设置为 false 会使此程序集中的类型
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
//请将此类型的 ComVisible 特性设置为 true。
[assembly: ComVisible(false)]

// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("815adacd-46aa-47b3-bd46-fde520deb6c8")]

// 程序集的版本信息由下列四个值组成:
//
// 主版本
// 次版本
// 生成号
// 修订号
//
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
// 方法是按如下所示使用“*”: :
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
@@ -371,7 +371,7 @@ private static void ThrowExceptionIfResponseError(IRestResponse response)

if (response.StatusCode != HttpStatusCode.OK)
{
throw new ApiCallException(HttpStatusErrors.GetError(response.StatusCode));
throw new ApiCallException(HttpStatusErrors.GetError(response.StatusCode), response.Content);
}
}

Oops, something went wrong.

0 comments on commit 282cdd5

Please sign in to comment.
You can’t perform that action at this time.