Skip to content

Commit

Permalink
Merge pull request #11 from mausworks/feature/contexts
Browse files Browse the repository at this point in the history
Contexts support.
  • Loading branch information
mausworks authored Nov 19, 2017
2 parents 0fd457a + 8db0a17 commit fa82eb4
Show file tree
Hide file tree
Showing 13 changed files with 233 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/Pidget.AspNet/Pidget.AspNet.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>0.2.1</Version>
<Version>0.3.0</Version>
<TargetFramework>netstandard2.0</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<DebugType>Full</DebugType>
Expand Down
49 changes: 35 additions & 14 deletions src/Pidget.Client/DataModels/ArbitraryData.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using Newtonsoft.Json;

namespace Pidget.Client.DataModels
{
[JsonDictionary]
public class ArbitraryData : IReadOnlyDictionary<string, object>
/// <summary>
/// A dictionary-like name/object data structure.
/// </summary>
public class ArbitraryData : IDictionary<string, object>
{
public IEnumerable<string> Keys => _data.Keys;
public ICollection<string> Keys => _data.Keys;

public IEnumerable<object> Values => _data.Values;
public ICollection<object> Values => _data.Values;

public int Count => _data.Count;

public object this[string key] => _data[key];
public bool IsReadOnly => _data.IsReadOnly;

private readonly IDictionary<string, object> _data
= new Dictionary<string, object>();
= new Dictionary<string, object>(StringComparer.Ordinal);

public ArbitraryData Set(string name, object data)
{
_data[name] = data;

return this;
public object this[string key]
{
get => Get(key);
set => _data[key] = value;
}

public object Get(string name)
private object Get(string key)
{
_data.TryGetValue(name, out object value);
TryGetValue(key, out object value);

return value;
}
Expand All @@ -45,5 +45,26 @@ public bool ContainsKey(string key)

public bool TryGetValue(string key, out object value)
=> _data.TryGetValue(key, out value);

public void Add(string key, object value)
=> _data.Add(key, value);

public bool Remove(string key)
=> _data.Remove(key);

public void Add(KeyValuePair<string, object> item)
=> _data.Add(item);

public void Clear()
=> _data.Clear();

public bool Contains(KeyValuePair<string, object> item)
=> _data.Contains(item);

public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
=> _data.CopyTo(array, arrayIndex);

public bool Remove(KeyValuePair<string, object> item)
=> _data.Remove(item);
}
}
27 changes: 27 additions & 0 deletions src/Pidget.Client/DataModels/ContextsData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace Pidget.Client.DataModels
{
/// <summary>
/// Provides additional contextual data, typically related to the current user.
/// See also: https://docs.sentry.io/clientdev/interfaces/contexts/
/// </summary>
public class ContextsData : ArbitraryData
{
public OperatingSystemData OperatingSystem
{
get => this["os"] as OperatingSystemData;
set => this["os"] = value;
}

public DeviceData Device
{
get => this["device"] as DeviceData;
set => this["device"] = value;
}

public RuntimeData Runtime
{
get => this["runtime"] as RuntimeData;
set => this["runtime"] = value;
}
}
}
51 changes: 51 additions & 0 deletions src/Pidget.Client/DataModels/DeviceData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
namespace Pidget.Client.DataModels
{
/// <summary>
/// Describes the device that caused an event,
/// most appropriate for mobile applications.
/// </summary>
public class DeviceData : ArbitraryData
{
public string Name
{
get => this["name"] as string;
set => this["name"] = value;
}

public string Family
{
get => this["family"] as string;
set => this["family"] = value;
}

public string Model
{
get => this["model"] as string;
set => this["model"] = value;
}

public string ModelId
{
get => this["model_id"] as string;
set => this["model_id"] = value;
}

public string Architecture
{
get => this["arch"] as string;
set => this["arch"] = value;
}

public double? BatteryLevel
{
get => this["arch"] as double?;
set => this["battery_level"] = value;
}

public string Orientation
{
get => this["orientation"] as string;
set => this["orientation"] = value;
}
}
}
38 changes: 38 additions & 0 deletions src/Pidget.Client/DataModels/OperatingSystemData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace Pidget.Client.DataModels
{
/// <summary>
/// Defines the operating system that caused an event.
/// </summary>
public class OperatingSystemData : ArbitraryData
{
public string Name
{
get => this["name"] as string;
set => this["name"] = value;
}

public string Version
{
get => this["version"] as string;
set => this["version"] = value;
}

public string Build
{
get => this["build"] as string;
set => this["build"] = value;
}

public string KernelVersion
{
get => this["kernel_version"] as string;
set => this["kernel_version"] = value;
}

public bool? Rooted
{
get => this["rooted"] as bool?;
set => this["rooted"] = value;
}
}
}
20 changes: 20 additions & 0 deletions src/Pidget.Client/DataModels/RuntimeData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Pidget.Client.DataModels
{
/// <summary>
/// Describes the runtime in more detail.
/// </summary>
public class RuntimeData : ArbitraryData
{
public string Name
{
get => this["name"] as string;
set => this["name"] = value;
}

public string Version
{
get => this["version"] as string;
set => this["version"] = value;
}
}
}
3 changes: 3 additions & 0 deletions src/Pidget.Client/DataModels/SentryEventData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,8 @@ public class SentryEventData

[JsonProperty("user")]
public UserData User { get; set; }

[JsonProperty("contexts")]
public ContextsData Contexts { get; set; }
}
}
18 changes: 8 additions & 10 deletions src/Pidget.Client/DataModels/UserData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,26 @@ public class UserData : ArbitraryData
{
public string Id
{
get => (string)Get("id");
set => Set("id", value);
get => this["id"] as string;
set => this["id"] = value;
}

public string UserName
{
get => (string)Get("username");
set => Set("username", value);
get => this["username"] as string;
set => this["username"] = value;
}

[JsonProperty("email")]
public string Email
{
get => (string)Get("email");
set => Set("email", value);
get => this["email"] as string;
set => this["email"] = value;
}

[JsonProperty("ip_address")]
public string IpAddress
{
get => (string)Get("ip_address");
set => Set("ip_address", value);
get => this["ip_address"] as string;
set => this["ip_address"] = value;
}
}
}
2 changes: 1 addition & 1 deletion src/Pidget.Client/Pidget.Client.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>0.2.1</Version>
<Version>0.3.0</Version>
<TargetFramework>netstandard2.0</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<DebugType>Full</DebugType>
Expand Down
12 changes: 11 additions & 1 deletion src/Pidget.Client/SentryEventBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ private readonly Dictionary<string, object> _extraData

private readonly List<string> _fingerprint = new List<string>(4);

private ContextsData _contextsData;


/// <summary>
/// Sets the captured exception.
Expand Down Expand Up @@ -159,6 +161,13 @@ public SentryEventBuilder SetRequestData(RequestData request)
return this;
}

public SentryEventBuilder SetContexts(ContextsData contexts)
{
_contextsData = contexts;

return this;
}

public SentryEventData Build()
{
AssertValidity();
Expand All @@ -177,7 +186,8 @@ public SentryEventData Build()
Extra = _extraData,
Fingerprint = _fingerprint,
Request = _requestData,
User = _userData
User = _userData,
Contexts = _contextsData
};
}

Expand Down
5 changes: 2 additions & 3 deletions test/Pidget.Client.Test/DataModels/ArbitraryDataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void SetValue_GetsEnumerated(string key, object value)
{
var data = new ArbitraryData();

data.Set(key, value);
data[key] = value;

Assert.Equal(key, data.First().Key);
Assert.Equal(value, data.First().Value);
Expand All @@ -26,12 +26,11 @@ public void GetSetValue(string key, object value)
{
var data = new ArbitraryData();

data.Set(key, value);
data[key] = value;

Assert.Equal(1, data.Count);
Assert.Equal(key, data.Keys.First());
Assert.Equal(value, data.Values.First());
Assert.Equal(value, data.Get(key));
Assert.Equal(value, data[key]);
Assert.True(data.ContainsKey(key));
Assert.True(data.TryGetValue(key, out object outValue));
Expand Down
21 changes: 16 additions & 5 deletions test/Pidget.Client.Test/DsnTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ namespace Pidget.Client.Test
{
public class DsnTests
{
public const string PublicKey = "PUBLIC_KEY";
public const string PublicKey = "public_key";

public const string SecretKey = "SECRET_KEY";
public const string SecretKey = "secret_key";

public const string Host = "HOST";
public const string Host = "host";

public const string ProjectId = "PROJECT_ID";
public const string ProjectId = "project_id";

public const string Path = "/PATH/";
public const string Path = "/path/";

public static readonly Dsn SentryDsn = Dsn.Create(
$"https://{PublicKey}:{SecretKey}@{Host}{Path}{ProjectId}");
Expand All @@ -34,5 +34,16 @@ public void GetPath()
[Fact]
public void GetProjectId()
=> Assert.Equal(ProjectId, SentryDsn.GetProjectId());

[Fact]
public void ToString_ReturnsUriString()
=> Assert.Equal(SentryDsn.Uri.ToString(), SentryDsn.ToString());

[Fact]
public void GetCaptureUrl()
=> Assert.Equal(
expected: $"https://{Host}/api/{ProjectId}/store/",
actual: SentryDsn.GetCaptureUrl());

}
}
Loading

0 comments on commit fa82eb4

Please sign in to comment.